C#Console在调用cmd的时候无论是cd还是F:盘符都无法更改cmd命令执行目录,有什么办法能解决呢?
一般问题倒好应付,但是遇到调用“使用log4j写log的程序”就会导致log位置错乱。请问大佬怎么解决???
这是代码
public static string input(string Input)
{
string logs = "";
ProcessStartInfo start = new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = false,
RedirectStandardInput = true
};
Process Reader = Process.Start(start);
using (Reader)
{
Reader.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceived);
Reader.BeginOutputReadLine();
Reader.StandardInput.WriteLine(Input);//输入命令
Reader.WaitForExit();
}
void OutputDataReceived(object sender, DataReceivedEventArgs Outputs)
{
if (null != Outputs)
{
Console.WriteLine(Outputs.Data);
logs = logs +"\n"+ Outputs.Data;
}
}
return logs;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string folder = Path.GetDirectoryName(filePath);
p.StandardInput.WriteLine(Path.GetPathRoot(folder).Trim('\\'));
p.StandardInput.WriteLine($"cd {folder}");
p.StandardInput.WriteLine(command);
p.StandardInput.AutoFlush = true;
p.StandardInput.Flush();
p.WaitForExit();
p.Close();
p.Dispose();