C#文件路径和文件名获取函数 C#中的文件路径获取函数和文件名字获取函数小结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#文件路径和文件名获取函数 C#中的文件路径获取函数和文件名字获取函数小结

  2021-03-21 我要评论
想了解C#中的文件路径获取函数和文件名字获取函数小结的相关内容吗,在本文为您仔细讲解C#文件路径和文件名获取函数的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,文件路径,文件名,下面大家一起来学习吧。

1. 获取绝对文件路径

复制代码 代码如下:

System.IO.Path.GetFullPath(string path)

string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath;

fullPath = Path.GetFullPath(path1);
fullPath = Path.GetFullPath(fileName);
fullPath = Path.GetFullPath(path2);


2. 获取文件名字(得到指定路径内的文件名,不包括扩展名)
复制代码 代码如下:

System.IO.Path.GetFileNameWithoutExtension(string path)
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileNameWithoutExtension(fileName);
result = Path.GetFileName(path);

3.获得包含 path 目录信息的string 或者为空引用
复制代码 代码如下:

System.IO.Path.GetDirectoryName(string path)

string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string rootPath = @"C:\";
string directoryName;
   
directoryName = Path.GetDirectoryName(fileName);
directoryName = Path.GetDirectoryName(path);
directoryName = Path.GetDirectoryName(rootPath);


4.合并两个路径字符串。
复制代码 代码如下:

System.IO.Path.Combine(String path1, String path2)

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们