CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: UnauthorizedAccessException when checking MD5

  1. #1
    Join Date
    Nov 2009

    Exclamation UnauthorizedAccessException when checking MD5

    I'm new to these forums, so hello to all of you, and thanks in advance for your help.

    I feel like my issue is relatively basic, but I have been unable to uncover a solution...

    I am trying to compute the md5 for a bunch of files amongst other things. However, for files in the System32 directory I continually encounter the UnauthorizedAccessException. These files are marked Read / Read & Execute, and as I understand it, this is part of the problem. I am just not sure why... since all I am trying to do here is read.

    When I compute the md5 of these files using the md5 function in CygWin it works fine. So, in theory at least there's got to be a way to accomplish this...

    My current code on retrieving the md5 is below:
    StringBuilder sb = new StringBuilder();
    FileStream fs = new FileStream(fileName, FileMode.Open);
    MD5 md5 = new MD5CryptoServiceProvider();
    byte[] hash = md5.ComputeHash(fs);
    foreach (byte hex in hash)
    return sb.ToString();

    I get the exception right when I create the FileStream. Is there another way around this problem that will not result in the previously mentioned uae? Thanks again.

  2. #2
    Join Date
    Mar 2004
    Prague, Czech Republic, EU

    Re: UnauthorizedAccessException when checking MD5

    Not sufficient privilegies? I'd bet this is the reason. Check if the account under which you run your code has access to the folder System32.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Nov 2009

    Re: UnauthorizedAccessException when checking MD5

    This was the first thing I checked, and I do indeed have access to the directory. More specifically, when I got to [properties >> security] of each file, System/Users/Administrators all have "Read" and "Read and Execute" access to the files in this directory.

    In my code, all I'm doing (unless I'm mistaken) is reading in a file byte by byte and computing the aggregate hash. Therefore, I'm uncertain as to why I am getting the permission error? Is there something I'm doing wrong in my code?

  4. #4
    Join Date
    Nov 2009

    Re: UnauthorizedAccessException when checking MD5

    Ah ha!

    I figured out the issue, it would appear that when initializing the FileStream object, the FileMode.Open command seemed to want to default to assuming it needed RW privs unless specified.

    I specified that I only needed read perms by slightly modifying the FileStream object:

    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

    The key there is the FileAccess.Read at the end. Re-ran it and all was well. Hope this helps others . Thanks for the suggestion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)