largeone
asked on
Spaces in Paths as Command Line Args (C#)
I have a windows service that calls an external "updater" .exe. I pass 3 args to the updater. The problem I'm having is that the second and third and are both paths that almost always contain whitespace characters. The updater app is always comparing files in "C:\Program Files\...\MyDir\ with files on a network share .that we have yet to establish.
The code is below, but the simple problem is that Void Main(args[]) sees "C:\Program" and "Files\...\MyDir\" as separate arguments because of the space in the path. Is there a way to escape that space and pass these two arguments as complete paths? Seems like this .NET world we're in should have devised a way around the old 8.3 naming conventions.
The code in my service:
string Update_Path=@"C:\Projects\ C#\MyUpdat edProject\ "); (this will be a UNC in production)
string Current_Path= @"C:\Program Files\MyCurProject\");
if(true)
UpdateMe("MyServiceName", Current_Path, Update_Path);
private void UpdateMe(string ServiceName, string CurPath, string UpdatePath)
{
Process proc = new Process();
string args = ServiceName + " " + @CurPath + " " + @UpdatePath + "";
proc.StartInfo.FileName = CurPath + WinServiceUpdater.exe";
proc.StartInfo.Arguments = args;
try
{
proc.Start();
}
catch (System.InvalidOperationEx ception IOex)
{
Debug.WriteLine(IOex.Messa ge);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message );
}
}
WinServiceUpdater.exe:
static void Main(string[] args)
{
if (args.Length == 0)
return;
string svcName = args[0]; //Name of service to update (will stop and start with this name)
string curPath = args[1]; //Directory Path of Current install files
string updatePath = args[2]; //Path Where Update files sit
DirectoryInfo UpdateSource = new DirectoryInfo(updatePath);
DirectoryInfo CurentFiles = new DirectoryInfo(curPath);
string debugmessage = "";
System.ServiceProcess.Serv iceControl ler target = new System.ServiceProcess.Serv iceControl ler(svcNam e);
if(target.Status == System.ServiceProcess.Serv iceControl lerStatus. Running)
target.Stop();
while (target.Status != System.ServiceProcess.Serv iceControl lerStatus. Stopped)
{
target.Refresh();
}
Thread.Sleep(2000);
foreach (FileInfo updateFile in UpdateSource.GetFiles("*.* "))
{
if (File.Exists(curPath + updateFile.Name))
{
debugmessage += "<br/>About to Delete: " + curPath + updateFile.Name;
//File.Delete(curPath + "\\" + updateFile.Name);
debugmessage += "<br/>About to Copy From: " + updatePath + updateFile.Name + " TO: " + curPath + updateFile.Name;
//File.Copy(updatePath + "\\" + updateFile.Name, curPath + "\\" + updateFile.Name);
}
else
{
debugmessage += "<br/>Does Not Exist: " + curPath + updateFile.Name;
}
}
debugmessage += "<br/><br/><br/><br/>targe t.status = " + target.Status.ToString();
EmailUtil.SendMessage(debu gmessage);
target.Start();
}
}
The code is below, but the simple problem is that Void Main(args[]) sees "C:\Program" and "Files\...\MyDir\" as separate arguments because of the space in the path. Is there a way to escape that space and pass these two arguments as complete paths? Seems like this .NET world we're in should have devised a way around the old 8.3 naming conventions.
The code in my service:
string Update_Path=@"C:\Projects\
string Current_Path= @"C:\Program Files\MyCurProject\");
if(true)
UpdateMe("MyServiceName", Current_Path, Update_Path);
private void UpdateMe(string ServiceName, string CurPath, string UpdatePath)
{
Process proc = new Process();
string args = ServiceName + " " + @CurPath + " " + @UpdatePath + "";
proc.StartInfo.FileName = CurPath + WinServiceUpdater.exe";
proc.StartInfo.Arguments = args;
try
{
proc.Start();
}
catch (System.InvalidOperationEx
{
Debug.WriteLine(IOex.Messa
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message
}
}
WinServiceUpdater.exe:
static void Main(string[] args)
{
if (args.Length == 0)
return;
string svcName = args[0]; //Name of service to update (will stop and start with this name)
string curPath = args[1]; //Directory Path of Current install files
string updatePath = args[2]; //Path Where Update files sit
DirectoryInfo UpdateSource = new DirectoryInfo(updatePath);
DirectoryInfo CurentFiles = new DirectoryInfo(curPath);
string debugmessage = "";
System.ServiceProcess.Serv
if(target.Status == System.ServiceProcess.Serv
target.Stop();
while (target.Status != System.ServiceProcess.Serv
{
target.Refresh();
}
Thread.Sleep(2000);
foreach (FileInfo updateFile in UpdateSource.GetFiles("*.*
{
if (File.Exists(curPath + updateFile.Name))
{
debugmessage += "<br/>About to Delete: " + curPath + updateFile.Name;
//File.Delete(curPath + "\\" + updateFile.Name);
debugmessage += "<br/>About to Copy From: " + updatePath + updateFile.Name + " TO: " + curPath + updateFile.Name;
//File.Copy(updatePath + "\\" + updateFile.Name, curPath + "\\" + updateFile.Name);
}
else
{
debugmessage += "<br/>Does Not Exist: " + curPath + updateFile.Name;
}
}
debugmessage += "<br/><br/><br/><br/>targe
EmailUtil.SendMessage(debu
target.Start();
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I should add this:
using "abc" + "" + "def" is really just adding an empty string. So the result of that is just "abcdef".
When you need to add special characters, you need to use the "\" escape character, as:
\"
\t
\n
etc.
Jim
using "abc" + "" + "def" is really just adding an empty string. So the result of that is just "abcdef".
When you need to add special characters, you need to use the "\" escape character, as:
\"
\t
\n
etc.
Jim
ASKER
Thanks for the input guys. For posterity's sake, the actual string that works for my purposes needed to have the ending backslashes doubled up and escaped, so the line of code looks like this:
string args = ServiceName + " " + "\"" + @CurPath + "\\\"" + " " + "\"" + @UpdatePath + "\\\"";
Points split cause neither of you was wrong.
string args = ServiceName + " " + "\"" + @CurPath + "\\\"" + " " + "\"" + @UpdatePath + "\\\"";
Points split cause neither of you was wrong.
Hi, Is there any other way available other than giving path within double quotes? In VB6, the path is treated as single argument even there are spaces within path, irrespective of adding quote or not. Please let me know your inputs.
Thanks
Samson.
Thanks
Samson.
ASKER
It used to look like this, but I changed it back:
string args = ServiceName + " " + "" + @CurPath + "" + " " + "" + @UpdatePath +"";