{"id":214,"date":"2012-12-05T13:45:56","date_gmt":"2012-12-05T18:45:56","guid":{"rendered":"http:\/\/binaryworld.net\/blogs\/?p=214"},"modified":"2013-08-13T14:43:17","modified_gmt":"2013-08-13T18:43:17","slug":"how-to-read-locked-file-in-vb-or-c","status":"publish","type":"post","link":"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/","title":{"rendered":"How to Read Locked file in VB or C#"},"content":{"rendered":"<p>Have you ever got this\u00a0error <em><span style=\"color: #ff0000;\">&#8220;File is being used by another process&#8221;<\/span><\/em><\/p>\n<p>Just try this create simple textfile on disk\u00a0(e.g. mydata.txt) and\u00a0open it with Excel&#8230; This way its locked (This is my trick to mimic locking of file)<\/p>\n<p>Now try to read file using below code while its opened by Exec.<\/p>\n<pre class=\"brush: vbnet; gutter: true\">Dim s As String = System.IO.File.ReadAllText(&quot;C:\\demo.csv&quot;)<\/pre>\n<p>Above code will fail if you try to read file which is locked by some one else&#8230; But there is one trick you can do to read file which is locked&#8230;.<\/p>\n<p>Use below code now<\/p>\n<p><strong>VB.net <\/strong><\/p>\n<pre class=\"brush: vbnet; gutter: true\">        Dim fs = New FileStream(&quot;C:\\demo.csv&quot;, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)\r\n        Using sr = New StreamReader(fs)\r\n            Console.Write(&quot;File content: &quot; &amp; sr.ReadToEnd)\r\n        End Using<\/pre>\n<p><strong>C#<\/strong><\/p>\n<pre class=\"brush: csharp; gutter: true\"> var fs = new FileStream(@&quot;C:\\demo.csv&quot;, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); \r\n        using (var sr = new StreamReader(fs)) {\r\n            Console.Write(&quot;File Content:&quot; + sr.ReadToEnd());\r\n        }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever got this\u00a0error &#8220;File is being used by another process&#8221; Just try this create simple textfile on disk\u00a0(e.g. mydata.txt) and\u00a0open it with Excel&#8230; This way its locked (This is my trick to mimic locking of file) Now try &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/\"> <span class=\"screen-reader-text\">How to Read Locked file in VB or C#<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49,66,65],"tags":[52,50],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>How to Read Locked file in VB or C# - BinaryWorld Blog<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"How to Read Locked file in VB or C# - BinaryWorld Blog\" \/>\r\n<meta property=\"og:description\" content=\"Have you ever got this\u00a0error &#8220;File is being used by another process&#8221; Just try this create simple textfile on disk\u00a0(e.g. mydata.txt) and\u00a0open it with Excel&#8230; This way its locked (This is my trick to mimic locking of file) Now try &hellip; How to Read Locked file in VB or C# Read More &raquo;\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/\" \/>\r\n<meta property=\"og:site_name\" content=\"BinaryWorld Blog\" \/>\r\n<meta property=\"article:published_time\" content=\"2012-12-05T18:45:56+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2013-08-13T18:43:17+00:00\" \/>\r\n<meta name=\"author\" content=\"Binary World\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Binary World\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/\",\"url\":\"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/\",\"name\":\"How to Read Locked file in VB or C# - BinaryWorld Blog\",\"isPartOf\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\"},\"datePublished\":\"2012-12-05T18:45:56+00:00\",\"dateModified\":\"2013-08-13T18:43:17+00:00\",\"author\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\"},\"breadcrumb\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/binaryworld.net\/blogs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Read Locked file in VB or C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\",\"url\":\"https:\/\/binaryworld.net\/blogs\/\",\"name\":\"BinaryWorld Blog\",\"description\":\"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\",\"name\":\"Binary World\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"caption\":\"Binary World\"},\"description\":\"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.\",\"url\":\"https:\/\/binaryworld.net\/blogs\/author\/admin\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Read Locked file in VB or C# - BinaryWorld Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/","og_locale":"en_US","og_type":"article","og_title":"How to Read Locked file in VB or C# - BinaryWorld Blog","og_description":"Have you ever got this\u00a0error &#8220;File is being used by another process&#8221; Just try this create simple textfile on disk\u00a0(e.g. mydata.txt) and\u00a0open it with Excel&#8230; This way its locked (This is my trick to mimic locking of file) Now try &hellip; How to Read Locked file in VB or C# Read More &raquo;","og_url":"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/","og_site_name":"BinaryWorld Blog","article_published_time":"2012-12-05T18:45:56+00:00","article_modified_time":"2013-08-13T18:43:17+00:00","author":"Binary World","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Binary World","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/","url":"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/","name":"How to Read Locked file in VB or C# - BinaryWorld Blog","isPartOf":{"@id":"https:\/\/binaryworld.net\/blogs\/#website"},"datePublished":"2012-12-05T18:45:56+00:00","dateModified":"2013-08-13T18:43:17+00:00","author":{"@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0"},"breadcrumb":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/binaryworld.net\/blogs\/how-to-read-locked-file-in-vb-or-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/binaryworld.net\/blogs\/"},{"@type":"ListItem","position":2,"name":"How to Read Locked file in VB or C#"}]},{"@type":"WebSite","@id":"https:\/\/binaryworld.net\/blogs\/#website","url":"https:\/\/binaryworld.net\/blogs\/","name":"BinaryWorld Blog","description":"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0","name":"Binary World","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","caption":"Binary World"},"description":"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.","url":"https:\/\/binaryworld.net\/blogs\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/214"}],"collection":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":0,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"wp:attachment":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}