Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
	}

But when I getting like this,
the output gives as C:\FolderName\FileName.txt
Before the file name folder name contains with spaces.

[Edited]Code is Wrapped in "pre" tags[/Edited]
Posted
Updated 28-Dec-10 18:29pm
v2

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);
}
 
Share this answer
 
Comments
Sanyon_d 29-Dec-10 0:54am    
Thanks
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);
 } 


Thanks..,

:rose::rose:
 
Share this answer
 
Comments
Sanyon_d 31-Dec-10 0:14am    
Thanks
You can also use FileInfo class to get the file names.

C#
foreach (string strFiles in aFolderContents)
    {
        FileInfo fi=new FileInfo(strFiles)
        Console.WriteLine(fi.Name);
    }
 
Share this answer
 
Comments
Sanyon_d 29-Dec-10 0:54am    
Thanks
Tarun.K.S 29-Dec-10 0:57am    
You're Welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900