none
c# access to the path is denied ( when overwriting the files ) RRS feed

  • Question

  • hello all thank you for looking at my post. 

    basically i making a program that copy's folder and some other stuff once it copy's all the files if i push the button again it gives me this error. 

    and this is the code i have currently for the copy.

    public void sunfly_num()
            {
    
                if (sunfly378 == true)
                {
                    try
                    {
                        if (Karaoke_download_res.locationtoinstall == "")
                        {
                            pathtoinstall elfenliedpath = new pathtoinstall();
                            elfenliedpath.ShowDialog();
                            // now attemt to copy stuff over. 
                            if (!Directory.Exists(Karaoke_download_res.locationtoinstall + @"\SF378 August 2017"))
                            {
                                Directory.CreateDirectory(pathString);
                                DirectoryCopy(elfenmainpath + @"SF378 August 2017", Settings.Default.karaokecopy + @"\SF378 August 2017", true);
                                MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
                            }
                            else
                            {
                                DirectoryCopy(elfenmainpath + @"SF378 August 2017", Settings.Default.karaokecopy + @"\SF378 August 2017", true);
                                MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
                            }
    
                        }
                        else
                        {
    
    
                            if (!Directory.Exists(Karaoke_download_res.locationtoinstall + @"\SF378 August 2017"))
                            {
                                Directory.CreateDirectory(pathString);
                                DirectoryCopy(Settings.Default.mainpath + @"SF378 August 2017", Settings.Default.karaokecopy + @"\SF378 August 2017", true);
                                File.SetAttributes(Settings.Default.karaokecopy + @"\SF378 August 2017", FileAttributes.Normal);
                                MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
                            }
                            else
                            {
                                File.SetAttributes(Settings.Default.karaokecopy + @"\SF378 August 2017", FileAttributes.Normal);
                                DirectoryCopy(Settings.Default.mainpath + @"SF378 August 2017", Settings.Default.karaokecopy + @"\SF378 August 2017", true);
                                MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
                            }
    
                        }
                    }
                    catch (Exception elf)
                    {
                        MessageBox.Show(elf.Message, "Path Location Error Code: 665");
                    }
                }
    
            }

    im not sure why it wont.. replace the files once they are added like i said first time i hit add karaoke it copy over without a problem but if i hit add again i am greeted with the above error and i am not sure why its doing this or why this is showing up i am admin i have disabled uac and vs is in admin mode and i still get the errors so really out of ideas on how to fix this. 

    any help would be appreciated thanks in advance elfenliedtopfan5 

    Tuesday, September 26, 2017 4:52 PM

Answers

  • Hi,

    What happend if you write the File.SetAttributes() property after copying file code, like below in your else condition. Please look the bold part

    else
    {
    
    
        if (!Directory.Exists(Karaoke_download_res.locationtoinstall + @"\SF378 August 2017"))
        {
            Directory.CreateDirectory(pathString);
            DirectoryCopy(Settings.Default.mainpath + @"SF378 August 2017", Settings.Default.karaokecopy + @"\SF378 August 2017", true);
            File.SetAttributes(Settings.Default.karaokecopy + @"\SF378 August 2017", FileAttributes.Normal);
            MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
        }
        else
        {
            //File.SetAttributes(Settings.Default.karaokecopy + @"\SF378 August 2017", FileAttributes.Normal);
            DirectoryCopy(Settings.Default.mainpath + @"SF378 August 2017", Settings.Default.karaokecopy + @"\SF378 August 2017", true);
    	File.SetAttributes(Settings.Default.karaokecopy + @"\SF378 August 2017", FileAttributes.Normal);
            MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
        }
    
    }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Monday, October 2, 2017 11:31 AM
    Moderator

All replies

  • Just because you're admin does it mean you have rights on the folder or a file in a folder. So, I suggest you check permissions on the resource.  
    Tuesday, September 26, 2017 6:14 PM
  • Also did you close the program and start it again and everything works? It could be a sign that you are not releasing the resource properly, and when you try it again, the program has the resource locked while the program is running.
    Tuesday, September 26, 2017 6:24 PM
  • Hi,

    What happend if you write the File.SetAttributes() property after copying file code, like below in your else condition. Please look the bold part

    else
    {
    
    
        if (!Directory.Exists(Karaoke_download_res.locationtoinstall + @"\SF378 August 2017"))
        {
            Directory.CreateDirectory(pathString);
            DirectoryCopy(Settings.Default.mainpath + @"SF378 August 2017", Settings.Default.karaokecopy + @"\SF378 August 2017", true);
            File.SetAttributes(Settings.Default.karaokecopy + @"\SF378 August 2017", FileAttributes.Normal);
            MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
        }
        else
        {
            //File.SetAttributes(Settings.Default.karaokecopy + @"\SF378 August 2017", FileAttributes.Normal);
            DirectoryCopy(Settings.Default.mainpath + @"SF378 August 2017", Settings.Default.karaokecopy + @"\SF378 August 2017", true);
    	File.SetAttributes(Settings.Default.karaokecopy + @"\SF378 August 2017", FileAttributes.Normal);
            MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
        }
    
    }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Monday, October 2, 2017 11:31 AM
    Moderator
  • You didn't post the code for DirectoryCopy which is what is responsible for doing the actual copy. I suspect the problem is in there. Put a breakpoint in the method and then step through the code in the debugger. 

    Michael Taylor
    http://www.michaeltaylorp3.net

    Monday, October 2, 2017 1:53 PM
    Moderator
  • Hi elfenliedtopfan2,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.
    Your understanding and cooperation will be grateful.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, December 19, 2017 5:24 AM
    Moderator