What is the easiest way to iterate over a file folder in C #?

I am trying to write a program that moves the local file system using a configuration file containing the appropriate file paths. My question is this: what are the best practices for using file I / O (this will be from the desktop application to the server and vice versa) and navigating the file system in C #?

I know how google is, and I found several solutions, but I would like to know which of the functions is the most reliable and flexible. Also, if anyone has tips on handling exceptions for input / output of C # files, which will also be very useful.

+4
source share
4 answers

You do not need a separate library, use classes in the System.IO namespace, for example, File , FileInfo , Directory , DirectoryInfo . A simple example:

 var d = new DirectoryInfo(@"c:\"); foreach(FileInfo fi in d.GetFiles()) Console.WriteLine(fi.Name); 
+8
source

What different libraries are you talking about?

I would pretty much stick to System.IO.Directory and the like. It has everything you need.

Sort of:

 foreach (var file in System.IO.Directory.GetFiles(@"C:\Yourpath")) { // Do ya thang. } 
+3
source

Various classes can be used in the System.IO namespace, including File , FileInfo , Directory and DirectoryInfo .

As for practice ... as with any IO, make sure you close all threads you open. You may also need to use the Disposable object, so check out the using keyword.

+2
source

System.IO is all you need :)

Regarding exception handling. If we do not expect an exception, we will never catch it. Truly unexpected exceptions should be unhandled. [looks guilty] normal, the only exception is at the highest level and only for reporting purposes, for example

 // assuming console program, but every application Console, WinForm, // Wpf, WindowsService, WebService, WcfService has a similar entry point class Program { // assume log4net logging here, but could as easily be // Console.WriteLine, or hand rolled logger private static readonly ILog _log = LogManager.GetLogger (typeof (Program)); static void Main (string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; } private static void CurrentDomain_UnhandledException ( object sender, UnhandledExceptionEventArgs e) { _log. Fatal ( string.Format ( "Unhandled exception caught by " + "'CurrentDomain_UnhandledException'. Terminating program.", e.ExceptionObject); } } 

If you expect an exception, then one of the following is valid:

 // example of first option. this applies ONLY when there is a // well-defined negative path or recovery scenario public void SomeFunction () { try { string allText = System.IO.File.ReadAllText (); } // catch ONLY those exceptions you expect catch (System.ArgumentException e) { // ALWAYS log an error, expected or otherwise. _log.Warn ("Argument exception in SomeFunction", e); // if the use-case\control flow is recoverable, invoke // recovery logic, preferably out of try-catch scope } } 

or

 // example of second option. this applies ONLY when there is no // well defined negative path and we require additional information // on failure public void SomeFunction () { try { string allText = System.IO.File.ReadAllText (); } // catch ONLY those exceptions you expect catch (System.ArgumentException innerException) { // do whatever you need to do to identify this // problem area and provide additional context // like parameters or what have you. ALWAYS // provide inner exception throw new SomeCustomException ( "some new message with extra info.", maybeSomeAdditionalContext, innerException); // no requirement to log, assume caller will // handle appropriately } } 
+2
source

Source: https://habr.com/ru/post/1312216/


All Articles