이 코드를 먼저 파일 이름이 존재하지 않는지 확인합니다. 이름이 바뀌면 둘 다 동일한 파일 이름 (test.txt)을 가지므로 내 테스트 파일이 제공되면 앱에 알게되었습니다. ### test.txt, # ~ test.txt는 잘못된 문자를 제거하지 않습니다.
만약 그렇다면, 나는 다른 foreach 루프로 이동하여 잘못된 문자를 특정 문자로 바꾼다. 코드가 도움이 될 것입니다.는
감사합니다.
이 코드를 먼저 파일 이름이 존재하지 않는지 확인합니다. 이름이 바뀌면 둘 다 동일한 파일 이름 (test.txt)을 가지므로 내 테스트 파일이 제공되면 앱에 알게되었습니다. ### test.txt, # ~ test.txt는 잘못된 문자를 제거하지 않습니다.
만약 그렇다면, 나는 다른 foreach 루프로 이동하여 잘못된 문자를 특정 문자로 바꾼다. 코드가 도움이 될 것입니다.는
감사합니다.
File.Exists
을 사용하면 파일이 이미 있는지 확인할 수 있습니다. 귀하의 예제에서 그래서 http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
:
string sanitized = Path.Combine(pathOnly, sanitizedFileName);
if (!System.IO.File.Exists(sanitized))
{
// perform the move
System.IO.File.Move(files2, sanitized);
}
else
{
// perform other action
}
을 그리고 ChrisF이 유용하게 질문에 대한 코멘트 섹션에서 언급 한 바와 같이, 당신은 파일 이름이 유효한지 만들기위한 Path.GetInvalidFileNameChars
및 Path.GetInvalidPathChars
을 사용할 수 있습니다 여기에 참조입니다.
if (System.IO.File.Exists(sanitized))
{
// The file already exists!
}
int replacement = 0;
while(File.Exists(sanitized))
{
int lastDot = sanitized.LastIndexOf('.');
if(lastDot < 0)
lastDot=sanitized.Length;
sanitized = String.Format("{0}{1}{2}",
sanitized.Substring(0,lastDot - (replacement>0?1:0)),
replacement++,
sanitized.Substring(lastDot)
);
}
당신은 [Path.GetInvalidFileNameChars] (http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars.aspx)와 [경로가 있다는 것을 알고있다. GetInvalidPathChars] (http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars.aspx)? – ChrisF
실제로 저는 C#에 대해 매우 익숙하지 않았으므로이 사실을 알지 못했습니다. – yeahumok
죄송합니다. 잘못된 길로 왔습니다. 불법 문자를 하드 코딩하는 대신 이러한 문자를 사용해야합니다. 언젠가 마이크로 소프트가 언젠가 그걸 바꿀지 모른다. – ChrisF