Directory.GetFiles - how do I get the file names only and not the complete path?
-
I have the following code which stores the filenames in an array. However, I want only the filenames to be stored. Surely there is an easy way to do this? I know I can replace the line, imageNames.Add(img); with imageNames.Add(img.Remove(0, 22)); which will remove the path (first 22 chars) leaving just the filename but this will be different when I upload to my server so don't really want to do it this way. Any ideas will greatly appreciated. foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } Many thanks Lorna
-
I have the following code which stores the filenames in an array. However, I want only the filenames to be stored. Surely there is an easy way to do this? I know I can replace the line, imageNames.Add(img); with imageNames.Add(img.Remove(0, 22)); which will remove the path (first 22 chars) leaving just the filename but this will be different when I upload to my server so don't really want to do it this way. Any ideas will greatly appreciated. foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } Many thanks Lorna
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
I have the following code which stores the filenames in an array. However, I want only the filenames to be stored. Surely there is an easy way to do this? I know I can replace the line, imageNames.Add(img); with imageNames.Add(img.Remove(0, 22)); which will remove the path (first 22 chars) leaving just the filename but this will be different when I upload to my server so don't really want to do it this way. Any ideas will greatly appreciated. foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } Many thanks Lorna
-
hello DirectoryInfo di = new DirectoryInfo(Path........); FileInfo[] rgFiles = di.GetFiles("*.jpg"); foreach (FileInfo fi in rgFiles) { // add u r needs }
Thanks but is that not the same as what I have and I get the complete path - here is another 2 lines of the code: ArrayList imageNames = new ArrayList(); string imagesDir = Server.MapPath("~/administration/uploaded/"); foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } thanks Lorna
-
I have the following code which stores the filenames in an array. However, I want only the filenames to be stored. Surely there is an easy way to do this? I know I can replace the line, imageNames.Add(img); with imageNames.Add(img.Remove(0, 22)); which will remove the path (first 22 chars) leaving just the filename but this will be different when I upload to my server so don't really want to do it this way. Any ideas will greatly appreciated. foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img); } Many thanks Lorna
Hi Lorna This should help you...
DirectoryInfo dir = new DirectoryInfo(@"C:\Test");
foreach (FileInfo file in dir.GetFiles())
{
Console.WriteLine(file.Name);
}oooo, the Jedi's will feel this one....
-
Hi Lorna This should help you...
DirectoryInfo dir = new DirectoryInfo(@"C:\Test");
foreach (FileInfo file in dir.GetFiles())
{
Console.WriteLine(file.Name);
}oooo, the Jedi's will feel this one....
Hurray it works! Thank you very much. For any other interested parties, I rewrote as: DirectoryInfo dir = new DirectoryInfo(@Server.MapPath("~/administration/uploaded/")); foreach (FileInfo file in dir.GetFiles()) { imageNames.Add(file.Name); } Which gave me an ArrayList of filenames only (no path).
-
Hurray it works! Thank you very much. For any other interested parties, I rewrote as: DirectoryInfo dir = new DirectoryInfo(@Server.MapPath("~/administration/uploaded/")); foreach (FileInfo file in dir.GetFiles()) { imageNames.Add(file.Name); } Which gave me an ArrayList of filenames only (no path).
Glad to help. :-D
oooo, the Jedi's will feel this one....