File.Copy - 拒绝访问路径

时间:2017-09-25 23:18:01

标签: c# file-copying

我想将文件夹从一个地方复制到另一个地方。如果文件和目录存在,我希望它替换它们,否则它会跨

复制文件

这是我的代码:

public void sunfly_num()
{
    if (sunfly378 == true)
    {
        try
        {
            if (Karaoke_download_res.locationtoinstall == "")
            {
                pathtoinstall elfenliedpath = new pathtoinstall();
                elfenliedpath.ShowDialog();
                if (File.Exists(locationtoinstall))
                {
                    if(Directory.Exists(Karaoke_download_res.locationtoinstall + "SF378 August 2017"))
                    {
                        File.SetAttributes(locationtoinstall, FileAttributes.Normal);
                        File.Copy(Karaoke_download_res.mainpath + "SF378 August 2017//", Karaoke_download_res.locationtoinstall + "//SF378 August 2017//", true);
                        MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
                    }
                    else
                    {
                      Directory.CreateDirectory(Karaoke_download_res.locationtoinstall + "SF378 August 2017");
                      File.SetAttributes(locationtoinstall, FileAttributes.Normal);           // Makes every read-only file into a RW file (in order to prevent "access denied" error)
                      File.Copy(Karaoke_download_res.mainpath + "SF378 August 2017//", Karaoke_download_res.locationtoinstall + "//SF378 August 2017//", true);
                      MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
                    }
                }
            }
            else
            {
                if (Directory.Exists(Karaoke_download_res.locationtoinstall))
                {
                    if (!Directory.Exists(pathString))
                    {
                        Directory.CreateDirectory(pathString);
                    }

                    foreach (var srcPath in Directory.GetFiles(Karaoke_download_res.mainpath + "//SF378 August 2017//"))
                    {
                        //Copy the file from sourcepath and place into mentioned target path, 
                        //Overwrite the file if same file is exist in target path
                        File.Copy(srcPath, srcPath.Replace(Karaoke_download_res.mainpath + "SF378 August 2017//", Karaoke_download_res.locationtoinstall + "\\SF378 August 2017"), true);
                    }
                    MessageBox.Show("Path Saved Karaoke Files Added To : " + Karaoke_download_res.locationtoinstall);
                    //  Directory.CreateDirectory(pathString);
                }
                else
                {
                    if (!Directory.Exists(pathString))
                    {
                        Directory.CreateDirectory(pathString);
                    }
                    foreach (var srcPath in Directory.GetFiles(Karaoke_download_res.mainpath + "SF378 August 2017"))
                    {
                        //Copy the file from sourcepath and place into mentioned target path, 
                        //Overwrite the file if same file is exist in target path
                        File.Copy(srcPath, srcPath.Replace(Karaoke_download_res.mainpath + "SF378 August 2017", Karaoke_download_res.locationtoinstall + "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");
        }
    }
}

当我调试它并在文件副本上运行时,它给我一个错误说

  

拒绝访问路径

image of error

而且我不确定为什么它会给我这个错误。我确保以管理员身份运行。我甚至试过

File.SetAttributes(locationtoinstall, FileAttributes.Normal);

它仍然给我同样的错误。

1 个答案:

答案 0 :(得分:1)

您将方法Copy的源和目标参数连接为常用字符串。我建议你:

  1. 使用Path.Combine() method连接它们;
  2. Combine()结果放入字符串变量中,然后将其用作Copy()方法的参数,以检查您的使用情况。