目录
  • C#获取文件名和文件路径
    • 方法一
    • 方法二
  • C#通过文件路径获取文件名小技巧

    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);//名称

    方法二

    C#获取文件名和文件路径的两种实现方式

    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”

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

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。