C#获取文件的绝对路径
作者:野牛程序员:2023-12-15 20:52:17C#阅读 2907
可以使用Path类来获取文件的绝对路径。以下是在C#中获取文件的绝对路径的一种方法:
using System;
using System.IO;
class Program
{
static void Main()
{
// 指定文件相对路径或绝对路径
string relativePath = "相对路径/文件名.txt";
// 获取当前工作目录
string currentDirectory = Directory.GetCurrentDirectory();
// 使用Path.Combine将相对路径和当前工作目录合并成绝对路径
string absolutePath = Path.Combine(currentDirectory, relativePath);
// 输出文件的绝对路径
Console.WriteLine(absolutePath);
}
}请替换相对路径/文件名.txt为实际的文件路径。上述代码将获取文件的绝对路径并输出到控制台。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:python获取当前路径下的文件名
- 下一篇:信号放大
