{"id":201,"date":"2012-04-10T11:02:34","date_gmt":"2012-04-10T03:02:34","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=201"},"modified":"2018-09-03T21:51:53","modified_gmt":"2018-09-03T13:51:53","slug":"c-facilities-for-dealing-with-folders","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/c-facilities-for-dealing-with-folders\/","title":{"rendered":"C# facilities for dealing with folders"},"content":{"rendered":"<p>Especially for batch applications, the ability to deal with folders is important. This post documents the C# facilities that can give our applications the ability to work with folders.<\/p>\n<h3>Get the folder path which the executing binary is running in<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring exeFolderPath \r\n    = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;\r\n<\/pre>\n<h3>Check whether a folder exists<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Check if there is a folder named as techcoil in the same directory\r\n\/\/ as the running executable.\r\nif (System.IO.Directory.Exists(Path.Combine(exeFolderPath, &quot;techcoil&quot;)))\r\n{\r\n    Console.WriteLine(&quot;Folder exists.&quot;);\r\n}\r\nelse\r\n{\r\n    Console.WriteLine(&quot;Folder does not exists.&quot;);\r\n} \/\/ end if\r\n<\/pre>\n<h3>Create a folder<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Create a folder named as techcoil in the same folder as the \r\n\/\/ executing binary.\r\nDirectory.CreateDirectory(Path.Combine(exeFolderPath, &quot;techcoil&quot;));\r\n<\/pre>\n<h3>Rename a folder<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Rename the folder from techcoil to new_techcoil\r\nDirectory.Move(&quot;techcoil&quot;, &quot;new_techcoil&quot;);\r\n<\/pre>\n<h3>Delete a folder<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Delete the folder named as techcoil that exists in the same folder as the \r\n\/\/ executing binary, recursively. \r\nDirectory.Delete(Path.Combine(exeFolderPath, &quot;techcoil&quot;), true);\r\n<\/pre>\n<h3>Get some information about a folder<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nDirectoryInfo techcoilInfo = new DirectoryInfo(&quot;techcoil&quot;);\r\n\/\/ Get the time when the folder is first created\r\nConsole.WriteLine(techcoilInfo.CreationTime);\r\n\/\/ Get the full path of the directory\r\nConsole.WriteLine(techcoilInfo.FullName);\r\n\/\/ Get the last occurence when the folder is being accessed\r\nConsole.WriteLine(techcoilInfo.LastAccessTime);\r\n\/\/ Get the last occurence when the folder is written to\r\nConsole.WriteLine(techcoilInfo.LastWriteTime);\r\n\/\/ Get the full name of the parent folder of techcoil\r\nConsole.WriteLine(techcoilInfo.Parent.FullName);\r\n<\/pre>\n<h3>Get list of files in a folder<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Get a list of pathnames of files that are in the techcoil folder.\r\nIEnumerable&lt;string&gt; filesInTechcoil = Directory.EnumerateFiles(&quot;techcoil&quot;);\r\n\r\n\/\/ Print the list of pathnames \r\nforeach (string filePath in filesInTechcoil) \r\n{\r\n    Console.WriteLine(filePath);\r\n} \/\/ end foreach\r\n<\/pre>\n<h3>Get a list of folders in a folder<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Get the pathnames \r\nIEnumerable&lt;string&gt; foldersInTechcoil = Directory.EnumerateDirectories(&quot;techcoil&quot;);\r\nforeach (string folderPath in foldersInTechcoil)\r\n{\r\n    Console.WriteLine(folderPath);\r\n} \/\/ end foreach\r\n<\/pre>\n<h3>External references<\/h3>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.directory.aspx\" title=\"MSDN reference for System.IO.Directory\" target=\"_blank\">MSDN reference for System.IO.Directory<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/8s2fzb02.aspx\" title=\"MSDN reference for System.IO.DirectoryInfo\" target=\"_blank\">MSDN reference for System.IO.DirectoryInfo<\/a><\/li>\n<\/ul>\n<h3>Related posts<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-create-a-file-in-c\/\" title=\"How to create a file in C#\" target=\"_blank\">How to create a file in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-read-from-file-in-c\/\" title=\"How to read from file in C#\" target=\"_blank\">How to read from file in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-write-to-file-in-c\/\" title=\"How to write to file in C#\" target=\"_blank\">How to write to file in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-save-and-load-objects-to-and-from-file-in-c\/\" title=\"How to save and load objects to and from file in C#\" target=\"_blank\">How to save and load objects to and from file in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-monitor-a-folder-for-new-files-from-your-c-application\/\" title=\"How to monitor a folder for new files from your C# application\" target=\"_blank\">How to monitor a folder for new files from your C# application<\/a><\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-3f\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3f&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3f&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F201&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Especially for batch applications, the ability to deal with folders is important. This post documents the C# facilities that can give our applications the ability to work with folders.<\/p>\n","protected":false},"author":1,"featured_media":1189,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[20,112,111,109,110],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/C-Sharp-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-3f","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/201"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1189"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}