C# 中有三种方法可获取当前路径: Environment.CurrentDirectory、Directory.GetCurrentDirectory 和 Assembly.GetExecutingAssembly().Location。它们分别返回当前工作目录的绝对路径、当前工作目录的绝对路径和当前应用程序的可执行文件路径。
C# 获取当前路径
获取当前路径在开发中经常会有用到,C#提供了多种方法来实现这一目的。
1. 使用Environment.CurrentDirectory
string path = Environment.CurrentDirectory;
此方法返回当前工作目录的绝对路径。
2. 使用Directory.GetCurrentDirectory
string path = Directory.GetCurrentDirectory();
此方法与Environment.CurrentDirectory相同,返回当前工作目录的绝对路径。
3. 使用Assembly.GetExecutingAssembly().Location
string path = Assembly.GetExecutingAssembly().Location;
此方法返回当前应用程序的可执行文件路径。
示例:
using System; using System.IO; namespace GetCurrentDirectory { class Program { static void Main() { // 使用 Environment.CurrentDirectory string path1 = Environment.CurrentDirectory; // 使用 Directory.GetCurrentDirectory string path2 = Directory.GetCurrentDirectory(); // 使用 Assembly.GetExecutingAssembly().Location string path3 = Assembly.GetExecutingAssembly().Location; Console.WriteLine("Current Directory:"); Console.WriteLine($"\tPath 1: {path1}"); Console.WriteLine($"\tPath 2: {path2}"); Console.WriteLine($"\tPath 3: {path3}"); } } }
以上就是c#怎么获取当前路径的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号