![](/img/trans.png)
[英]I am using System.Diagnostics.Process to run the sqlite special command but only half of the commands get executed
[英]How to run a command using System.Diagnostics.Process?
在命令提示符下運行命令的這段代碼有什么問題? 我嘗試運行此代碼,它沒有給出任何錯誤,並且沒有執行應有的功能。 如果我將命令復制到命令提示符並手動運行,它會很好地工作嗎?
謝謝!
[TestMethod]
public void TestProcess()
{
string command1 = @"sejda-console simplesplit --files -f C:\TestFiles\test.pdf -o C:\TestFiles\split1\ -s all";
ProcessStartInfo processInfo;
Process process;
//I have the batch file sejda-console in C:\sejda-console-3.2.83\bin so I concatenated the directory of the batch file with the actual command.
processInfo = new ProcessStartInfo("cmd.exe", @"C:\sejda-console-3.2.83\bin " + command1);
processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process = Process.Start(processInfo);
process.WaitForExit();
process.Close();
}
或者,我也嘗試這種方法,也不起作用。
[TestMethod]
public void TestProcess3()
{
string MyBatchFile = @"C:\sejda-console-3.2.83\bin\sejda-console.bat";
string _sourcePath = @"C:\TestFiles\test.pdf";
string _targetPath = @"C:\TestFiles\split1\";
var process = new Process
{
StartInfo = {
Arguments = String.Format("/C simplesplit --files -f {0} -o {1} -s all", _sourcePath, _targetPath)
}
};
process.StartInfo.FileName = MyBatchFile;
bool b = process.Start();
}
因此,您的代碼應如下所示:
[TestMethod]
public void TestProcess()
{
string command1 = @"sejda-console simplesplit --files -f C:\TestFiles\test.pdf -o C:\TestFiles\split1\ -s all";
ProcessStartInfo processInfo;
Process process;
//I have the batch file sejda-console in C:\sejda-console-3.2.83\bin so I concatenated the directory of the batch file with the actual command.
processInfo = new ProcessStartInfo("cmd.exe", @"/C \"C:\sejda-console-3.2.83\bin\" + command1 + "\"");
processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process = Process.Start(processInfo);
process.WaitForExit();
process.Close();
}
試試這個processInfo
:
var batch = "sejda-console.bat";
var sourcePath = @"C:\TestFiles\test.pdf";
var targetPath = @"C:\TestFiles\split1\";
var processInfo = new ProcessStartInfo();
processInfo.WorkingDirectory = @"C:\sejda-console-3.2.83\bin";
processInfo.FileName = "cmd.exe";
processInfo.Arguments = $"/C {batch} simplesplit --files -f \"{sourcePath}\" -o \"{targetPath}\" -s all";
// todo set windows style etc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.