获取文件名 [英] Get File Names
本文介绍了获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码将文件夹的内容存储到数组中
I''m getting contents of the folder to a array by using following code
string[] aFolderContents = Directory.GetFiles(strSourcePath, "*.*", SearchOption.AllDirectories);
Then I loop through the array to get the each file names.
foreach (string strFiles in aFolderContents)
{
Console.WriteLine(strFiles);
}
但是当我这样的时候,
输出为C:\ FolderName \ FileName.txt
文件名之前的文件夹名称包含空格.
代码包装在前置"标签中.[/编辑]
But when I getting like this,
the output gives as C:\FolderName\FileName.txt
Before the file name folder name contains with spaces.
Code is Wrapped in "pre" tags[/Edited]
推荐答案
如果只想获取文件名,请使用以下代码:
If you want to get the File name only, use the following piece of code:
foreach (string strFiles in aFolderContents)
{
string FileName = System.IO.Path.GetFileName(strFiles);//Gives you file name only
Console.Write(FileName);
}
此处我使用DirectoryInfo,FileInfo提供代码
Here I am providing code using DirectoryInfo, FileInfo
DirectoryInfo dinfo = new DirectoryInfo("c:\\rajesh\\
");
FileInfo[] myfno= dinfo.GetFiles(*.* , SearchOption.AllDirectories);
foreach (FileInfo fno in myfno)
{
MessageBox.Show("FileName" + fno.Name);
MessageBox.Show("Filename with path " + fno.FullName);
MessageBox.Show("Only Path " + fno.DirectoryName);
MessageBox.Show("parent Dir" + fno.DirectoryName);
MessageBox.Show("File Extension" + fno.Extension);
}
谢谢..,
:rose :: rose:
Thanks..,
:rose::rose:
还可以使用FileInfo类来获取文件名.
You can also use FileInfo class to get the file names.
foreach (string strFiles in aFolderContents)
{
FileInfo fi=new FileInfo(strFiles)
Console.WriteLine(fi.Name);
}
这篇关于获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文