【发布时间】:2014-01-13 08:21:39
【问题描述】:
我有一个项目,从 c:\work\SomeVariantFolder\MySolution\MyProject\Bin\Debug 运行,我需要从以下子文件夹之一执行该项目的命令行:c:\work\SomeVariantDev。 我面临的问题是从我的项目运行所在的文件夹到我想运行此命令行的文件夹。
请注意,我不能在此解决方案中使用批处理文件。
我试图做的 - 声明一个私有方法,该方法从同一个进程执行三个命令,向上四个文件夹然后执行我的命令,但这似乎不起作用。我觉得我在这里做错了什么,因为如果我从 c:\work\SomeVariantFolder\ 运行此命令,它运行良好。
var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine("cd..");
process.StandardInput.WriteLine("cd..");
process.StandardInput.WriteLine("cd..");
process.StandardInput.WriteLine("cd..");
process.StandardInput.WriteLine("my command");
请注意,由于我的解决方案的性质,我不能使用批处理文件,也不能使用 c:\work\SomeVariantFolder 作为硬编码文件夹,因为在某些情况下“SomeVariantFolder”名称可能会改变。
如有任何帮助,将不胜感激
【问题讨论】:
-
为了确保你在你想去的地方,你可以先选择驱动器
C:然后转到该驱动器的根目录cd\` then enter the directory you finally wantcd path\to\subfolder; keep in mind that you need to either escape "\" in c#-strings or precede the string with@`忽略转义序列 -
而不是
CD-ing 从一个目录到另一个目录,为什么不直接设置ProcessStartInfo的WorkingDirectory? -
@NahumLitvin,我尝试使用此示例,但无法满足我的需求。 Thomas W,这是一个我没有遇到的好答案。 IronGeek,我相信这是正确的答案,如果你能回答我的问题,我会标记你。
-
IronGeek 所说的。另外,不要忘记您可能会遇到权限问题 - 由于 Windows 以高优先级将 DLL 加载到工作目录中,如果您没有以 root 权限运行应用程序,您可能会被阻止更改工作目录。尝试以 root 权限运行应用程序始终是捕获许多权限问题的好方法:)
-
@user3150947 我已根据您的要求发布了答案