c# - Path类

Path类的使用及方法:

1、Path.GetFileName( )

快速获取一个文件路径下的文件名扩展名, 返回字符串类型的 文件名.扩展名

static void Main(string[] args)
{
    //隶属于 namespace System.IO 命名空间下
    // 源码中Path类是一个静态类,所以不能创建对象,
    string path = @"E:\ai\dll\index.dll";
    string res = Path.GetFileName(path);
    Console.WriteLine(res); // index.dll
    Console.ReadLine();
}

2、Path.GetFileNameWithoutExtension ( )

获得文件名字,但是不包含扩展名

static void Main(string[] args)
{
    string path = @"E:\ai\dll\index.dll";
    string res = Path.GetFileNameWithoutExtension(path);
    Console.WriteLine(res); // index
    Console.ReadLine();
}

3、Path.GetExtension( )

直接获得扩展名

static void Main(string[] args)
{
    string path = @"E:\ai\dll\index.dll";
    string res = Path.GetExtension(path);
    Console.WriteLine(res); // .dll
    Console.ReadLine();
}

4、Path.getDirectoryName( )

从一个文件目录中获得这个文件所在文件夹的路径

static void Main(string[] args)
{
    string path = @"E:\ai\dll\index.dll";
    string res = Path.GetDirectoryName(path);
    Console.WriteLine(res); // E:\ai\dll
    Console.ReadLine();
}

5、Path.getFullPath( )

获得文件所在的全路径(绝对路径)

static void Main(string[] args)
{
    string path = @"E:\ai\dll\index.dll";
    string res = Path.GetFullPath(path);
    Console.WriteLine(res); // E:\ai\dll\index.dll
    Console.ReadLine();
}

6、Path.Combine( )

连接两个字符串,作为路径

static void Main(string[] args)
{
    string res = Path.Combine(@"E:\a\s", "index.html");
    Console.WriteLine(res); // E:\a\s\index.html
    Console.ReadLine();
}

猜你喜欢

转载自blog.csdn.net/qq_42778001/article/details/109058769
今日推荐