{"id":95,"date":"2016-02-12T16:50:52","date_gmt":"2016-02-12T16:50:52","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=95"},"modified":"2022-02-17T06:24:15","modified_gmt":"2022-02-17T06:24:15","slug":"working-with-files","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/file-input-and-output\/working-with-files","title":{"rendered":"Working With Files"},"content":{"rendered":"<pre>\r\n<code>\r\nusing System.IO;\r\n<\/code><\/pre>\n<h4>\n\tDoes file exist?<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tif (File.Exists(&quot;c:\\\\temp.txt&quot;))\r\n<\/code><\/pre>\n<h4>\n\tDelete File<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\ttry\r\n\t{\r\n\t\tif (File.Exists(&quot;c:\\\\temp.txt&quot;))\r\n\t\t\tFile.Delete(&quot;c:\\\\temp1.txt&quot;);\r\n\t}\r\n\tcatch (IOException )\r\n\t{\r\n\t\tSystem.Diagnostics.Debug.WriteLine(&quot;This file is in use by another application - please close it first&quot;);\r\n\t}\r\n<\/code><\/pre>\n<p>\n\tor\n<\/p>\n<pre>\r\n<code>\r\n\ttry\r\n\t{\r\n\t\tif (File.Exists(&quot;c:\\\\temp.txt&quot;))\r\n\t\t{\r\n\t\t\t\/\/If file has read only attribute set clear it so that delete can occur\r\n\t\t\tif ((File.GetAttributes(&quot;c:\\\\temp1.txt&quot;) &amp; System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)\r\n\t\t\t\tFile.SetAttributes(&quot;c:\\\\temp1.txt&quot;, System.IO.FileAttributes.Normal);\r\n\r\n\t\t\tFile.Delete(&quot;c:\\\\temp1.txt&quot;);\r\n\t\t}\r\n\t}\r\n\tcatch (IOException )\r\n\t{\r\n\t\tSystem.Diagnostics.Debug.WriteLine(&quot;This file is in use by another application - please close it first&quot;);\r\n\t}\r\n<\/code><\/pre>\n<h4>\n\tGet Filename Without Path or Extension<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tsTemp = Path.GetFileNameWithoutExtension(MyFilename)\r\n<\/code><\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System.IO; Does file exist? if (File.Exists(&quot;c:\\\\temp.txt&quot;)) Delete File try { if (File.Exists(&quot;c:\\\\temp.txt&quot;)) File.Delete(&quot;c:\\\\temp1.txt&quot;); } catch (IOException ) { System.Diagnostics.Debug.WriteLine(&quot;This file is in use by another application &#8211; please close it first&quot;); } or try { if (File.Exists(&quot;c:\\\\temp.txt&quot;)) { \/\/If file has read only attribute set clear it so that delete can occur if ((File.GetAttributes(&quot;c:\\\\temp1.txt&quot;) [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-file-input-and-output"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/95\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}