String help! Process isn't starting, probably because the argument string is kind of messed up
I have the following code:
ProcessStartInfo si = new ProcessStartInfo("./Resou开发者_开发知识库rces/pdftk.exe", executionstring);
Process myproc = Process.Start(si);
In my watch window, while debugging, I see this:
alt text http://img529.imageshack.us/img529/174/watchn.png
What I would actually type at a command prompt is:
pdftk.exe "C:\test.pdf" unpack_files output "C:\TEMP" dont_ask
However, I think somewhere in there the process isn't getting started with the correct arguments, and thus not doing its job. Any suggestions?
The debugger is showing you the string with escaped characters, this an artifact of the Visual Studio debugger.
e.g. \"C:\\test.pdf\"
is actual a string containing "C:\test.pdf"
The actual string being passed is exactly as you want it so the problem lies elsewhere.
(If you want to be really truly doubly sure, just print out the args at the start of the program)
I got it working. What I ended up doing was putting my string onto the clipboard, copying it to notepad, and then analyzing if it's correct:
Clipboard.SetText(executionstring);
My problem came down to the arguments to pdftk.exe being formed incorrectly.
You probably built your execution string with an incorrect use of the backslashes. It's important that you understand how backslashes work exactly. Take the following statement:
strcpy(buffer,"c:\tools");
If you investigate the value of buffer in the debugger you will see something like this:
C:<tab>ools
( will probably be replaced by some visual spaces or nothing at all). This is because \t is translated to the tab character by the compiler.
To get a correct buffer, you have to write this
strcpy(buffer,"c:\\tools");
The first backslash escapes the second one, ending up with only 1 backslash.
However, if you build up your buffer like this:
buffer[0] = 'c';
buffer[1] = ':';
buffer[2] = '\\';
buffer[3] = '\\';
buffer[4] = 't';
...
Then the buffer will be this:
c:\\tools
And will indeed contain 2 backslashes.
This is because it is the compiler that interprets the backslashes, not the runtime.
Conclusion: realize backslashes are interpreted by the compiler, and only if you use backslashes in constant strings or constant characters, they are interpreted. If you construct strings dynamically, there is no need to use 2 backslashes.
Take a look there : String literals
string a = "hello, world"; // hello, world
string b = @"hello, world"; // hello, world
string c = "hello \t world"; // hello world
string d = @"hello \t world"; // hello \t world
string e = "Joe said \"Hello\" to me"; // Joe said "Hello" to me
string f = @"Joe said ""Hello"" to me"; // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt
string h = @"\\server\share\file.txt"; // \\server\share\file.txt
精彩评论