views:

106

answers:

3

I am running my c# application from "D:\App\program.cs" my application needs to execute a file placed in "c:\program files\software\abc.exe"

How can i set relative path in my program to execute the "abc.exe" ?

A: 

It's not clear what you mean by "set relative path", but if you're using Process and ProcessStartInfo to run the executable, I would suggest that you use an absolute path to specify the executable, and ProcessStartInfo.WorkingDirectory to tell the process where to run (so that relative paths will be evaluated appropriately within the new process).

EDIT: If you want the batch file to run c:\Program Files\Software\abc.exe then the contents of the batch file should be just:

"c:\Program Files\Software\abc.exe"

(Note the quotes to allow for space.)

I don't see what this has got to do with relative pathnames though.

Jon Skeet
My application will be creating a batch file with the path and executable name. And then execute the batch file via cmd as per requirement. In this case, i need to provide a relative path to the application in order to write to a batch file before it is executed. Clear ?
Anuya
@Jon Skeet, To make my question simpler.. I want to execute the .exe in "c:\program files\software\abc.exe" via double clicking the batch file in "D:\BatchFiles\ExecAbc.bat" Now what should be inside the batchfile inorder to execute the abc.exe ?? Thanks.
Anuya
@Anuya: I've edited my answer. It's still not at *all* clear where the relative path comes in. Perhaps you've misunderstood the term?
Jon Skeet
A: 

You shouldn't be using relative path for referring something from Program Files. I would recommend to use Environment.GetFolderPath (and Environment.SpecialFolder) to get path to Program Files and then use some config setting to get reminder path to the program.

VinayC
@VinayC, I should'nt be doing that or Cannot do that ? Any way my requirement is to use relative path to do this. Thanks.
Anuya
@Anuya, you can do that but should not be doing this because - for example "Program Files" can exist on any logical drive. You can use special folder to get path to your program and use full path within the batch file (assuming batch file is generated by code). Not sure why insistence on relative path.
VinayC
@Vinay, All the machines have program files on c drive. Its a part of system arrangement. May be that's why the requirement is like this. So in this case i think there will be no issues in using relative path pointing to "c:\program files\software\abc.exe". Now how can i do this ? Thanks.
Anuya
@Anuya A path pointing to c:\program files\software\abc.exe is not a relative path. It's absolute. You can't say what a relative path is pointing to since that depends on you current position. It's like saying that a compass needle always point to Spain
Rune FS
A: 

If the application is in Program Files then you can create a batch file like

"%ProgramFiles%\software\abc.exe"
ajay_whiz
ajay_whiz, I tried the above, pasting in my batchfile and double clicked it. Its not working !!!
Anuya
What is the error message? I hope you have put the `"` quotes?
ajay_whiz
I dint get any error message. Nothing happens. CMD is opened and closed by itself when i double click the batchfile. Thats it.
Anuya
@Anuya try running that file in command prompt `(Start-> Run-> cmd)` there you should be able to see the error.
ajay_whiz