从文件名获取扩展名 [英] get extension from file name
本文介绍了从文件名获取扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以从文件名获得扩展名
我的档案是
D:\ Sam \DSC0001
现在我要检查DSC0001扩展名和文件是否存在
请帮帮我
: - )
解决方案
你试过这个: system.io .path.getextension [ ^ ]
尝试:
string path = @ D:\ Sam \DSC0001;
int index = path.LastIndexOf(' \\'跨度>);
if (index > 0 )
{
string dir = path.Substring( 0 ,index );
string file = path.Substring(index + 1 );
string [] files = Directory.GetFiles(dir,file + *跨度>)。
if (files.Length > 0 )
{
// 文件存在。
foreach (字符串 f 在文件中)
{
Console.WriteLine(Path.GetExtension(f));
}
}
}
Dim path_ as string = D:\DSC0001
Dim fileExtension 作为 字符串 = Path.GetExtension(path_)
如果 不 File.Exists(file_path& 。& fileExtension)然后
msgbox( 找不到文件)
结束 如果
can i get extension from file name
my file is
D:\Sam\DSC0001
now i want to check DSC0001 extension and file exist or not
please help me
:-)
解决方案
Have you tried this : system.io.path.getextension[^]
Try:
string path = @"D:\Sam\DSC0001"; int index = path.LastIndexOf('\\'); if (index > 0) { string dir = path.Substring(0, index); string file = path.Substring(index + 1); string[] files = Directory.GetFiles(dir, file + ".*"); if (files.Length > 0) { // File exists. foreach (string f in files) { Console.WriteLine(Path.GetExtension(f)); } } }
Dim path_ as string = "D:\DSC0001" Dim fileExtension As String = Path.GetExtension(path_ ) If Not File.Exists(file_path & "." & fileExtension) Then msgbox("File Not Found") End If
这篇关于从文件名获取扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文