定义工作目录执行一个程序(C#) [英] Defining a working directory for executing a program (C#)

查看:226
本文介绍了定义工作目录执行一个程序(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图让一个可执行从一个特定的文件夹启动。

在code我下面的应用程序崩溃奇怪的是:

 进程p =新工艺();
p.StartInfo.WorkingDirectory =倾销;
p.StartInfo.FileName =秒;
p.Start();
 

我调试它,它说,它不能找到启动文件,但该文件/文件夹defintly存在,是我的语法不好?

下面作品的code,但一个工作directroy没有被定义,所以它不能找到可执行

 的Process.Start(@垃圾\,S);
 

解决方案

您设置(转储)的工作目录是相对于当前的工作目录。您可能要检查当前的工作目录。

您应该能够工作目录设置到执行程序集的目录与此code ...

 字符串exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly()位置。);
Directory.SetCurrentDirectory(exeDir);
 

或者更好的是,不要使用相对路径,设置p.StartInfo.WorkingDirectory的绝对路径。

I am currently trying to get an executable to be launched from a specific folder.

The code I have below crashes the application oddly enough:

Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = s;
p.Start();

I debugged it, and it was saying it can't find the file to start, but the file / folder defintly exists, is my syntax bad?

The code below works, but a working directroy is not defined, so it can't find the executable

Process.Start(@"dump\", s);

解决方案

The working directory that you set ("dump") is relative to the current working directory. You might want to check the current working directory.

You should be able to set the working directory to the executing assemblies directory with this code...

string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Directory.SetCurrentDirectory(exeDir);

Or, better yet, don't use a relative path, set p.StartInfo.WorkingDirectory to the absolute path.

这篇关于定义工作目录执行一个程序(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆