C# 如何实现不用openFileDialog而指到当前程序所在跟目录bin文件下的文件

来源:百度知道 编辑:UC知道 时间:2024/05/21 09:19:43
我不想弹出那个文件选择框,只想点击按钮就直接把当前程序所在的跟目录的文件路径给一个字符串变量,该怎么办,给点代码范例,谢谢,在线等,着急呢!是C# Winform窗口程序

/// <summary>
/// 获取当然exe文件所在的路径
/// </summary>
/// <returns>路径</returns>
public static string GetAbsolutePath()
{
string FolderPath = string.Empty;
try
{
string strCodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;

int n = strCodeBase.LastIndexOf('/');
if (n > 8)
{
FolderPath = strCodeBase.Substring(8, n - 8); // 8是 file:/// 的长度
}
else
{
FolderPath = ".";
}
}
catch
{
FolderPath = ".";
}
return FolderPath;
}

AppDomain.CurrentDomain.BaseDirectory
获取当前应用程序域的基目录
如果要获取应用程序根目录下的文件路径可以这样写:
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory
, "test.txt");

String path = "我是绝对路径下面的txt:&quo