分享

C# 获取文件名及扩展名

 _src_ 2023-01-31 发布于四川
  1. string strFullPath = @"d:\test\mytest.txt";
  2. string strFilename = Path.GetFileName(strFullPath);//返回带扩展名的文件名
  3. Trace.WriteLine("返回带扩展名的文件名" + strFilename);
  4. string extension = Path.GetExtension(strFullPath);//扩展名
  5. Trace.WriteLine("扩展名" + extension);
  6. string strFileNameWithoutExtension = Path.GetFileNameWithoutExtension(strFullPath);// 没有扩展名的文件名 "default"
  7. Trace.WriteLine("没有扩展名的文件名" + strFileNameWithoutExtension);
  8. string dirPath = Path.GetDirectoryName(strFullPath); //返回文件所在目录
  9. Trace.WriteLine("返回文件所在目录" + dirPath);
  10. string strFullPath1 = Path.Combine(@"d:\test", "mytest.txt"); //返回 "d:\test\default.avi"
  11. Trace.WriteLine("路径合成" + strFullPath1);
  12. string strFullPath2 = Path.GetFullPath("新建文本文档.txt");//返回指定路径字符串的绝对路径
  13. Trace.WriteLine("返回指定路径字符串的绝对路径" + strFullPath2);

运行效果:

  1. 返回带扩展名的文件名mytest.txt
  2. 扩展名.txt
  3. 没有扩展名的文件名mytest
  4. 返回文件所在目录d:\test
  5. 路径合成d:\test\mytest.txt
  6. 返回指定路径字符串的绝对路径C:\Users\Administrator\Desktop\新建文件夹\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\新建文本文档.txt

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多