【问题标题】:Command Syntax for Processstartinfo from variables来自变量的 Processstartinfo 的命令语法
【发布时间】:2013-07-08 13:35:23
【问题描述】:

谁能帮我看看这里的语法

string sourcePath = @textBox2.Text.ToString();
string targetPath = @textBox1.Text.ToString();

System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"XCOPY C:\folder D:\Backup\folder /i");

psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process copyFolders = System.Diagnostics.Process.Start(psi);
copyFolders.WaitForExit();

我正在尝试更改下面的这一行以将 c:\folder 替换为 (sourcePath) 并将 D:\Backup\folder 替换为 targetPath

(@"XCOPY C:\folder D:\Backup\folder /i");

当我这样的消息框看起来不错时,我一直找不到源代码

MessageBox.Show(@"XCOPY " + (sourcePath) + (targetPath) + " /i");

【问题讨论】:

    标签: c# syntax processstartinfo


    【解决方案1】:

    你需要调试,我从这里开始:

    string args = string.format("{0} {1} /i", sourcePath, targetPath);
    Debug.WriteLine(args);
    //verify your paths (both) are correct
    
    string cmd = string.format("XCOPY {0}", args);
    Debug.WriteLine(cmd);
    //copy the command and test it out directly in cmd
    

    另外,尝试传递你的论点....作为论点....

    System.Diagnostics.ProcessStartInfo psi = 
      new System.Diagnostics.ProcessStartInfo("XCOPY", args);
    

    您可以查看the documentation 以获取带参数传递的示例

    【讨论】:

    • 感谢 Mike C 使用此代码字符串 args = (sourcePath) + " " + (targetPath); System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("XCOPY", args + " /i");
    【解决方案2】:

    看起来没有什么不对。也许调试它,以便您可以从中获取文本并将其复制/粘贴到资源管理器以确保路径有效并且没有什么时髦的事情发生?

    另外,您不需要对 TextBox 的 .Text 属性执行 .ToString()。它已经是一个字符串了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 2010-12-11
      • 2021-05-23
      相关资源
      最近更新 更多