{"id":99,"date":"2011-04-03T10:44:00","date_gmt":"2011-04-03T15:44:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=99"},"modified":"2011-04-03T10:44:00","modified_gmt":"2011-04-03T15:44:00","slug":"using-fileiopermissions-with-streamwriter","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=99","title":{"rendered":"Using FileIOPermissions With StreamWriter"},"content":{"rendered":"<pre><span style=\"color: rgb(0, 153, 0);\">--The following exemplifies writing to a text file in a scenario where<br \/>--permissions need to be temporarily altered:<br \/><\/span><br \/><span style=\"color: rgb(0, 0, 153);\">string tempPath = @\"c:\/file.txt\";<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">if (File.Exists(tempPath))<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">{<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">FileIOPermission fio =<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">   new FileIOPermission(FileIOPermissionAccess.Write, tempPath);<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">fio.AddPathList(FileIOPermissionAccess.Write |<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">   FileIOPermissionAccess.Read, tempPath);<\/span><br \/><br \/><span style=\"color: rgb(0, 0, 153);\">fio.Demand();<\/span>  <span style=\"color: rgb(0, 153, 0);\">--use the .Demand method to obtain permission<\/span><br \/><br \/><span style=\"color: rgb(0, 0, 153);\">StreamReader reader = new StreamReader(tempPath);<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">string content = reader.ReadToEnd();<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">reader.Close();<\/span><br \/><br \/><span style=\"color: rgb(0, 0, 153);\">for (int j = 0; j &lt; stringToReplace.Length; j++)<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">{<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">  content = Regex.Replace(content, stringToReplace[j],<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">      replacementString, RegexOptions.IgnoreCase);<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">} <\/span><br \/><span style=\"color: rgb(0, 0, 153);\">StreamWriter writer = new StreamWriter(tempPath);<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">writer.Write(content); writer.Close();<\/span><br \/><span style=\"color: rgb(0, 0, 153);\">} <\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8211;The following exemplifies writing to a text file in a scenario where&#8211;permissions need to be temporarily altered:string tempPath = @&#8221;c:\/file.txt&#8221;;if (File.Exists(tempPath)){FileIOPermission fio = new FileIOPermission(FileIOPermissionAccess.Write, tempPath);fio.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, tempPath);fio.Demand(); &#8211;use the .Demand method to obtain permissionStreamReader reader = new StreamReader(tempPath);string content = reader.ReadToEnd();reader.Close();for (int j = 0; j &lt; stringToReplace.Length; j++){ content = Regex.Replace(content, stringToReplace[j], &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=99\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using FileIOPermissions With StreamWriter&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/99"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":0,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}