iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#Path类---文件路径解读
  • 937
分享到

C#Path类---文件路径解读

C#Path类C#文件路径C#Path 2023-01-28 06:01:50 937人浏览 安东尼
摘要

目录C# Path类—文件路径C# 文件路径 Path类 测试总结C# Path类—文件路径 给定如下字符串类型filePath,表示一个文件路径: stri

C# Path类—文件路径

给定如下字符串类型filePath,表示一个文件路径:

string filePath = "D:\\Program\\Test\\Config.txt";

下面是Path类中的一些常用方法及执行的结果:

Path.GetFullPath(filePath);   //执行结果为==>"D:\\Program\\Test\\Config.txt";

Path.GetDirectoryName(filePath); //执行结果为==>D:\\Program\\Test

Path.GetFileName(filePath);  //执行结果为==>Config.txt

Path.GetFileNameWithoutExtension(filePath); //执行结果为==>Config

Path.GetExtension(filePath); //执行结果为==>.txt

Path.GetPathRoot(filePath); //执行结果为==>D:\

获取当前的程序目录:

AppDomain.CurrentDomain.BaseDirectory;    //执行结果==>"D:\\Program\\Test\\Bin\\Debug\\"
Application.StartupPath;    //执行结果==>"D:\\Program\\Test\\Bin\\Debug"

Environment.CurrentDirectory;//获取和设置当前目录(该进程从中启动的目录)的完全限定目录
Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名

C# 文件路径 Path类 测试

腾出点时间对Path类做一个系统的测试

    private void PathTest()
        {
            //------------必须的空间-------using System.Diagnostics;     using System.IO;
            string path = @"C:\Users\cks\Desktop\zzg\ERPWork1125\User.lua";
            Debug.Print(Path.ChangeExtension(path, "txt"));                     // 输出:-----C:\Users\cks\Desktop\zzg\ERPWork1125\User.txt
            string path1 = @"C:\Users\cks\Desktop\zzg";
            string path2 = @"gg/e.txt";
            Debug.Print(Path.Combine(path1, path2));                            //输出:-----C:\Users\cks\Desktop\zzg\gg/e.txt
            Debug.Print(Path.GetDirectoryName(path));                           //输出:-----C:\Users\cks\Desktop\zzg\ERPWork1125
            Debug.Print(Path.GetExtension(path));                               //输出:-----.lua
            Debug.Print(Path.GetFileName(path));                                //输出:-----User.lua
            Debug.Print(Path.GetFileNameWithoutExtension(path));                //输出:-----User
            Debug.Print(Path.GetFullPath(path));                                // 输出:-----C:\Users\cks\Desktop\zzg\ERPWork1125\User.lua
            Debug.Print(String.Join("/x/", Path.GetInvalidFileNameChars()));    //输出:-----"/x/</x/>/x/|/x/
            Debug.Print(String.Join("/a/", Path.GetInvalidPathChars()));        //输出:----- " /a/</a/>/a/|/a/
            Debug.Print(Path.GetPathRoot(path));                                //输出:-----C:\
            Debug.Print(Path.GetRandomFileName());                              //输出:-----0am13z3o.gzd
            Debug.Print(Path.GetTempFileName());                                //输出:-----C:\Users\cks\AppData\Local\Temp\tmp81E5.tmp
            Debug.Print(Path.HasExtension(path).ToString());                    //输出:-----True
            Debug.Print(Path.IsPathRooted(path).ToString());                    //输出:-----True
            Debug.Print(Path.AltDirectorySeparatorChar.ToString());             //输出:-----/
            Debug.Print(Path.DirectorySeparatorChar.ToString());                //  输出:-----\
            Debug.Print(String.Join("/x/", Path.InvalidPathChars));             //输出:-----"/x/</x/>/x/|/x/
            Debug.Print(Path.PathSeparator.ToString());                         //输出:-----;
            Debug.Print(Path.VolumeSeparatorChar.ToString());                   //输出:-----:  
            // LuaDLL.getc(stdin);
            //test t = new test();
        } 

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: C#Path类---文件路径解读

本文链接: https://www.lsjlt.com/news/178351.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作