CSharp获取当前路径
时间: 2023-09-09 14:09:58 浏览: 37
可以通过调用系统库中的方法获取当前程序的运行路径,具体实现代码如下:
```csharp
using System;
using System.IO;
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Console.WriteLine(path);
```
输出结果为当前程序运行路径。
相关问题
CSharp获取当前目录
获取当前目录可以使用C#中的System.IO命名空间中的Directory.GetCurrentDirectory()方法,示例代码如下:
```
using System.IO;
string currentDirectory = Directory.GetCurrentDirectory();
```
执行以上代码后,currentDirectory变量将包含当前目录的路径。
C#获取当前路径
你可以使用 C# 中的 `Environment.CurrentDirectory` 属性获取当前应用程序的工作目录,即当前路径。
以下是示例代码:
```csharp
string currentDirectory = Environment.CurrentDirectory;
Console.WriteLine("当前路径:" + currentDirectory);
```
在这个示例中,我们使用 `Environment.CurrentDirectory` 属性获取当前路径,并将其存储在字符串变量 `currentDirectory` 中。然后,我们使用 `Console.WriteLine` 方法将当前路径输出到控制台。