2014-11-09 5 views
0

파일을 복사하는 프로그램을 작성했습니다. 때로는 파일을 복사하는 동안 예외가 throw됩니다 - "경로에 대한 액세스가 거부되었습니다." (아마 다른 프로그램이이 파일을 사용하기 때문입니다).경로에 대한 액세스가 거부되었습니다. - C#

주의 사항 : I 관리자

을하지만 같은 프로그램을 실행! 수동으로 동일한 파일을 복사하면 작동합니다!

왜? 프로그램에 어떤 문제가 있습니까?

try 
       { 
        CopyClass.Copy(m_FilesSources[i].Path, m_FilesDestinations[i], true, m_FilesSources[i].Postfix); 
       } 
catch (Exception ex) 
       { 
        isAccessDenied = true; 
        tbLog.Text += " - " + ex.Message + "\n"; 
       } 


class CopyClass 
{ 
    public static bool Copy(string sourceDirPath, string destDirPath, bool copySubDirs, string postfix) 
    { 
      if (postfix == null) 
      { 
       FileCopy(sourceDirPath, destDirPath, copySubDirs); 
       return true; 
      } 
      DirectoryCopy(sourceDirPath, destDirPath, copySubDirs, postfix); 
      return true; 
    } 

    public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, string postfix) 
    { 
     // Get the subdirectories for the specified directory. 
     DirectoryInfo dir = new DirectoryInfo(sourceDirName); 
     DirectoryInfo[] dirs = dir.GetDirectories(); 

     // If the destination directory doesn't exist, create it. 
     if (!Directory.Exists(destDirName)) 
     { 
      Directory.CreateDirectory(destDirName); 
     } 

     // Get the files in the directory and copy them to the new location. 
     FileInfo[] files = dir.GetFiles(); 

     foreach (FileInfo file in files) 
      { 
       string temppath = System.IO.Path.Combine(destDirName, file.Name); 
       if (postfix == ".") 
       { 
        file.CopyTo(temppath, copySubDirs); 
       } 
       else if (file.Name.EndsWith(postfix)) 
       { 
        file.CopyTo(temppath, copySubDirs); 
       } 
      } 

     // If copying subdirectories, copy them and their contents to new location. 
     if (copySubDirs) 
     { 
      foreach (DirectoryInfo subdir in dirs) 
      { 
       string tempPath = System.IO.Path.Combine(destDirName, subdir.Name); 
       DirectoryCopy(subdir.FullName, tempPath, copySubDirs, postfix); 
      } 
     } 
    } 

    public static void FileCopy(string sourceFileName, string destDirName, bool overwrite) 
    { 
     string destFileName = destDirName + sourceFileName.Substring(sourceFileName.LastIndexOf('\\') + 1); 

     // If the destination directory doesn't exist, create it. 
     if (!Directory.Exists(destDirName)) 
     { 
      Directory.CreateDirectory(destDirName); 
     } 
     System.IO.File.Copy(sourceFileName, destFileName, overwrite); 
    } 
} 

}

+1

아마도 누군가가 그것에 대해 말하고 싶다면 코드를 보여 주어야합니다. 그렇지 않으면 수정 구슬이있는 누군가가 화면을 볼 수있을 때까지 기다려야합니다. – Steve

답변

1

문제는 내가 덮어 썼던 파일이 읽기 전용이라는 것입니다. 내가 수동으로 정상으로 대상 파일의 속성을 변경 한이 문제를 해결하기 위해 (읽기 전용되지 않음) :

if (File.Exists(destFileName)) 
      { 
       File.SetAttributes(destFileName, FileAttributes.Normal);   // Makes every read-only file into a RW file (in order to prevent "access denied" error) 
      } 

희망이 도움이됩니다.

0

당신이 응용 프로그램이 제한된 사용자로 실행되기 때문에 응용 프로그램에 액세스 할 수없는 파일을 읽기/쓰기/수정하려고 가능성이 높다.

관리자 권한으로 프로그램을 실행 해보십시오. (마우스 오른쪽 단추로 클릭 -> 관리자로 실행).

VS를 관리자로 실행할 수도 있습니다.이 경우 응용 프로그램이 관리자 권한으로 실행됩니다.

+0

관리자 권한으로 프로그램을 실행합니다. 다른 건 없니? –

+0

그러면 사용중인 코드가 표시되어야합니다. –

+0

코드 –

관련 문제