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();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值