代码改变世界

文件操作类Path

2012-08-15 18:09  C#与.NET探索者  阅读(166)  评论(0编辑  收藏  举报

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

namespace 文件操作Path类
{
    class Program
    {
        static void Main(string[] args)
        {
            string p = @"c:\abc\ab\a\b.txt";
            string fn = Path.GetFileName(p);
            Console.WriteLine(fn);
            //这里并没有修改了实际的文件名,知识把字符串p,改成了
            //"c:\abc\ab\a\b.jpg"
            string pResult = Path.ChangeExtension(p, ".jpg");
            Console.WriteLine(pResult);

            string dir = Path.GetDirectoryName(p);
            Console.WriteLine(dir);

            string s1 = @"c:\a\b";
            string s2 = "x.jpg";
            //string r = s1 + s2;
            string r = Path.Combine(s1, s2);
            Console.WriteLine(r);

            string ext = Path.GetExtension(p);
            Console.WriteLine(ext);

            string fnWithoutExt = Path.GetFileNameWithoutExtension(p);
            Console.WriteLine(fnWithoutExt);
            //获得指定路径对应的绝对路径(完整的路径。 )
            string fullPath = Path.GetFullPath("..");
            Console.WriteLine(fullPath);
            //
            Console.WriteLine("临时目录:{0}", Path.GetTempPath());
            Console.WriteLine();
            Console.WriteLine("临时文件名 :{0}", Path.GetTempFileName());
            
            Console.ReadKey();
        }
    }
}