我想將文件夾從一個地方複製到另一個地方。如果文件和目錄存在,我想它來代替他們,否則整個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");
}
}
}
當我調試它,並在文件上運行它複製它給了我一個錯誤說
對路徑的訪問被拒絕
我不知道爲什麼它給了我這個錯誤。我確定它以管理員身份運行。我甚至試過
File.SetAttributes(locationtoinstall, FileAttributes.Normal);
它仍然給我同樣的錯誤。
'// //?也許你的意思是'\\\'? – Gusman
錯誤中的路徑看起來可疑。 – mayu