给定文件系统路径,有没有一种更短的方法来提取不带扩展名的文件名? [英] Given a filesystem path, is there a shorter way to extract the filename without its extension?

查看:100
本文介绍了给定文件系统路径,有没有一种更短的方法来提取不带扩展名的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF C#中编程.我有以下路径:

I program in WPF C#. I have e.g. the following path:

C:\Program Files\hello.txt

,我想从中提取 hello .

and I want to extract hello from it.

路径是从数据库检索的string.目前,我正在使用以下代码将路径按'\'拆分,然后再次按'.'拆分:

The path is a string retrieved from a database. Currently I'm using the following code to split the path by '\' and then split again by '.':

string path = "C:\\Program Files\\hello.txt";
string[] pathArr = path.Split('\\');
string[] fileArr = pathArr.Last().Split('.');
string fileName = fileArr.Last().ToString();

它可以工作,但是我认为应该有更短,更聪明的解决方案.有什么主意吗?

It works, but I believe there should be shorter and smarter solution to that. Any idea?

推荐答案

Path.GetFileName

Path.GetFileNameWithoutExtension

Path 类很棒.

这篇关于给定文件系统路径,有没有一种更短的方法来提取不带扩展名的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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