Error: An unhandled exception of type "System.UnauthorizedAccessException" occurred in the mscorlib.dll file

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

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

I read online and tried admin rights and created the app.manifest file:

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

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

I also tried this event, although I did not think that it would work, because it is a directory, not a file:

 File.SetAttributes(startupDirectory, FileAttributes.Normal); 

These are WinForms and I am on Windows 7, but also want it to be in the world for Windows 8+. How to do it?

Thanks in advance!

+6
source share
4 answers

Try

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

The first overload is the source. The second overload is the destination. I think the reason may be this

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

Try setting the permissions to "Full Control" for the .Net user, from where you read / save files.

To refuse access to the IIS server for a specific file, follow these 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 undoubtedly solve the problem associated with access,

UnauthorizedAccessException means one of three things:

  • The caller does not have the required permission.
  • path is a directory.
  • path specifies a read-only file.
+2
source

This exception is caused by a Windows error. He does not have a special error code "it absolutely does not make sense", he simply gives an error code "refused". Which .NET translates to a UnauthorizedAccessException.

The "no sense" problem is that you are trying to copy a directory using the file copy method. Directories are not files. To copy a directory, you must first create a new directory, and then copy all the files to the directory. .NET has a method for this; most C # programmers tend to think that this is the β€œwrong” namespace. This is Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory ().

But you will need to think a little about this problem, of course, it makes no sense to call the new directory "startup.exe". Probably, to copy a specific file from the launch directory, we cannot guess what it could be.

+3
source

Try the following:

  File.Copy(startupDirectory, "Startup.exe", true); File.SetAttributes("Startup.exe", FileAttributes.Normal); 
+1
source

You can run Visual Studio with administrator privileges. (Suppose Windows7, right-click the Visual Studio icon on the Start menu and select "Run as administrator")

+1
source

Source: https://habr.com/ru/post/979886/


All Articles