字符串帮忙!进程未启动,可能是因为参数字符串有点混乱

发布于 2024-09-05 04:29:09 字数 516 浏览 6 评论 0原文

我有以下代码:

ProcessStartInfo si = new ProcessStartInfo("./Resources/pdftk.exe", executionstring);
Process myproc = Process.Start(si);

在我的监视窗口中,在调试时,我看到以下内容:

alt text http://img529.imageshack.us/img529/174/watchn.png

我实际在命令提示符下输入的是:

pdftk.exe "C:\test.pdf" unpack_files output "C:\TEMP" dont_ask

但是,我认为该过程在某个地方没有使用正确的参数开始,因此没有完成其工作。有什么建议吗?

I have the following code:

ProcessStartInfo si = new ProcessStartInfo("./Resources/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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枫林﹌晚霞¤ 2024-09-12 04:29:09

调试器向您显示带有转义字符的字符串,这是 Visual Studio 调试器的工件。

例如 \"C:\\test.pdf\" 实际上是一个包含 "C:\test.pdf" 的字符串

传递的实际字符串正是您想要的所以问题出在别处。

(如果你想真正双重确定,只需在程序开始时打印出参数即可)

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)

戒ㄋ 2024-09-12 04:29:09

我成功了。我最终所做的是将字符串放入剪贴板,将其复制到记事本,然后分析它是否正确:

Clipboard.SetText(executionstring);

我的问题归结为 pdftk.exe 的参数形成不正确。

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.

擦肩而过的背影 2024-09-12 04:29:09

您可能错误地使用了反斜杠来构建执行字符串。
了解反斜杠的确切工作原理非常重要。
采用以下语句:

strcpy(buffer,"c:\tools");

如果您研究调试器中缓冲区的值,您将看到类似这样的内容:(

C:<tab>ools

可能会被一些视觉空间替换或根本没有替换)。
这是因为 \t 被编译器翻译为制表符。

要获得正确的缓冲区,您必须这样写:

strcpy(buffer,"c:\\tools");

第一个反斜杠转义第二个反斜杠,最终只有 1 个反斜杠。

但是,如果您像这样构建缓冲区:

buffer[0] = 'c';
buffer[1] = ':';
buffer[2] = '\\';
buffer[3] = '\\';
buffer[4] = 't';
...

那么缓冲区将是这样的:

c:\\tools

并且确实包含 2 个反斜杠。

这是因为是编译器解释反斜杠,而不是运行时。

结论:意识到反斜杠是由编译器解释的,并且只有当您在常量字符串或常量字符中使用反斜杠时,它们才会被解释。如果动态构造字符串,则无需使用 2 个反斜杠。

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.

很酷又爱笑 2024-09-12 04:29:09

看看那里:字符串文字

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文