C# - 如何提取路径文件名和扩展名? [英] C# - How to extract the file name and extension from a path?

查看:197
本文介绍了C# - 如何提取路径文件名和扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,说我有



 字符串PATH =C:\\Program Files\\Program\\ \\\File.exe; 



如何获得唯一的file.exe?我在想用的东西分割(见下文),但我试过不行......



这是我的代码。

 列表<串GT;特效=新的List<串GT;(); //用于检查指定的进程已经运行
的foreach(在Process.GetProcesses过程巴勒斯坦红新月会())
procs.Add(prcs.ProcessName); //每个进程添加到列表
的foreach(字符串升的File.ReadAllLines(MultiStart.txt))//获取的进程列表(完整路径)
如果(!l.StartsWith( //))//检查它是否是注释掉
如果[l.Split('\\')长(procs.Contains(l.Split('\\'!) - 1 ]))//检查进程已经运行
的Process.Start(L);



我可能只是作为一个菜鸟。 ._。


解决方案

System.IO 有不同的班级一起工作文件和目录。在他们之间,最有用的一个是路径里面有大量的静态辅助方法来与文件和文件夹:



<预类=郎-CS prettyprint-覆盖> Path.GetExtension(yourPath); //返回.EXE
Path.GetFileNameWithoutExtension(yourPath); //返回文件
Path.GetFileName(yourPath); //返回的file.exe


So, say I have

string path = "C:\\Program Files\\Program\\File.exe";

How do I get only "File.exe"? I was thinking something with split (see below), but what I tried doesn't work...

This is my code.

        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);

I'm probably just being a noob. ._.

解决方案

System.IO has different classes to work with files and directories. Between them, one of the most useful one is Path which has lots of static helper methods for working with files and folders:

Path.GetExtension(yourPath); // returns .exe
Path.GetFileNameWithoutExtension(yourPath); // returns File
Path.GetFileName(yourPath); // returns File.exe

这篇关于C# - 如何提取路径文件名和扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆