Directory Search - c#

This is a simple question and I am sure you C# Pros know it.
If you want to grab the contents of a directory on a hard drive (local or otherwise) in a C Sharp program, how do you go about it?

Call Directory.GetFiles.

If you mean get a directory then it is very simple. The following code will give you a list of all the directories in a given directory and then list all the files in the directory:
string dir = #"C:\Users\Joe\Music";
DirectoryInfo dirInfo = new DirectoryInfo(dir);
FileInfo[] files = dirInfo.GetFiles();
DirectoryInfo[] directories = dirInfo.GetDirectories();
foreach (DirectoryInfo directory in directories)
Console.WriteLine(directory.Name);
foreach (FileInfo file in files)
Console.WriteLine (file.Name);

Related

How to get the directory without sub directory in c#

I would like to ask is there any way to get all the folder/directory name in a path:
My problem is:
I would like to get several folders in Local Disk C: but I don't want to use the following code to search all the folder in C: which causes the program stuck.
foreach (string dirPath in Directory.GetDirectories(source, "*", SearchOption.AllDirectories))
{
...
}
Try this:
using System.IO;
var directories = Directory.GetDirectories("c:\\");
This should give you all the directories at the root level of the C drive.
Try this:
string[] dirs = Directory.GetDirectories(#"c:\", "*", SearchOption.TopDirectoryOnly);
This should give you just top directory only.

How do I access Images folder in the controller in the current development server?

My development server is called the WebApp which has the following folders as shown in the image. I want to write a method/function to access the 'Images' folder inside the controller while the application is running
You need to get the path to the folder and then can access each file in the folder. Try this:
string path = Server.MapPath("~/Images/");
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileInfo[] files = dirInfo.GetFiles();
foreach(FileInfo file in files)
{
string fileName = file.Name;
}

How to rename a fileInfo by adding \\?\ prefix to avoid System.IO.PathTooLongException?

I'm looking for a way to avoid System.IO.PathTooLongException. I read that I could put \\?\ in front of a long path.
The problem is that I have FileInfo objects and I ask me how to add the \\?\ path prefix to my FileInfo paths without writting useless code.
Note that I have folders with several thousand of files to copy and I don't want that my application takes a lot of useless time during his process.
Thanks.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
FileInfo[] files = dir.GetFiles(Constants.ALL_FILES, SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
file.CopyTo(destAbsolutePath);
}

Copy Specific file type from subfolders

I am new to C# and I am trying to build a console application to copy a specific file type .e.g *.txt that is nested within subfolders to be copied to another directory.
The directory looks something like this
C:\V1.1\Folder_*\Folder\Folder\Folder\Filetype.txt
* meaning today's date
How would I get a list of files matching that pattern?
Try something like this.
var path = String.Format(#"C:\V1.1\Folder_{0}\Folder\Folder\Folder",
DateTime.ToString("dd-MM-yyyy"))
DirectoryInfo di = new DirectoryInfo(path);
var files=di.GetFiles("*.txt", SearchOption.AllDirectories)
You can check this site out for more information.
Thanks for all the help. I ended up doing this:
foreach (var file in Directory.GetFiles(sourceDir, "*.txt", SearchOption.AllDirectories))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);

Delete specific files in directory using C#

There are many .bmp files present in my C:\TEMP directory.
I am using following code to delete all .bmp file in my C:\TEMP directory but somehow its not working as I expect. Can anyone help me in this?
string[] filePaths = Directory.GetFiles(#"c:\TEMP\");
foreach (string filePath in filePaths)
{
if (filePath.Contains(".bmp"))
File.Delete(filePath);
}
I have already checked that .bmp file and the directory has no read only attribute
For starters, GetFiles has an overload which takes a search pattern http://msdn.microsoft.com/en-us/library/wz42302f.aspx so you can do:
Directory.GetFiles(#"C:\TEMP\", "*.bmp");
Edit: For the case of deleting all .bmp files in TEMP:
string[] filePaths = Directory.GetFiles(#"c:\TEMP\", "*.bmp");
foreach (string filePath in filePaths)
{
File.Delete(filePath);
}
This deletes all .bmp files in the folder but does not access subfolders.
Should also use .EndsWith instead of .Contains
You Could write below code in fast way:
string[] t = Directory.GetFiles(Environment.CurrentDirectory, "*.pdf");
Array.ForEach(t, File.Delete);
or For Text Files:
string[] t = Directory.GetFiles(Environment.CurrentDirectory, "*.txt");
Array.ForEach(t, File.Delete);
So, You could write code for all extension and all directories.

Categories

Resources