如何修剪没有扩展名的文件名 [英] How to trim a file name without extension
本文介绍了如何修剪没有扩展名的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过使用
Path.GetFileNameWithoutExtension(filename); $ p $来获取没有扩展名和路径的文件名p>
但我想要没有延期的整条路径。
我希望以下输出为字符串
C:\ Users \Admin \Desktop \ MyFileName
我希望在没有延期的情况下发生这种情况。
怎么做
解决方案
最简单的方法?
string path = @ D:\ Temp \ MyFile.txt;
string fullPathWithoutExtension = path.Substring( 0 ,path.LastIndexOf(' 。'));
文件名可能包含多个。因此上述代码在所有情况下都不安全。
尝试下面的代码。
< pre lang =c#> FileInfo fi = new FileInfo( @ C:\XMLFile1.txt跨度>);
Console.WriteLine(fi.DirectoryName + @ \ + fi.Name。替换(fi.Extension, ));
Console.Read();
I can get the filename without extension and path by using
Path.GetFileNameWithoutExtension(filename);
But I want whole path without extension.
I want the following output as string
C:\Users\Admin\Desktop\MyFileName
I want this to happen without extension.
How to do this
解决方案
Easiest way?
string path = @"D:\Temp\MyFile.txt"; string fullPathWithoutExtension = path.Substring(0, path.LastIndexOf('.'));
File name may contain multiple . so the above mentioned code is not safe at all the scenario.
Try below code.
FileInfo fi = new FileInfo(@"C:\XMLFile1.txt"); Console.WriteLine(fi.DirectoryName +@"\" +fi.Name.Replace(fi.Extension,"")); Console.Read();
这篇关于如何修剪没有扩展名的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文