我正在写一个使用文件名的小应用程序,但是有一个困难,我找不到可执行文件的名称,我尝试了很多,例如:
1. string exePath = Path.GetDirectoryName( Application.ExecutablePath);
2. string patch = System.Windows.Forms.Application.ExecutablePath;
3. Application.StartupPath
以编译错误结束。
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main()
{
WebRequest request = WebRequest.Create("http://*******/indata.php?nt=2");
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
//string exePath = Path.GetDirectoryName(Application.ExecutablePath);
//string patch = System.Windows.Forms.Application.ExecutablePath;
//Console.WriteLine(Application.StartupPath);
var path = System.Reflection.Assembly.GetEntryAssembly().Location;
response.Close();
}
}
}
最后我需要 indata.php?nt=(file name)
系统.dll
编码:
找到“c#获取应用程序文件名”
你的命名空间很奇怪。试试这个
如果您需要知道可执行应用程序的路径,我建议您使用
让我解释一下:@DIlshod 代码适用于项目,因此如果您尝试通过从应用程序调用此代码将其移动到单独的库项目,您将获得可执行程序集的路径,即dll 库(名称将与 *.dll 库完全对应,而不是 *.exe 文件)
通过提取命令行参数,您可以保证从操作系统获取可执行应用程序的路径,而不是当前可执行模块的路径。@Andrew 的解决方案也比反射更正确。
如果您只需要带扩展名的文件名: