Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll : Access to the path ... is denied

Tags:

c#

.net

file-io

wpf

I am trying to write a file to a directory that exists and is created by me in G:\\ i.e. not a system directory or in root drive like this

File.WriteAllBytes(directoryPath.Replace("wav", "mp3"), asBytesArray);

But this is throwing me this exception:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll    
Additional information: Access to the path 'G:\Song' is denied.

How can i solve this issue? Please help!

This is coming while I am debugging from Visual Studio and i am not sure how to give administrative rights to the application which is being run as a debug

like image 947
Maven Avatar asked Dec 13 '13 21:12

Maven


People also ask

What is System UnauthorizedAccessException?

An UnauthorizedAccessException exception is thrown when the operating system denies access because of an I/O error or a security error. If you are attempting to access a file or registry key, make sure it is not read-only.


2 Answers

This usually means that either there is a folder named "G:\Song", so you can't replace a directory with a file, or that your windows user account doesn't have permission to write to that drive.

Right-click on the drive and look at security permissions to be sure you have permission. Also, make sure the drive is not readonly, as in the case of a write-protected SD card or flash drive.

like image 59
agent-j Avatar answered Oct 13 '22 21:10

agent-j


I had a similar issue and stumbled across this thread looking for an answer to another issue I am having.

But you issue sounds similar to one I had a few weeks ago, and the issue was that SYSTEM needed to also have Full Control over the folder.

May or may not be the issue, but mentioned incase it solves your or someone else's similar issue to what I experienced.

like image 42
timv Avatar answered Oct 13 '22 19:10

timv