0

I am creating an application that reads a .txt file with a list of files to copy to different locations and to overwrite the files if they already exist, one location is:

C:\Program Files (x86)\Common Files\System\ado\

When doing this I am getting the following error:

System.UnauthorizedAccessException: 'Access to the path 'C:\Program Files (x86)\Common Files\System\ado\msado27.tlb' is denied.

I am running my application as an Administrator and I have updated my applications manifest file to invoke the application as an Administrator user. However I am not sure why I am still getting this error even running as an Administrator?

To copy the files I am using the .Net file operations:

File.Copy(sourceFile, destFile, true);

Full code section that deals with the file copying for your reference:


 void readFilesToInstall()
        {
            // 1 Read FileToInstall.txt Line By Line from C:\Temp2\BarsInstaller
            // 2 each line read copy to correct location
            // 3.  update progress bar 

            string[] lines = File.ReadAllLines(@"C:\Temp2\BarsInstaller\FilesToInstall.txt");
            string sourcePath = @"C:\Temp2\BarsInstaller\";
            progressBar.Invoke((Action)delegate
            {

                progressBar.Maximum = lines.Length;
                progressBar.Value = 0;
                lblPercent.Text = "0%";

            });

            foreach (string line in lines)
            {
                string[] col = line.Split('=');
                //process col[0], col[1], col[2]
                string fileName = col[0];
                //string sourceFullName = source + fileName;
                DirectoryInfo fileCopyToLocation = new DirectoryInfo(col[1]);
                string sourceFile = Path.Combine(sourcePath, fileName);
                string destFile = Path.Combine(col[1], fileName);
                darkLabel2.Invoke((Action)delegate
                {

                    darkLabel2.Text = destFile;

                    darkLabel2.Refresh();

                });

                copyFilesToInstall(sourceFile, destFile, fileCopyToLocation);
                progressBar.Invoke((Action)delegate
                {

                    progressBar.Value++;


                });


            }

        }

        void copyFilesToInstall(string sourceFile, string destFile, DirectoryInfo target)
        {
            if(Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }





            File.Copy(sourceFile, destFile, true);
        }


Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
dbjoe
  • 65
  • 9
  • Running an app as an administrator doesn't grant you permission to all files and directories. Check if you actually have write permission for that path and your admin account. – Jarek Danielak May 01 '20 at 15:11
  • Areyou running your program from Visual Studio or using .exe file? – Sina Hoseinkhani May 01 '20 at 15:12
  • OK I think I may have found the issue some files are owned by the TrustedInstaller group, any ideas how I can copy my files? – dbjoe May 01 '20 at 15:13
  • Here this questions and aswers may help: https://stackoverflow.com/questions/8821410/why-is-access-to-the-path-denied – Arthur Cam May 01 '20 at 15:14

0 Answers0