tags:

views:

55

answers:

2

Im creating a Windows Service and I want to put a dynamic path in the code. But it only accepts static code.

This works:

Process.Start("C:\\Program Files\\Program\\Program.exe", "-socket 12345");  

But this doesnt:

String path = "C:\\Program Files\\Program";  
String programName = "\\Program.exe";  
String fileLocation = path + programName;  
Process.Start(fileLocation, "-socket 12345");  

Someone can help me?

+2  A: 

You should never concat paths. Use Path.Combine instead.

String path = @"C:\Program Files\Program";  
String programName = "Program.exe";  
String fileLocation = System.IO.Path.Combine(path, programName);
Process.Start(fileLocation, "-socket 12345");  
Dmytrii Nagirniak
Just make sure to not try `"\\Program.exe"` like the OP wrote. If the second path starts with a \, it is considered rooted and returned without concatenation.
280Z28
+2  A: 

Your code examples result in identical calls to Process.Start. Whatever the problem is, it's not shown here.

280Z28
Why in the first sample it finds the file and in the second it doesnt?
Mig
@Mig, because you did not post complete code. The working and non-working code are identical.
Dmytrii Nagirniak