这篇文章主要介绍了C#获取文件名和文件路径的两种实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
C#获取文件名和文件路径
方法一
OpenFileDialog open = new OpenFileDialog();
open.RestoreDirectory = true;
string fullname = open.FileName;
string path = System.IO.Path.GetDirectoryName(fullname);//路径
string name = System.IO.Path.GetFileName(fullname);//名称
方法二
OpenFileDialog open = new OpenFileDialog();
open.RestoreDirectory = true;
string fullpath = open.FileName;
//获取文件路径和文件名
int index = fullpath.LastIndexOf("//"); //返回“//”最后一次出现的位置
string filepath = fullpath.Substring(0,index); //截取字符串,0到“//”最后出现的位置
string filename = fullpath.Substring(index+1); //截取文件名
C#通过文件路径获取文件名小技巧
string fullPath = @"\WebSite1\Default.aspx";
string filename = System.IO.Path.GetFileName(fullPath);//文件名 “Default.aspx”
string extension = System.IO.Path.GetExtension(fullPath);//扩展名 “.aspx”
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fullPath);// 没有扩展名的文件名 “Default”
以上为个人经验,希望能给大家一个参考,也希望大家多多支持得得之家。
织梦狗教程
本文标题为:C#获取文件名和文件路径的两种实现方式


基础教程推荐
猜你喜欢
- Unity shader实现高斯模糊效果 2023-01-16
- C#中 Json 序列化去掉null值的方法 2022-11-18
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C# 解析XML和反序列化的示例 2023-04-14
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#中的Linq to JSON操作详解 2023-06-08
- Unity 如何获取鼠标停留位置下的物体 2023-04-10