Error: An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Asked Answered
D

4

6

This is the part that crashes and gives me this error is when I try to copy a file to a certain location.

string startupDirectory = "C:\\Users\\Tyler\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
File.Copy(startupDirectory, "Startup.exe");

I have read online and tried administrator rights and have created an "app.manifest" file:

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

I confirmed that in the settings, the app manifest is set to this file, but it still gives me the same error.

I have also tried this event though I didn't think it would work because it is a directory not a file:

File.SetAttributes(startupDirectory, FileAttributes.Normal);

This is WinForms and I am on windows 7 but also want it to world for windows 8+. How do I do this?

Thanks in advance!

Dotation answered 18/12, 2014 at 21:11 Comment(2)
What is the name of source file name? In startupDirectory variable did you mentioned it?Genitourinary
A solved in the title doesn't help anyone. If you found a solution apart from the given answers you can answer your own question and mark it as accepted answer. That way others encountering a problem of the same kind will be helped too.Walling
V
2

Try

public static void Copy(string sourceFileName, string destFileName);

First overload is source 2nd overload is destination i think reason might be this

File.Copy("Startup.exe",startupDirectory);

Try setting the access permissions to "Full control" for the .Net user from where you are reading/saving the files.

For Access Denied Error in IIS server for particular file , please follow the below steps

1- Goto to C:\\Users\\Tyler\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup

2- Right click on your file -> Properties -> Pop Up of User properties appears -> click on Security tab-> click on Edit -> select Users-> tick on Allow Full Control -> Click Ok

This will surely solve the Access denied problem

An UnauthorizedAccessException means one of 3 things:

  • The caller does not have the required permission.
  • path is a directory.
  • path specified a read-only file.
Vinasse answered 18/12, 2014 at 21:21 Comment(7)
I am confused on the first option... The second and third did not work and for the forth one I cant find select IIS_IUSRSDotation
@Tyler13579 give full control to userVinasse
full control is already checked and greyed out so I cant change itDotation
the second option gave me this error An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The target file "C:\Users\Tyler\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" is a directory, not a file.Dotation
@Tyler13579 try with this msdn.microsoft.com/en-us/library/c6cfw35a(v=vs.110).aspxVinasse
try File.Copy("Startup.exe",startupDirectory,true);Vinasse
I am looking at the website though and will try thatDotation
H
3

This exception is triggered by a Windows error. It does not have a dedicated "this make absolutely no sense" error code, it just produces an "access denied" error code. Which .NET translates to a UnauthorizedAccessException.

The "makes no sense" problem here is that you are trying to copy a directory with a file copy method. Directories are not files. Copying a directory requires creating a new directory first, then copying all of the files in the directory. .NET has a method for that, most C# programmers tend to think it is the 'wrong' namespace. It is Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory().

But you'll need to think a bit more about this problem, it of course doesn't make sense to call the new directory "startup.exe". A probably meant to copy a specific file from the Startup directory, we can't guess what it might be.

Hoxsie answered 18/12, 2014 at 23:14 Comment(0)
V
2

Try

public static void Copy(string sourceFileName, string destFileName);

First overload is source 2nd overload is destination i think reason might be this

File.Copy("Startup.exe",startupDirectory);

Try setting the access permissions to "Full control" for the .Net user from where you are reading/saving the files.

For Access Denied Error in IIS server for particular file , please follow the below steps

1- Goto to C:\\Users\\Tyler\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup

2- Right click on your file -> Properties -> Pop Up of User properties appears -> click on Security tab-> click on Edit -> select Users-> tick on Allow Full Control -> Click Ok

This will surely solve the Access denied problem

An UnauthorizedAccessException means one of 3 things:

  • The caller does not have the required permission.
  • path is a directory.
  • path specified a read-only file.
Vinasse answered 18/12, 2014 at 21:21 Comment(7)
I am confused on the first option... The second and third did not work and for the forth one I cant find select IIS_IUSRSDotation
@Tyler13579 give full control to userVinasse
full control is already checked and greyed out so I cant change itDotation
the second option gave me this error An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The target file "C:\Users\Tyler\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" is a directory, not a file.Dotation
@Tyler13579 try with this msdn.microsoft.com/en-us/library/c6cfw35a(v=vs.110).aspxVinasse
try File.Copy("Startup.exe",startupDirectory,true);Vinasse
I am looking at the website though and will try thatDotation
P
1

Try this:

    File.Copy(startupDirectory, "Startup.exe", true);
    File.SetAttributes("Startup.exe", FileAttributes.Normal);
Principality answered 18/12, 2014 at 21:38 Comment(0)
M
1

You can run Visual Studio with Administrative Rights. (Assuming Windows7, Right-click on Visual Studio icon in the start menu and click "Run as Administrator")

Marbleize answered 13/7, 2017 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.