Directory.Move(oldDir, newDir)
을 사용하여 디렉토리의 이름을 바꿉니다. 때마다 "IOException
"경로에 액세스 'oldDir'거부되었습니다. 그러나 탐색기에서 디렉터리를 마우스 오른쪽 단추로 클릭하면 아무 문제없이 이름을 바꿀 수 있습니다.디렉토리의 이름을 바꾸는 방법
내 질문은 :Directory.Move
을 사용하지 않고 디렉토리의 이름을 바꾸는 다른 방법이 있습니까?
명령 셸 (Process.Start()
)을 사용하여 생각했지만이 방법을 사용하는 것이 좋습니다.
더 자세한 사항이 프로그램이 계속 실행되고
, 나는 예외를 얻을 Windows 탐색기에서 수동으로 이름을 바꿀 수 있습니다 - 내 커서가있는 중단 점에 일시 정지 된 상태 (오른쪽 클릭> 이름 바꾸기를) 블록을 잡아라. 또한 Directory.Move
에 중단 점을 설정하고 성공적으로 탐색기에서 디렉터리 이름을 바꾼 다음 Directory.Move
을 단계적으로 지나서 catch (IOException)
에 다시 기록했습니다. 당신은 그냥 방법을 입력 볼 수 있듯이
여기에 내 코드
public bool Copy()
{
string destPathRelease = ThisUser.DestPath + "\\Release";
if (Directory.Exists(destPathRelease))
{
try
{
string newPath = ThisUser.DestPath + '\\' + (string.IsNullOrEmpty(currBuildLabel) ? ("Release" + '_' + DateTime.Now.ToString("yyyyMMdd_HHmmss")) : currBranchName) + '.' + currBuildLabel;
Directory.Move(destPathRelease, newPath);
}
catch (IOException)
{
// Breakpoint
}
}
}
입니다. 이전에 내 프로그램에서이 디렉토리를 만진 적이 없었습니다.
이 방법은 나를 위해 작동하지 않기 때문에 나는 그렇게 할 다른 방법을 찾아야합니다.
솔루션
public bool Copy()
{
string destPathRelease = ThisUser.DestPath + "\\Release";
SHFILEOPSTRUCT struc = new SHFILEOPSTRUCT();
struc.hNameMappings = IntPtr.Zero;
struc.hwnd = IntPtr.Zero;
struc.lpszProgressTitle = "Rename Release directory";
struc.pFrom = destPathRelease + '\0';
struc.pTo = ThisUser.DestPath + '\\' + (string.IsNullOrEmpty(this.currBuildLabel) ? ("Release" + '_' + DateTime.Now.ToString("yyyyMMdd_HHmmss")) : this.currBranchName) + '.' + this.currBuildLabel + '\0';
struc.wFunc = FileFuncFlags.FO_RENAME;
int ret = SHFileOperation(ref struc);
}
제로 구분 더블 제로 종료 문자열로 pTo
및 pFrom
을 사용하는 것이 중요 있습니다.
http://stackoverflow.com/a/2024022/961113 – Habib
이것은 폴더에있는 파일을 '읽기 전용'으로 설정하고 특정 권한을 제한하게됩니다. – LukeHennerley
@ 하비브 (Habib)는 사용자가 무엇을 시도하고 있는지 확실하지 않습니다. 'Directory.Move'와 같은 다른 메소드가이 스레드에 없으므로 말하기. @LukeHennerley가 읽기 전용 일 경우 왜 Windows 탐색기에서 이름을 바꿀 수 있습니까? @both 전체 질문을 읽었습니까? (어떤 식 으로든 의미가 없도록하려고 시도하는 것) – theknut