Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My code here:
C#
string sapath1 = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Windows));
            File.SetAttributes(sapath1, FileAttributes.Normal);

            MD5 md5 = MD5.Create();
            StringBuilder sb = new StringBuilder();

            using (FileStream st = new FileStream(sapath1, FileMode.Open, FileAccess.Read, FileShare.Read))// error here
            {
                foreach (byte b in md5.ComputeHash(st))
                    sb.Append(b.ToString("x2").ToLower());
            }

            label1.Text =  sb.ToString();

I got error: System.UnauthorizedAccessException: 'Access to the path 'C:\' is denied.'

What I have tried:

Help me please. Is there any solution for this?
Posted
Updated 19-Jul-19 23:57pm
v3

Debug your project: you will find out that sapath1 does not hold the path to a file, but to a root drive (most probably c:\).
I think you forgot to reference an actual file; you cannot open a stream on a directory, let alone a root drive.
 
Share this answer
 
Comments
BillWoodruff 20-Jul-19 4:58am    
+5 You nailed it.
phil.o 20-Jul-19 6:10am    
Thanks :)
No. "Access denied" means exactly what it says - the user that your app is running under does not have access to the file or folder that you are trying to open.

Move the file to a more publicly accessible folder, or change the user. You can't "work around" this problem.
 
Share this answer
 
v2
Comments
BillWoodruff 20-Jul-19 6:02am    
The error here is the result of calling 'File.SetAttributes on a Drive or Directory.
Lê Hiển Vinh 20-Jul-19 7:04am    
How to get md5 of all files in folder c#?
OriginalGriff 20-Jul-19 7:10am    
Read the folder files list: Directory.GetFiles will do it.
Read each file and calculate the MD5. Be aware that GetFiles will return hidden files which you can't read and need to skip.
Quote:
Is there any solution for this?

Yes don't try to work directly on root of volume C. Windows restrict its usage because it is dangerous.
Work on a sub directory. C:\MyWorkingDir\
 
Share this answer
 
v2
Comments
BillWoodruff 20-Jul-19 4:59am    
My vote of #2: You can't set FileAttributes on any Directory: only on a File.
Patrice T 20-Jul-19 6:05am    
Hi Bill,
The error is on file creation, and I know that windows actively prevent it on system volume root for normal apps.
'You can't set FileAttributes on any Directory: only on a File.'
As a matter of fact, in past, I have used file attributes on directories to hide them.
Patrice T 20-Jul-19 6:07am    
Just seen it, but it look that the OP found my answer useful. :)
May be it is not so bad.
BillWoodruff 20-Jul-19 6:23am    
You are incorrect: calling 'File.SetAttribute on any Directory path will result in:

System.UnauthorizedAccessException

There is another mechanism for setting Directory Attributes: use the 'Attributes Property of the Directory's 'DirectoryInfo.
Patrice T 20-Jul-19 6:38am    
I was doing it in the savage world of MS-DOS :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900