{"id":5499,"date":"2017-10-22T09:35:41","date_gmt":"2017-10-22T09:35:41","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5499"},"modified":"2017-10-22T09:35:41","modified_gmt":"2017-10-22T09:35:41","slug":"ioexception-the-process-cannot-access-the-file-because-it-is-used-by-another-process","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/ioexception-the-process-cannot-access-the-file-because-it-is-used-by-another-process\/","title":{"rendered":"IOException &#8211; the process cannot access the file because it is used by another process"},"content":{"rendered":"<p>I&#8217;m using a logging library which is writing to a local log file and I also have a diagnostic tool which allows me to view the log files, but if I try to use <em>File.Open<\/em> I get the IOException, <\/p>\n<p>&#8220;the process cannot access the file because it is used by another process&#8221; <\/p>\n<p>this is obviously self-explanatory (and sadly not the first time I&#8217;ve had this and had to try and recall the solution).<\/p>\n<p>So to save me searching for it, here the solution which allows me to open a file that&#8217;s already opened for writing to by another process<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing (var stream = \r\n   File.Open(currentLogFile, \r\n      FileMode.Open, \r\n      FileAccess.Read, \r\n      FileShare.ReadWrite))\r\n{\r\n   \/\/ stream reading code\r\n}\r\n<\/pre>\n<p>The key to the File.Open line is the FileShare.ReadWrite. We&#8217;re interested in opening the file to read but we still need to specify the share flag(s) FileShare.ReadWrite.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m using a logging library which is writing to a local log file and I also have a diagnostic tool which allows me to view the log files, but if I try to use File.Open I get the IOException, &#8220;the process cannot access the file because it is used by another process&#8221; this is obviously [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49,3],"tags":[],"class_list":["post-5499","post","type-post","status-publish","format-standard","hentry","category-net","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5499"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5499\/revisions"}],"predecessor-version":[{"id":5545,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5499\/revisions\/5545"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}