码迷,mamicode.com
首页 > 其他好文 > 详细

Path 类。这个类是专门用来操作路径的。

时间:2014-12-14 15:55:38      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:path 类   path   

<1>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Path路径
{
    /// <summary>
    /// Path 这个类是专门用来操作路径的。
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            string str = @"H:\鹿鼎记\第八集.rmvb";

            //现在我们要获取“第八集.rmvb”这个名字。我们可以这么获取。
            string fileName = str.Substring( str.LastIndexOf("\\")+1);
            Console.WriteLine(fileName); //输出:第八集.rmvb


            //-------------------------------------------------------


            //除了上面的方法外,我们还有更好的方法。-------获取文件名;
            string fileName2 = Path.GetFileName(str);
            Console.WriteLine(fileName2); //输出:第八集.rmvb

            //获取不带扩展名的文件名
            string fileName3 = Path.GetFileNameWithoutExtension(str);
            Console.WriteLine(fileName3); //输出:第八集

            //获取文件的扩展名
            string expansionName = Path.GetExtension(str);
            Console.WriteLine(expansionName); //输出:.rmvb

            //获取自定路径字符串的绝对路径
            string fullPath = Path.GetFullPath(str);
            Console.WriteLine(fullPath);//输出:H:\鹿鼎记\第八集.rmvb


            //连接“两个字符串”作为路径
            string strPath = Path.Combine(@"D:\a\", "b.txt");
            Console.WriteLine(strPath); //输出:D:\a\b.txt


            Console.ReadKey();
        }
    }
}


Path 类。这个类是专门用来操作路径的。

标签:path 类   path   

原文地址:http://blog.csdn.net/fanbin168/article/details/41925347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!