System.UnauthorizedAccessException was unhandled

37,756

Solution 1

You may need to run your project in administrator mode if you want access to the root directory

You can do this by adding this to the app manifest

<requestedExecutionLevel level="requireAdministrator" uiAccess="true"/>

Solution 2

Double click on app.manifest file and if app.manifest not present Right click on your project, add -> New Item -> Application Manifest File then replace this line

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

with this

<requestedExecutionLevel level="requireAdministrator" uiAccess="true" />

It will run your application with administrator privileges.

Solution 3

I'm not sure exactly what all might throw an UnauthorizedAccessException.

One idea though... you're trying to create a file directly in the c: drive, which is sometimes disallowed by system policy. For the sake of troubleshooting, try creating the file somewhere else that you'd have access to, like My Documents.

Share:
37,756
somethingSomething
Author by

somethingSomething

myreddIT reddiWisdom is knowing that you are nothing, Love is knowing that you are everything and between the two your life moves. First time I was introduced to Linux was in 2007, it was Kubuntu and this person told me about the philosophy and I was told that you are not a real Linux user if you are not programming in Linux. I've been using Linux for everyday use since 2009. I started to use Linux for real in 2012. L'amour, la chaleur et la paix! ´ó ^ĸØ←←{}¬^²³»»»»»»»»»»»»»»»»»»»»»»»»»»»» P.S. Earth is for f * * K sake flat as a pancake, I kid you not The Best Flat Earth Documentary ...... If you want to know more goto ericdubay.com or find Eric Dubay on youtube(https://www.youtube.com/channel/UC0_CSKUIVVFlfocgezQEBDg/videos) Humans are in jail and everthing in our society is made to imprison us, but don't despair help is here and things are changing. We were seen as threats to the real world and illegal because we are 97% ape but what they didn't know is that we are 3% g o d l y watch this and see that this is not a human song, there are beings and higher entities everywhere:Grimes - Genesis. The world we live in is not a world, it's a highly advanced computer, the creators name is 42. There are many secrets hiding in movies. If you don't believe me then,↓↓↓↓ Watch this youtube song: (Grimes - We Appreciate Power) https://www.youtube.com/watch?v=gYG_4vJ4qNA When you listen to music, notice how the singer is talking to someone, it could be you he's talking to but there is also a being you should know about that all songs are referering to:↓↓↓↓↓↓↓ Watch and listen to this song; (Gwen Stefani - What You Waiting For) stupid boy, take a chance you st*** Check this website out, it's awsome: https://debgen.simplylinux.ch/

Updated on December 09, 2020

Comments

  • somethingSomething
    somethingSomething over 3 years

    I am getting a access denied exception. How can I fix this?

    Here is the exception:

    System.UnauthorizedAccessException was unhandled HResult=-2147024891 Message=Access to the path 'c:\message.txt' is denied.
    Source=mscorlib

    Here is the code:

        public static void WriteToFile(string s)
        {
            fs = new FileStream("c:\\message.txt",
            FileMode.Append, FileAccess.Write);
            sw = new StreamWriter(fs);
            sw.WriteLine(s);
            sw.Flush();
            sw.Close();
            fs.Close();
        }
    

    EDIT: It works if I run vs2012 as administrator, but is there a way or a reason to do it as normal user?

    And this works:

        public static void WriteToFile(string s)
        {
            fs = new FileStream(@"C:\Users\KristjanBEstur\Documents\message.txt",
            FileMode.Append, FileAccess.Write);
            sw = new StreamWriter(fs);
            sw.WriteLine(s);
            sw.Flush();
            sw.Close();
            fs.Close();
            File.Delete(@"C:\Users\KristjanBEstur\Documents\message.txt");
        }
    
  • somethingSomething
    somethingSomething over 10 years
    There was a new exception when I used your code:System.IO.FileNotFoundException was unhandled HResult=-2147024894 Message=Could not find file 'c:\message.txt'. Source=mscorlib FileName=c:\message.txt
  • Sudhakar Tillapudi
    Sudhakar Tillapudi over 10 years
    @somethingSomething: now it is sure that there is no file message.txt in c: drive , did you check it?
  • Sudhakar Tillapudi
    Sudhakar Tillapudi over 10 years
    @somethingSomething: i have edited my answer for checking file exists or not, try it and please let me know the result.
  • somethingSomething
    somethingSomething over 10 years
    I want the file to be created if it doesn't exist. How would you do that?
  • somethingSomething
    somethingSomething over 10 years
    The code I have works if I run vs2012 as administrator. This is the problem.
  • Sudhakar Tillapudi
    Sudhakar Tillapudi over 10 years
    so it is clear that the file needs admin previliges to open the file, you can goto file properties,select security, from there hange the file permission for other users to open.access the file