2017-09-25 1 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

,691

거부

그리고 왜 내가이 오류를 내는지 모르겠습니다. 관리자로서의 실행을 확실히했습니다. 나도 해봤 어

File.SetAttributes(locationtoinstall, FileAttributes.Normal); 

그리고 아직도 나에게 같은 오류를 준다.

+0

'//'? 어쩌면 당신은'\\\'을 의미했을까요? – Gusman

+0

오류의 경로가 의심스러워 보입니다. – mayu

답변

1

Copy 메서드의 원본 및 대상 매개 변수를 공통 문자열로 연결합니다. 그들을 연결하는

  1. 사용 Path.Combine()method; 나는 당신을 추천합니다
  2. Copy() 메서드의 매개 변수로 사용하기 전에 Combine() 결과를 문자열 변수에 넣어서 무엇을 사용하고 있는지 확인하십시오.
+0

ummm ... 나는 결코 path.combine()을 사용하지 않았다. 어떻게 시도 하겠는가? –

+0

문자열 이름 = Path.Combine (Karaoke_download_res.mainpath, "SF378 August 2017"), [자세히보기] (https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110) .aspx) –

관련 문제