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

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

问题描述

所以,说我有

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

我如何只获得File.exe"?我正在考虑拆分(见下文),但我尝试的方法不起作用......

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

这是我的代码.

        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 有不同的类来处理文件和目录.在它们之间,最有用的一个是 Path,它有许多用于处理文件和文件夹的静态辅助方法:

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
Path.GetDirectoryName(yourPath); // returns C:Program FilesProgram

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

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