Home c# refused access along the way. How to fix?

refused access along the way. How to fix?

Author

Date

Category

Hello everyone.
I need to copy the entire folder with files and subfolders to another folder.
Who is interested in or suspiciously: I make a car-backup browser.
There are suspicions that this is due to the fact that the browser process is open. But it will not be very beautiful if I stupidly write Process.kill ("opera.exe") . I need to make cross-platform, if I never know in Firefox. The problem is that I do not know the names of the processes of all browsers. Khm Khm, closer to business.
Code:

directory.createdirectory ("other");
      String Username = Environment.userName;
      String start = "C: \\ Users \\";
      String [] dirs = {START + UserName + "\\ AppData \\ Local \\ Google \\ Chrome \\"
        START + UserName + "\\ APPDATA \\ Local \\ Yandex \\ YandexBrowser \\",
        START + UserName + "\\ AppData \\ Roaming \\ Opera Software \\ Opera Stable \\",
        START + UserName + "\\ AppData \\ Roaming \\ Mozilla \\ Firefox \\ Profiles \\"
        START + UserName + "\\ APPDATA \\ Roaming \\ Microsoft \\ Windows \\ Cookies \\"
        START + UserName + "\\ APPDATA \\ Local \\ Microsoft \\ Windows \\ inetcookies \\"};
For (uint16 i = 0; i & lt; dirs.length; i ++)
      {
        Backup (Dirs [i]);
      }
Static Private Void Backup (String Dir)
    {
      Try.
      {
        Directory.move (DIR, "Other \\"); // Error here
      }
      Catch (DirectoryNotFoundException) {}
    }

knocks:

system.io.ioException: "denied access on the path" C: \ users \ *** \ appdata \ Roaming \ Opera Software \ Opera Stable \ "."

Administrator rights on the program are issued in the manifest.


Answer 1, Authority 100%

  1. If you want to make backup – you need to copy, not move.
  2. if you want to move – then the best way is the question whether it is possible to close the program, and then if possible, then close. Move the program files while it is started explicitly.
  3. Another reason for which a similar error may occur when copying / movement is a refusal to access files in principle. That is, there is not enough rights to the user. Categories Program Files and APPDATA are categories with limited access and copy administrator rights. That is, it is necessary to check whether the program is launched on behalf of the admin and request a restart with the right right if not.

Copy directory with all subdirectory and files:

// Create an identical catalog tree
Foreach (String Dirpath in Directory.GetDirectories (Sourcepath, "*", searchoption.Altirectories))
  Directory.createDirectory (Dirpath.Replace (Sourcepath, DestinationPath));
// Copy all files. And overwrite (if such exist)
Foreach (String Newpath in Directory.getFiles (Sourcepath, "*. *", searchoption.AldRectories))
  File.copy (Newpath, Newpath.Replace (Sourcepath, DestinationPath), True);

Answer 2

Honestly, a strange backup with file movement … Try all the same to copy files. In this case, even if the file is locked with a process, it is usually not forbidden to read it.

In general, creating a backup request to close the application is more justified if the backup is created by the program side. Since the file may be in a non-intentable state at the time of copying it. And from such a backup may not be a sense. Although the browsers may not be so relevant

How to copy directories is described in the official material from MS https://docs.microsoft.com/ru-ru/dotnet/standard/io/how-to-copy-directories . I think you can find useful information on your problem

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions