TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Path Exists Example

Use the Directory.Exists method from System.IO. Determine if a path exists.
Path exists. Does a specific path exist on the disk? Consider a program that requires a certain directory—it might store settings files, data or images.Path
With a special method, we can ensure a directory exists. This can make the program more reliable when it starts—the directory will always exist (in normal situations).
An example. We can almost never be sure that a directory is present. The user may delete it, or it might be lost due to some other interaction on the system.

Further: Your installation or upgrade process might have mistakenly removed it, for reasons beyond your control.

However: We can try to ensure a path exists, and create it if it doesn't, with a method similar to this one.

C# program that ensures path exists using System; using System.IO; class Program { public static void EnsurePathExists(string path) { // ... Set to folder path we must ensure exists. try { // ... If the directory doesn't exist, create it. if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } catch (Exception) { // Fail silently. } } static void Main() { // Test the method. EnsurePathExists(@"C:\programs\exampledir\"); Console.WriteLine("DONE"); } } Output DONE
Notes, above program. This method checks to see if the path exists. If the path does not exist, we attempt to create the location—we try to ensure the location exists.

Also: The code catches it own exceptions when it cannot do its job. We should use exceptions when code cannot do what it needs to do.

Exception

Directory: This is a static class in the IO namespace. You can call Exists and CreateDirectory on it.

Directory
Notes, redundant check. The Directory.CreateDirectory method will do nothing if the directory already exists. So we do not need to call Directory.Exists first.

Note: Thanks to Nikolai Wanner for showing that CreateDirectory can be used without an initial Directory.Exists check.

A summary. Here we ensure paths exist. It shows some examples of exception handling, and the Directory class in System.IO. The exception handling is not finished here.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf