C# 之 获取文件名及拓展名
1、用Path类的方法(最常用)
string fullPath = @"\WebSite\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”
string dir = System.IO.Path.GetDirectoryName(fullPath); //返回文件所在目录“\\WebSite”
2、用Substring截取
string aFirstName = fullPath.Substring(fullPath.LastIndexOf("\\") + 1, (fullPath.LastIndexOf(".") - fullPath.LastIndexOf("\\") - 1)); //不带拓展名的文件名“Default”
string aLastName = fullPath.Substring(fullPath.LastIndexOf(".") + 1, (fullPath.Length - fullPath.LastIndexOf(".") - 1)); //扩展名“aspx”
string strFileName = fullPath.Substring(fullPath.LastIndexOf("\\") + 1, fullPath.Length - 1 - fullPath.LastIndexOf("\\"));//带拓展名的文件名 “Default.aspx”
string strExtensionName = fullPath.Substring(fullPath.LastIndexOf("."), fullPath.Length - fullPath.LastIndexOf("."));//扩展名 “.aspx”
3、用openFileDialog.SafeFileName,取到该文件的所在目录路径
string path1 = System.IO.Path.GetDirectoryName(openFileDialog1.FileName) + @"\";
string path = Path.GetFileName("C:\My Document\path\image.jpg"); //只获取文件名image.jpg
4、进程相关
//获取当前进程的完整路径,包含文件名(进程名)。获取包含清单的已加载文件的路径或 UNC 位置。
string str0 = this.GetType().Assembly.Location;
//C:\Users\yiyi\AppData\Local\Temp\Temporary ASP.NET Files\web\bd33ba98\cbcc133a\App_Web_eidyl2kf.dll
//result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取新的 System.Diagnostics.Process 组件并将其与当前活动的进程关联
//获取关联进程主模块的完整路径,包含文件名(进程名)
string str1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer40.exe
//result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取或设置当前工作目录(即该进程从中启动的目录)的完全限定路径。
string str2 = System.Environment.CurrentDirectory;
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0
//result: X:\xxx\xxx (.exe文件所在的目录)
//获取当前 System.Threading.Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
string str3 = System.AppDomain.CurrentDomain.BaseDirectory;
//F:\Code\得利斯20150923\web\
//result: X:\xxx\xxx\ (.exe文件所在的目录+"\")
//获取或设置包含该应用程序的目录的名称。
string str4 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//F:\Code\得利斯20150923\web\
//result: X:\xxx\xxx\ (.exe文件所在的目录+"\")
//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str5 = System.Windows.Forms.Application.StartupPath;
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0
//result: X:\xxx\xxx (.exe文件所在的目录)
//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str6 = System.Windows.Forms.Application.ExecutablePath;
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer40.exe
//result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取应用程序的当前工作目录(不可靠)。
string str7 = System.IO.Directory.GetCurrentDirectory();
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0
//result: X:\xxx\xxx (.exe文件所在的目录)
C# 之 获取文件名及拓展名的更多相关文章
- C# 获取文件名及扩展名
C# 获取文件名及扩展名 string aFirstName = aFile.Substring(aFile.LastIndexOf("\\") + 1, (aFile.LastI ...
- C# 获取文件名及扩展名【转】
https://www.cnblogs.com/libushuang/p/5794976.html C# 获取文件名及扩展名 string aFirstName = aFile.Substring(a ...
- lua 获取文件名和扩展名
local str = "aaa.bbb.bbb.txt" --获取文件名 function getFileName(str) local idx = str:match(&quo ...
- asp获取文件名和扩展名的函数代码
<% '获取文件名(不含扩展名) Function getFilename(text)text = Left(text,inStrRev(text,".")-1)getFil ...
- FileUtils【获取SD卡根目录、读写文件、移动、复制、删除文件、获取文件名、后缀名操作类】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装了获取SD卡根目录路径.以及对文件读写.获取文件名等相关操作. 因为需要用到android.permission.READ_EX ...
- php获取文件名和后缀名
php获取文件名 1 function retrieve($url) 2 { 3 preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$url,$match); 4 re ...
- PHP 获取文件名和扩展名的方法
dirname(path) path: 代表你的文件路径,必须为绝对路径,可以使用__FILE__, 表示列出当前文件的绝对路径,包含文件名 函数会返回当前文件的上一级路径,也就是除了文件名称的路径 ...
- Shell 字符串处理、获取文件名和后缀名
http://blog.csdn.net/guojin08/article/details/38704823
- JavaScript 获取文件名,后缀名
function getBaseName(str) { var segs = str.split('.'); if(segs.length > 1) segs.pop(); return seg ...
随机推荐
- C# 中winform的一些属性设置
1 窗体的大小固定住,不能调整其大小 窗体FormBorderStyle 属性设置为 FixedSingle; MaximizeBox 属性设置为false; MinimizeBox 属性设置为 ...
- mysql远程连接出现 ERROR 2003 (HY000): Can't connect to MySQL server on IP
修改了如下两个位置,解决了这个问题: 修改/etc/mysql/my.cof配置文件:因为mysql默认只允许本机连接 修改远程连接用户权限:远程连接的用户被设置为不允许远程连接 首先修改/etc/m ...
- Bzoj 2006: [NOI2010]超级钢琴 堆,ST表
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2222 Solved: 1082[Submit][Statu ...
- Windows下Android SDK Manage下载速度缓慢的解决方法
在SDK Manager下Tools->Options打开了SDK Manager的Settings,选中“Force https://… sources to be fetched using ...
- 很好用的mybatis分页解决方案
分页如果写在SQL脚本中,将会大大影响我们后续数据库的迁移难度.mybatis的分页一般是自己实现一个mybatis的拦截器,然后根据某些特定的条件开启分页,对原有SQL进行改造. 正在我对mybat ...
- [iOS基础控件 - 5.2] 查看大图、缩放图片代码(UIScrollView制作)
原图: 900 x 1305 拖曳滚动: 缩放: 主要代码: // // ViewController.m // ImageZoom // // Created by ...
- [iOS基础控件 - 4.2] APP列表 字典转模型Model
A.使用字典加载数据的缺点 1.用户自行指定key,容易出错 2.存入.取出都需要key,容易混乱 B.模型 (MVC中的model) 1.字典与模型对比: (1)字典:存储数据,通过字符串类型的 ...
- Emgu CV 高斯建模
Codeprivate void button1_Click(object sender, EventArgs e) { Emgu.CV.Capture cap = new Capture(" ...
- Struts2中的session、request、respsonse获取方法
个人对于struts有一种复杂的心情,平心而论,struts2是个人最早接触到的的框架,在学校的时候就已经开始学习了,大四毕业设计,无疑用的还是struct,那时候SSH还是很流行的,后来出来实习,直 ...
- LocalDB 的创建与迁移
首先创建对应的对象 public class Movie { public int ID { get; set; } public string Title { get; set; } public ...