파일을 복사하는 프로그램을 작성했습니다. 때로는 파일을 복사하는 동안 예외가 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);
}
}
}
아마도 누군가가 그것에 대해 말하고 싶다면 코드를 보여 주어야합니다. 그렇지 않으면 수정 구슬이있는 누군가가 화면을 볼 수있을 때까지 기다려야합니다. – Steve