1

The button1_Click Event (Copy PDF's to new location) works perfectly when the button is clicked and the code is executed the 1st time;

however, upon clicking the button a second time (with same text box entry), it throws the following error:

System.UnauthorizedAuthorizedAccessException: Access to the path "\share\drive....

Obviously, I don't want this to be able to execute twice during a session, given the same text box entry. Before I tackle that, I would like to fix this exception error. Am I erroneously leaving the path open?

Code updated to show solution:

public static string Case_No;

namespace CEB_Process
{
    public partial class Form1 : Form 
    {

    public Form1()
    {
        InitializeComponent();
    }



    //===============================
    // TEXT BOX ENTRY
    //===============================
    private void textBox1_TextChanged(object sender, EventArgs e)
    { 
        Form1.Case_No = textBox1.Text;
    }


    //==============================
    // CHECK if Direcotry Exists
    //==============================

    public void CreateIfMissing(string path)
    {
        if (!Directory.Exists(path)) 
        {
            DirectoryInfo di = Directory.CreateDirectory(path);
            //Added
            var permissions = new DirectoryInfo(path);
            permissions.Attributes &= ~FileAttributes.ReadOnly; 
            MessageBox.Show("The directory was created successfully");
        }
    }


    //=================================
    // MOVE Violation PDF's Button Click
    //==================================
    private void button1_Click(object sender, EventArgs e)
    {

        //Declare Source path directory from text box entry
        string sourcePath = string.Format(@"\\share\drive\etc{0}", Case_No);
        string targetPath = string.Format(@"\\share\drive\etc{0}", Case_No);        

            try
            {
                //Call Method to Check/Create Path
                CreateIfMissing(targetPath);

                //Get TRAKiT Violation PDF's from source
                foreach (var sourceFilePath in Directory.GetFiles(sourcePath, "*.pdf"))
                {
                    string fileName = Path.GetFileName(sourceFilePath);
                    string destinationFilePath = Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(sourceFilePath, destinationFilePath, true);
                    File.SetAttributes(destinationFilePath, FileAttributes.Normal);
                }//End For Each Loop
                MessageBox.Show("Files Copied Successfully!");
            }//end try
            catch (Exception x)
            {
                MessageBox.Show("The process failed", x.ToString());
            }


    }//End Button Module

}//End Namespace
}//End Class
Tennis
  • 137
  • 12
  • 1
    not related to your problem: `finally { } //Used in conjunction with Try to Release resources `: an empty finally block will do.... nothing! (no release of any resource, not that you need here... just delete this finally) – Gian Paolo Dec 14 '16 at 14:26
  • I assumed it was freeing up resources. Thank you. – Tennis Dec 14 '16 at 15:28

2 Answers2

0

I also had the problem I added the following line of code before and after a Copy / Delete.

File.Copy(file, dest, true);
File.SetAttributes(dest, FileAttributes.Normal);

(PS: Taken from Why is access to the path denied?)

Community
  • 1
  • 1
Mohit Vashistha
  • 1,824
  • 3
  • 22
  • 49
  • Worked like a charm in conjunction with: `permissions.Attributes &= ~FileAttributes.ReadOnly;` set during the directory creation method. Thank you! – Tennis Dec 14 '16 at 15:10
0

I suppose you're using File.Copy without overwriting the selected file. That means the file is getting copied and it's temporarily locked by OS, and then it's not open to modifications (read-only). This is the reason of your UnauthorizedAccessException.

Check if you can accessthe file first.

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
  • After making the changes above, it appears the files are now open to modification (overwrite). – Tennis Dec 14 '16 at 15:36