Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取正在执行的exe';NET中的路径是什么?_C#_.net_Filesystems - Fatal编程技术网

C# 获取正在执行的exe';NET中的路径是什么?

C# 获取正在执行的exe';NET中的路径是什么?,c#,.net,filesystems,C#,.net,Filesystems,从位于c:/dir中的程序a.exe中,我需要打开文本文件c:/dir/text.txt。我不知道a.exe在哪里,但text.txt将始终位于同一路径中。如何从程序本身中获取当前正在执行的程序集的名称,以便访问文本文件 编辑: 如果a.exe是Windows服务怎么办?它没有应用程序,因为它不是Windows应用程序 提前谢谢 string exePath = Application.ExecutablePath; string startupPath = Application.Startu

从位于c:/dir中的程序a.exe中,我需要打开文本文件c:/dir/text.txt。我不知道a.exe在哪里,但text.txt将始终位于同一路径中。如何从程序本身中获取当前正在执行的程序集的名称,以便访问文本文件

编辑: 如果a.exe是Windows服务怎么办?它没有应用程序,因为它不是Windows应用程序

提前谢谢

string exePath = Application.ExecutablePath;
string startupPath = Application.StartupPath;
编辑- 不使用应用程序对象:

string path = System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
有关更多信息,请参见此处:


我通常通过以下方式访问包含应用程序的.exe的目录:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

获取您感兴趣的程序集(例如,分配给
System.Reflection.assembly a
变量):

  • System.Reflection.Assembly.GetEntryAssembly()
    ,或
  • typeof(X).Assembly
    对于您感兴趣的程序集中的类
    X
    (对于Windows窗体,您可以使用
    typeof(Program)
然后获取从中加载程序集
a
的文件的路径:

  • System.IO.Path.GetDirectoryName(a.Location)

Windows窗体应用程序中的
应用程序
对象也是一种可能性,如其他答案所述。

在NUnit中进行测试时,使用peSHlr的答案也很有效

MessageBox.Show("This program is located in: " + Environment.CurrentDirectory);
var thisType = typeof(MyCustomClass);

var codeLocation = Path.GetDirectoryName(thisType.Assembly.Location);

var codeLocationPath = Path.GetDirectoryName(codeLocation);

var appConfigPath = Path.Combine(codeLocationPath, "AppConfig");

在VB.NET中,我们可以通过以下方式获得:

Assembly.GetEntryAssembly.Location
在C#中:


我也是,看到其他答案后,我现在觉得我错过了一个窍门!对于那些在LINQPad中尝试这一行并获得NullReferenceException的人,
Assembly.GetEntryAssembly()
在LINQPad中返回null,但在您自己的.NET应用程序中,它应该返回非null值。请注意,如果在MS测试中执行此代码,则此代码不起作用:在这种情况下,GetEntryAssembly()返回null,代码失败。请参阅本文,了解如何在生产和单元测试中使用此功能:我使用
Assembly Assembly=Assembly.GetEntryAssembly()解决单元测试问题??Assembly.getExecutionGassembly()应用程序
类型位于
System.Windows.Forms
命名空间中-请参阅:WPF呢,应用程序不包含StartupPath方法??!!
Assembly.GetEntryAssembly().Location