Link to home
Create AccountLog in
Avatar of eagle_ea
eagle_ea

asked on

System.Diagnostics.Process.Start

Hello experts
currently i'm executing .vbs file from C# windows form by below method,
System.Diagnostics.Process.Start("C:\\Documents and Settings\\user\\test.vbs",param);

test.vbs is an item in the same project.
is there a way to execute the .vbs file without giving the absolute path like above?
Avatar of BourbonKid
BourbonKid
Flag of United States of America image

You could use the following property to retrieve the base directory where your program is located. This may help :

System.Reflection.Assembly.GetEntryAssembly().Location

Open in new window


Note : This won't work with dynamically generated assemblies.
Avatar of eagle_ea
eagle_ea

ASKER

this gives me the location of the executable of the project.
Yes. Provided you include your vbs file in the project and modify the "Copy to output directory" for this file so that it is either always copied or copied if newer, your vbs file should always be located in the executable directory of the project at runtime.

If so you will be able to replace your hardcoded path with something like :

string filePath = System.IO.Path.Combine(System.Reflection.Assembly.GetEntryAssembly().Location, "test.vbs");
System.Diagnostics.Process.Start(filePath, param);

Open in new window


if i put your code the path becomes "../program.exe/test.vbs"
this is not a valid path
Right. The location is a file name. We should write :

string filePath = System.IO.Path.Combine(System.Reflection.Assembly.GetEntryAssembly().Location, "test.vbs");
System.IO.FileInfo file = new FileInfo(filePath);
System.Diagnostics.Process.Start(file.Directory.FullName, param);

Open in new window

it opens another program.exe running
Avatar of Bill Prew
Where do you want the VBS to be located, in the USER folder, in the folder with the EXE, or someplace else?

~bp
ASKER CERTIFIED SOLUTION
Avatar of BourbonKid
BourbonKid
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account