Mele Koneya
Mele Koneya

Reputation: 33

Handling Spaces in Application.ExecutablePath

I am getting a string variable for the Application.ExecutablePath and then passing the variable to a command line via ProcessStartInfo with some arguments. My code works fine as long as the "ExecutablePath" does not contain spaces, but if it does such as C:\documents and settings\jsmith\desktop, C# processes the spaces as escapes and I don't get results from the command line.

Here is how I am getting the Path and also sending the command "lmutil"

string execName = Application.ExecutablePath;
FileInfo execFileInfo = new FileInfo(execName);
execPath = execFileInfo.DirectoryName;

string lmUtil = @"\lmutil";
lmExec = execPath + lmUtil;

This is the method in which I need to handle quotes in paths 

GetLicStats(lmExec + " lmstat -a -c " + licport + "@" + curAdd);

How can I make sure that lmExec is handled correctly if it contains spaces.

Upvotes: 1

Views: 341

Answers (2)

btlog
btlog

Reputation: 4780

GetLicStats(lmExec + " lmstat -a -c " + licport + "@\"" + curAdd + "\"");

Put quotes around the address usually works

Upvotes: 3

Evan Mulawski
Evan Mulawski

Reputation: 55344

execPath = execFileInfo.DirectoryName.Replace(" ", "\ ");

Just replace the spaces with an escaped space.

Upvotes: 0

Related Questions