所以,说我有
string path = "C:\\Program Files\\Program\\File.exe";
Run Code Online (Sandbox Code Playgroud)
我如何只获得"File.exe"?我正在考虑拆分(见下文),但我尝试的东西不起作用......
这是我的代码.
List<string> procs = new List<string>(); //Used to check if designated process is already running
foreach (Process prcs in Process.GetProcesses())
procs.Add(prcs.ProcessName); //Add each process to the list
foreach (string l in File.ReadAllLines("MultiStart.txt")) //Get list of processes (full path)
if (!l.StartsWith("//")) //Check if it's commented out
if (!procs.Contains(l.Split('\\')[l.Split('\\').Length - 1])) //Check if process is already running
Process.Start(l);
Run Code Online (Sandbox Code Playgroud)
我可能只是一个菜鸟.._.
man*_*man 84
System.IO有不同的类来处理文件和目录.在它们之间,最有用的一个是Path有许多用于处理文件和文件夹的静态帮助方法:
Path.GetExtension(yourPath); // returns .exe
Path.GetFileNameWithoutExtension(yourPath); // returns File
Path.GetFileName(yourPath); // returns File.exe
Path.GetDirectoryName(yourPath); // returns C:\Program Files\Program
Run Code Online (Sandbox Code Playgroud)
使用最后一个字符搜索,您可以获得正确的结果.
string path = "C:\\Program Files\\Program\\fatih.gurdal.docx";
string fileName = path.Substring(path.LastIndexOf(((char)92))+ 1);
int index = fileName.LastIndexOf('.');
string onyName= fileName.Substring(0, index);
string fileExtension = fileName.Substring(index + 1);
Console.WriteLine("Full File Name: "+fileName);
Console.WriteLine("Full File Ony Name: "+onyName);
Console.WriteLine("Full File Extension: "+fileExtension);
Run Code Online (Sandbox Code Playgroud)
输出:
完整档案名称:fatih.gurdal.docx
完整档案Ony名称:fatih.gurdal
完整文件扩展名:docx
| 归档时间: |
|
| 查看次数: |
58480 次 |
| 最近记录: |