Questions tagged [system.io.directory]
90 questions
170
votes
22 answers
Multiple file-extensions searchPattern for System.IO.Directory.GetFiles
What is the syntax for setting multiple file-extensions as searchPattern on Directory.GetFiles()? For example filtering out files with .aspx and .ascx extensions.
// TODO: Set the string 'searchPattern' to only get files with
// the extension…

Seb Nilsson
- 26,200
- 30
- 103
- 130
21
votes
4 answers
c# - Function to replicate the folder structure in the file path
I need a simple function which will take a FileInfo and a destination_directory_name as input, get the file path from the fileinfo and replicate it in the destination_directory_name passed as the second parameter.
for ex. filepath is…

renegadeMind
- 4,073
- 5
- 29
- 37
11
votes
3 answers
Directory.CreateDirectory Latency Issue?
I'm trying to create a remote directory, and then write a file to it. Every great once in a while, the application fails with a System.IO.DirectoryNotFoundException while trying to write the file.
When I write the file, I'm using the returned…

majorpayne27
- 171
- 2
- 11
9
votes
2 answers
Performance problems counting the files with System.IO.Directory.GetFiles()
I use in a C# program
System.IO.Directory.GetFiles(dirname, "*.*", System.IO.SearchOption.AllDirectories).Length;
to count to number of files in a directory(and its subdirectories) on a share. I do this for many directories within a loop.
The…

Elmex
- 3,331
- 7
- 40
- 64
9
votes
2 answers
Deleting Locked Files & Folders
I am writing an application that updates some drivers. However the drivers are "in use" and can't be deleted unless I restart my computer.
So how can I write an application to delete these locked drivers without restarting the PC. IF Restarting MUST…

Cocoa Dev
- 9,361
- 31
- 109
- 177
7
votes
4 answers
Weird functionality in .NET's Directory.GetFiles() when search pattern contains 3 chars for extension
I recently bumped into a weird functionality from Microsoft:
Let's assume our folder c:\tmp123 contains 3 files -
1.txt
2.txtx
3.txtxt
a) Invocation of Directory.GetFiles(@"C:\tmp123", "*.txt") yields in 3 returned items.
b) Invocation of…

user1140419
- 111
- 1
- 6
7
votes
3 answers
Is there a reason why we would use Directory.GetFiles() over Directory.EnumerateFiles()?
I am not sure why we would use Directory.GetFiles for if Directory.EnumerateFiles would be able to do the same thing and you would be able to enumerate the list even before the whole list of directories found is returned.
What is the difference…

SamIAm
- 2,241
- 6
- 32
- 51
6
votes
3 answers
What happens with Directory.EnumerateFiles if directory content changes during iteration?
I've read discussions about difference between Directory.EnumerateFiles and Directory.GetFiles.
I understand that internally they both use
System.IO.FileSystemEnumerableFactory.CreateFileNameIterator()
The difference is that EnumerateFiles might use…

Harald Coppoolse
- 28,834
- 7
- 67
- 116
4
votes
2 answers
Get next file in the folder
When you open a picture in Windows Photo Viewer, you can navigate backward and forward between supported files using arrow keys (next photo / previous photo).
The question is: how to get path of the next file given path of the current file in the…

SharpAffair
- 5,558
- 13
- 78
- 158
4
votes
2 answers
c# Directory.GetDirectories excluding folders
I'm trying to iterate through a list of users folders in windows in "c:\Users" but exclude the microsoft built-in user folders, the below is the code segment I'm using to accomplish this feat but it's for some reason not working as intended.
private…

Clu
- 339
- 3
- 8
- 18
3
votes
2 answers
C# - Check if can access / read folder
Before closing my question, please see what I checked and doesn't work at the end of this question
Question
I've been trying all sorts of SO answers and all of them brings me a an exception, besides many of them sounding overcomplicated for…

Daniel Möller
- 84,878
- 18
- 192
- 214
3
votes
2 answers
.NET Directory.Move() vs File.Move()
We have an application that stores files in a temporary location until the user decides to finalize their transaction. The temp directory is unique for each transaction. Once they do finalize, the files are moved from the temp location to a final…

Eric
- 53
- 9
3
votes
5 answers
C# path to certain parent directory
Is there a short way to get certain parent directory path from current path.
I have a path like this:
"c:\\users\\userName\\documents\\visual studio 2013\\Projects\\SolutionName\\ProjectName\\bin\\Debug"
and i want to get the path to…

Alex Tertyshnyk
- 71
- 3
- 10
2
votes
1 answer
Deleting files after copying kills application
I found a very weird bug when using FileInfo.Delete() or Directory.Delete(path, true) after copying a folder.
Problem:
Copy a folder containing many files and a few subfolders recursively
Try to delete the original folder recursively
Exactly on the…

Daniel Möller
- 84,878
- 18
- 192
- 214
2
votes
2 answers
Directory.CreateDirectory fails with invalid character
I am facing issue that my path string passes check for Path.GetInvalidPathChars() but fails when trying to create directory.
static void Main(string[] args)
{
string str2 = @"C:\Temp\hjk&(*&ghj\config\";
foreach (var character in…

Farukh
- 2,173
- 2
- 23
- 38