2017-09-25 73 views
0

我想將文件夾從一個地方複製到另一個地方。如果文件和目錄存在,我想它來代替他們,否則整個File.Copy - 訪問路徑被拒絕

這份文件是我的代碼有:

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); 

它仍然給我同樣的錯誤。

+0

'// //?也許你的意思是'\\\'? – Gusman

+0

錯誤中的路徑看起來可疑。 – mayu

回答

1

您將方法Copy的源和目標參數連接爲常用字符串。我建議您:

  1. 使用Path.Combine()method連接它們;
  2. Combine()結果放入字符串變量中,然後將其作爲Copy()方法的參數,以便檢查您正在使用的方法。
+0

嗯...我從來沒有真正使用過path.combine()我會如何嘗試? –

+0

String name = Path.Combine(Karaoke_download_res.mainpath,「SF378 2017年8月」),[詳細閱讀本文](https://msdn.microsoft.com/en-us/library/fyy7a5kt(v = vs.110)的.aspx) –