none
how to Have program overcome Unauthorized Access Exception RRS feed

  • Question

  • Using visual 2010 on XP and windows 7.

    I tried to solve this before without luck and decided to give it another go.

    I wrote a small program to backup my user directory.

    When it attempts to copy the AppData folder I get an Unauthorized Access Exception. I can copy it fine using the windows file explorer.

    I need my program capable of changing/copying any file or folder on my network.

    An example of code doing this would be most helpful.

    Maybe my program can prompt me for the name and password it needs at the start.

    Thanks.



    • Edited by jack0987 Wednesday, July 24, 2019 1:03 PM
    Wednesday, July 24, 2019 12:45 PM

All replies

  • Hello,

    • Try running the app as admin or
    • Place the executable under the user's Document folder and try running the app.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 24, 2019 1:31 PM
    Moderator
  • Thanks for the reply.

    I tried running my program as administrator. It did not help.

    I tried adding a manifest file to my program with a highestAvailable setting. That did not help.

    I am running my program from my desktop. That does not help.

    Near as I can understand, I need to address two issues.

    First, I need to go through all my network computers and set them up so that my name and password are good everywhere for anything. (Tried once setting permissions and nearly locked myself out. I was not happy.)

    Second, I need to have my program do the same as first.

    Wednesday, July 24, 2019 2:02 PM
  • NuGet BaseConnectionLibrary for database connections.

    Is there an applied code sample in C# that will help me understand it's usage?

    Wednesday, July 24, 2019 3:25 PM
  • NuGet BaseConnectionLibrary for database connections.

    Is there an applied code sample in C# that will help me understand it's usage?

    The source code is at the following page.  The actual NuGet version is in VB.NET but works with C# too.

    https://github.com/karenpayneoregon/BaseConnectionsCS


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 24, 2019 6:05 PM
    Moderator
  • How will that help me?

    Will C# compile the VB source code?

    Wednesday, July 24, 2019 7:20 PM
  • No, that will not help you.  That is just an advertisement that is part of Karen's signature.  It's not part of her advice to you.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, July 24, 2019 9:08 PM
  • This problem is not solvable.  As a local administrator, you can copy any file on your own computer, but when you access another computer on your network, you are subject to the security restrictions of that computer.  The fact that you are MACHINE1\Administrator doesn't mean a darn thing to MACHINE2.  You're just an unknown guest.  There is no trust relationship.

    In a production environment, your machines would all be part of a domain.  The domain administrator has rights to access files across different systems.  If you're not running a domain, then your only option is to run the backup separately on the individual machines.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.


    • Edited by Tim Roberts Wednesday, July 24, 2019 9:12 PM
    Wednesday, July 24, 2019 9:12 PM
  • Thanks for your reply. For now, I will take it one step at a time.

    The Windows file explorer will copy the AppData directory whereas my little backup utility gets an access denied exception when run from my local machine.

    Please offer what you can think may be the problem.

    Thursday, July 25, 2019 12:05 AM
  • Hi jack0987,

    Thank you for posting here.

    Could you provide related code here? It will be beneficial for us to reproduce your problem.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, July 25, 2019 10:00 AM
    Moderator
  • This is the part of the code that does the work.

    The error I am getting is labeled "ERROR - PSD1" in my code. I only get the error for the AppData directory. All other files and directories will scan or backup without a problem. As we both know this is a hidden directory. In most cases I would not care about this directory except that some programs on some computers are placing important data there and I must have a backup.

    It may seem odd the I am running Windows XP and Windows 7 but a lot of applications I use do everything I need without my getting very expensive upgrades. In one case, I have one app running on Windows 98 that they want $5000 for the upgrade to Windows 10. I do have Windows 10 computers that are running apps where there were problems on Windows 7 where they ran before.

    void processSubDirectories (string directory, bool backup)
            {
                try
                {
                    subDirectories = Directory.GetDirectories(directory, "*.*", SearchOption.AllDirectories);
    
                    // An UnauthorizedAccessException exception will be thrown if we do not have
                    // discovery permission on a folder or file. It may or may not be acceptable 
                    // to ignore the exception and continue enumerating the remaining files and 
                    // folders. It is also possible (but unlikely) that a DirectoryNotFound exception 
                    // will be raised. This will happen if currentDir has been deleted by
                    // another application or thread after our call to Directory.Exists. The 
                    // choice of which exceptions to catch depends entirely on the specific task 
                    // you are intending to perform and also on how much you know with certainty 
                    // about the systems on which this code will run.
    
                    DirectoryInfo[] subDirectoryInfo = new DirectoryInfo[subDirectories.Length];
    
                    for (int x = 0; x < subDirectories.Length; x++)
                    {
                        try
                        {
                            subDirectoryInfo[x] = new DirectoryInfo(subDirectories[x]);
                            if (backup == false)
                            {
                                scanSubDirectoriesCount++;
                                scanSubDirectoriesCountLabel.Text = "Files:  " + scanFilesCount.ToString();
                            }
                            else
                            {
                                backupSubDirectoriesCount++;
                                backupSubDirectoriesCountLabel.Text = "Files:  " + scanFilesCount.ToString();
                            }
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            //Console.WriteLine(ex.Message);
                            textBox3.AppendText("ERROR - PSD3 - Unauthorized Access Exception - " + ex.Message + "\r\n");
                            continue;
                        }
                        catch (System.IO.DirectoryNotFoundException ex)
                        {
                            //Console.WriteLine(ex.Message);
                            textBox3.AppendText("ERROR - PSD4 - Directory NotFound Exception - " + ex.Message + "\r\n");
                            continue;
                        }
                    }  // End for
                }
                catch (UnauthorizedAccessException ex)
                {
                    //Console.WriteLine(ex.Message);
                    textBox3.AppendText("ERROR - PSD1 - Directory NotFound Exception - " + ex.Message + "\r\n");
                }
                catch (System.IO.DirectoryNotFoundException ex)
                {
                    //Console.WriteLine(ex.Message);
                    textBox3.AppendText("ERROR - PSD2 - Directory NotFound Exception - " + ex.Message + "\r\n");
                }
    
            }  // End void processSubDirectories (string directory, bool backup)


    • Edited by jack0987 Thursday, July 25, 2019 3:43 PM
    Thursday, July 25, 2019 3:28 PM
  • Hi jack0987, 

    Thanks for your feedback.

    I make a test based on your code and get the same exception.

    I have found a useful reference, and you can refer it.

    C# UnauthorizedAccessException to User Folder

    Besides, if I have any misunderstanding, you can describe it clearly about the AppData directory.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 29, 2019 9:35 AM
    Moderator