I trying to write a program that navigates the local file system using a config file containing relevant filepaths. My question is this: What are the best practices to use when performing file I/O (this will be from the desktop app to a server and back) and file system navigation in C#?
I know how to google, and I have found several solutions, but I would like to know which of the various functions is most robust and flexible. As well, if anyone has any tips regarding exception handling for C# file I/O that would also be very helpful.
Using pathlib module With Python 3.4, you can also use the pathlib module. To iterate over files in a directory, use the Path. glob(pattern) function, which glob the given relative pattern in the specified directory and yield the matching files.
The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).
You don't need a separate library, use the classes in the System.IO
namespace like File
, FileInfo
, Directory
, DirectoryInfo
. A simple example:
var d = new DirectoryInfo(@"c:\");
foreach(FileInfo fi in d.GetFiles())
Console.WriteLine(fi.Name);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With