Trying to open an .exe file with a .txt file as an argument using Process.Start()

StackOverflow https://stackoverflow.com/questions/19375302

  •  30-06-2022
  •  | 
  •  

Вопрос

I have an .exe file which takes in a .txt file as an argument. However, I cant make it work using the code below in vb.net. It works when I run the .exe with cmd with the text file.

Dim a As New ProcessStartInfo
a.FileName = "C:\Users\Asim Rahman\Desktop\Project Input Files\DirectStiffness.exe"
a.Arguments = "C:\Users\Asim Rahman\Desktop\Project Input Files\HW3A.txt"
a.WindowStyle = ProcessWindowStyle.Maximized
Process.Start(a)

I have also tried opening the program and the file in numerous other ways, but I have failed to make it work. Any help would be greatly appreciated. Thanks!

Это было полезно?

Решение

I suppose that your exe file finds your text file by way of input args.
But your full path to the text file contains spaces and this breaks your args parameters to more than one element.
Your program refers to the first argument that is an incomplete path.

args[0] = "C:\users\asim" 
.....

and so on for every space present in your pathname to the txt file.

A simple solution is to specify the WorkingDirectory and remove the full path from the arguments

Dim a As New ProcessStartInfo
a.WorkingDirectory = "C:\Users\Asim Rahman\Desktop\Project Input Files"
a.FileName = "DirectStiffness.exe"
a.Arguments = "HW3A.txt"
a.WindowStyle = ProcessWindowStyle.Maximized
Process.Start(a)

Другие советы

The problem is probably that you need to quote the file name you pass as argument, because it contains spaces. If you don't wrap it in double quotes, your application will get the following parameters seperately:

C:\Users\Asim
Rahman\Desktop\Project
Input
Files\HW3A.txt

Try this:

C# version

a.Arguments = @"\"C:\Users\Asim Rahman\Desktop\Project Input Files\HW3A.txt\"";

VB version

a.Arguments = """C:\Users\Asim Rahman\Desktop\Project Input Files\HW3A.txt"""
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top