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?
C#VB Script

Avatar of undefined
Last Comment
BourbonKid

8/22/2022 - Mon
BourbonKid

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.
eagle_ea

ASKER
this gives me the location of the executable of the project.
BourbonKid

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


Your help has saved me hundreds of hours of internet surfing.
fblack61
eagle_ea

ASKER
if i put your code the path becomes "../program.exe/test.vbs"
this is not a valid path
BourbonKid

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

eagle_ea

ASKER
it opens another program.exe running
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
BourbonKid

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question