Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3392084
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:52:43+00:00 2026-05-18T03:52:43+00:00

Before writing to the file: using (StreamWriter outfile = new StreamWriter(filePath, false, System.Text.Encoding.UTF8)) {

  • 0

Before writing to the file:

using (StreamWriter outfile = new StreamWriter(filePath, false, System.Text.Encoding.UTF8))
{
    outfile.Write(content);            
}

I’m checking the file write accsses using this:

public bool CheckIfAccessToFileIsGranted(string filePath)
{
     try
     {
        new FileIOPermission(FileIOPermissionAccess.Write, filePath).Demand();
        return true;
     }
     catch (SecurityException)
     {
        return false;
     }
}

Despite the fact above function grants me the assess (returns true), I’m getting UnauthorizedAccessException while opening the stream. Why ?

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:52:44+00:00Added an answer on May 18, 2026 at 3:52 am

    The problem here is you’re confusing Code Access Security (CAS) with File System Permissions. The former is a restriction which applies only to CLR processes while the latter is an operating system level restriction. These two methods of security are independent of each other (although file system information can contribute to CAS policy).

    In this case the UnauthorizedAcessException represents a lack of permissions in the file system but you’re attempting to guard it with a CAS check which will not work.

    At the file system level (excluding CAS) the practice of attempting to verify an operation will or won’t succeed before hand is really a futile one. There is simply no way to do this properly because so many outside entities can change the file system between the check and attempt to access. It’s much more reliable to just try and access the file and catch the exception which results from the failed access.

    Here’s a link to a detailed blog post I wrote on this issue

    • http://blogs.msdn.com/b/jaredpar/archive/2009/12/10/the-file-system-is-unpredictable.aspx
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am writing an application for iphone to read a text file using NSData. NSFileHandle
PHP is automatically escaping my quotes before writing to a file using fwrite. I
Is it possible (and reasonable) to write Controller tests and classes before writing the
I'm writing a shell script that before running needs to check that system Perl
I already created a txt file using python with a few lines of text
* strong text *I am trying to save a multi-page TIF file using the
When I open a file using fopen($filename, 'w+') (that is, reading and writing), it
I'm reading in a text file using BinaryReader, then doing what I want with
I'm using the Console.SetOut method to write all my Console.Out.WriteLines to a file, and
I'm working on building the URLs for my REST API before I begin writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.