开发某些特定软件程序时,需要我们获取某些文件的路径,但是,有些朋友会使用绝对路径,如果,用户使用了绝对路径,更改了文件位置,程序运行时,就会出现错误,那么,我们如何通过c#编程语言来获取某个文件的路径呢?
方法一:使用 AppDomain.CurrentDomain.baseDirectory
string appPath = AppDomain.CurrentDomain.baseDirectory;
Console.WriteLine(appPath);
方法二:使用 Application.StartupPath(仅适用于 Windows Forms)
using System.Windows.Forms;
string appPath = Application.StartupPath;
MessageBox.Show(appPath);
方法三:使用 Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
using System.Reflection;
using System.IO;
string appPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
Console.WriteLine(appPath);
方法四:使用 Environment.CurrentDirectory
string currentDir = Environment.CurrentDirectory;
Console.WriteLine(currentDir);

[VIP第1年] 指数:1




