C#获取文件名和文件路径

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

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

C#获取文件名和文件路径

Welcome_Back   2022-09-28 我要评论

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”

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

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

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