{"id":1769,"date":"2016-10-17T15:13:21","date_gmt":"2016-10-17T13:13:21","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=1769"},"modified":"2016-10-17T15:46:03","modified_gmt":"2016-10-17T13:46:03","slug":"cs-files-and-folders","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=1769","title":{"rendered":"cs Files and Directories"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Files\"><span class=\"toc_number toc_depth_1\">1<\/span> Files<\/a><ul><li><a href=\"#File_exists\"><span class=\"toc_number toc_depth_2\">1.1<\/span> File exists<\/a><\/li><li><a href=\"#Get_all_file_content_in_a_String\"><span class=\"toc_number toc_depth_2\">1.2<\/span> Get all file content in a String<\/a><\/li><li><a href=\"#Write_all_content_string_to_a_File\"><span class=\"toc_number toc_depth_2\">1.3<\/span> Write all content string to a File <\/a><\/li><\/ul><\/li><li><a href=\"#Directories\"><span class=\"toc_number toc_depth_1\">2<\/span> Directories<\/a><ul><li><a href=\"#Directory_exists\"><span class=\"toc_number toc_depth_2\">2.1<\/span> Directory exists<\/a><\/li><li><a href=\"#Create_Directory\"><span class=\"toc_number toc_depth_2\">2.2<\/span> Create Directory<\/a><\/li><li><a href=\"#Test_if_directory_is_readonly\"><span class=\"toc_number toc_depth_2\">2.3<\/span> Test if directory is readonly<\/a><\/li><li><a href=\"#Get_directory_name\"><span class=\"toc_number toc_depth_2\">2.4<\/span> Get directory name<\/a><\/li><\/ul><\/li><li><a href=\"#IDisposable_implementation_how_to_use_8220using8221_instruction\"><span class=\"toc_number toc_depth_1\">3<\/span> IDisposable implementation, how to use &#8220;using&#8221; instruction ?<\/a><\/li><\/ul><\/div>\n<h1><span id=\"Files\">Files<\/span><\/h1>\n<h2><span id=\"File_exists\">File exists<\/span><\/h2>\n<pre lang=\"csharp\">\r\nusing System.IO;\r\nbool exists = File.Exists(\"myfile.txt\");\r\n<\/pre>\n<h2><span id=\"Get_all_file_content_in_a_String\">Get all file content in a String<\/span><\/h2>\n<pre lang=\"csharp\">\r\nusing System.IO;\r\nString contents = File.ReadAllText(\"myfile.txt\");\r\n<\/pre>\n<h2><span id=\"Write_all_content_string_to_a_File\">Write all content string to a File <\/span><\/h2>\n<pre lang=\"csharp\">\r\nusing System.IO;\r\nFile.WriteAllText(\"myfile.txt\", \"hello world !!\", Encoding.UTF8);\r\n<\/pre>\n<h1><span id=\"Directories\">Directories<\/span><\/h1>\n<h2><span id=\"Directory_exists\">Directory exists<\/span><\/h2>\n<pre lang=\"csharp\">\r\nusing System.IO;\r\nbool exists = Directory.Exists(\"my\/dir\");\r\n<\/pre>\n<h2><span id=\"Create_Directory\">Create Directory<\/span><\/h2>\n<pre lang=\"chsarp\">\r\nusing System.IO;\r\nDirectoryInfo dirInfo = Directory.CreateDirectory(\"my\/dir\");\r\nbool exists = dirInfo.Exists;\r\n<\/pre>\n<h2><span id=\"Test_if_directory_is_readonly\">Test if directory is readonly<\/span><\/h2>\n<pre lang=\"chsarp\">\r\nusing System.IO;\r\nDirectoryInfo dirInfo = new DirectoryInfo(\"my\/dir\");\r\nbool isReadOnly = (dirInfo.Attributes & FileAttributes.ReadOnly) > 0;\r\n\r\n\/\/ Note, if you want to remove ReadOnly attributes:\r\n\/\/ dirInfo.Attributes = dirInfo .Attributes & ~FileAttributes.ReadOnly;\r\n<\/pre>\n<h2><span id=\"Get_directory_name\">Get directory name<\/span><\/h2>\n<pre lang=\"csharp\">\r\nusing System.IO;\r\nString dirname = Path.GetDirectoryName(\"my\/dir\");\r\n<\/pre>\n<h1><span id=\"IDisposable_implementation_how_to_use_8220using8221_instruction\">IDisposable implementation, how to use &#8220;using&#8221; instruction ?<\/span><\/h1>\n<p>Instead of writing something like this :<\/p>\n<pre lang=\"csharp\">\r\nStreamReader reader = new StreamReader(myfile.txt\");\r\ntry\r\n{\r\n    s = reader.ReadLine();\r\n}\r\nfinally\r\n{\r\n    reader.Close();\r\n}\r\n<\/pre>\n<p>you can write this :<\/p>\n<pre lang=\"csharp\">\r\nusing (StreamReader reader = new StreamReader(\"myfile.txt\"))\r\n{\r\n    s = reader.ReadLine();\r\n}\r\n\/\/ here is called \"reader.Dispose()\".\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 Files1.1 File exists1.2 Get all file content in a String1.3 Write all content string to a File 2 Directories2.1 Directory exists2.2 Create Directory2.3 Test if directory is readonly2.4 Get directory name3 IDisposable implementation, how to use &#8220;using&#8221; instruction ? Files File exists using System.IO; bool exists = File.Exists(&#8220;myfile.txt&#8221;); Get all file content in a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1767,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1769","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1769"}],"version-history":[{"count":7,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1769\/revisions"}],"predecessor-version":[{"id":1778,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1769\/revisions\/1778"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1767"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}