c# - 我包含的外部可执行文件的路径是什么?

标签 c# visual-studio-2010 visual-studio relative-path

因此,我可以使用以下代码在我的项目中成功运行带有各种参数的命令行应用程序:

String f = fileName;
Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "C:\\projects\\something\\MediaInfo.exe";
process.StartInfo.Arguments = " \"--Inform=Video;%Duration%|%Width%|%Height%;\" \"" + f + "\"";
process.Start();
StreamReader output = process.StandardOutput;
process.WaitForExit();
MessageBox.Show(output.ReadToEnd());

现在,我想指定一个相对路径,而不是我为 MediaInfo.exe 指定的绝对路径,这样当我将我的应用程序发送给某人时,他们就不需要费心将它放在正确的位置地方。

在 Visual Studio 中,我选择我的项目并单击添加现有项目。我添加了我的文件。然后选择“始终复制”。我已经为“构建操作”尝试了各种项目。我觉得“无”是正确的选择。

无论如何,我的目标是让 process.StartInfo.FileName 成为相对路径。我不知道该怎么做。有什么想法吗?

最佳答案

您的应用程序的根目录可以通过...获取

string appRoot = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

此语句需要 System.IO 和 System.Diagnostics 才能编译。

您可以附加到此值以到达应用程序根目录中的子目录,例如“data”,方法是...

string dataPath = Path.Combine(appRoot, "data");

应用程序需要以完全信任的方式运行才能使这些工作...

关于c# - 我包含的外部可执行文件的路径是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8859817/

相关文章:

c++ - 默认智能设备项目找不到依赖项

c# - AutomationElement 还活着?

c# - 使用静态类使 UI 元素可从 .NET 中的所有类访问是一种好习惯吗?

c++ - Visual Studio 调试器不会因调试断言失败而中断

visual-studio - 更改 Visual Studio 2013 的背景

visual-studio - 如何确定在 Powershell 的 Visual Studio 包管理器控制台中选择的默认项目?

c# - 为多个 VisualStateGroup 实现 VisualStates 的正确方法是什么?

c# - 无法从 android 中使用 WCF 方法

.net - 使用 Async V3 而不安装它

c# - MS Office Visual Studio 加载项、共享加载项和 Excel 2010 加载项之间有什么区别?