2016-07-28 2 views
1

다른 폴더로 복사 한 후 zip 파일을 삭제하는 방법 ... 삭제하는 동안 예외가 발생합니다. "파일이 다른 프로세스에서 사용 중입니다"라는 메시지가 나타납니다. 당신이 압축 파일을 압축 해제하면다른 폴더로 복사 한 후 Zip 파일을 삭제하는 방법

string pathString1 = FullFilePath; 
string sourceFileName = Path.GetFileName(pathString1); 
string foldername = Path.GetDirectoryName(pathString1); 
string pathString = Path.Combine(foldername, "Uploaded"); 
if (!System.IO.Directory.Exists(pathString)) 
{ 
    System.IO.Directory.CreateDirectory(pathString); 
    string destFile = System.IO.Path.Combine(pathString, sourceFileName); 
    File.Copy(pathString1, destFile); 

    File.Delete(pathString1); 
    File.Delete(FileName); 
} 
+1

당신이 일을하는 데 사용하는 코드를 제공 재시도 마무리를 기다리고 다시 시도 파일들. –

+0

도움을 요청한 코드를 표시하십시오. –

+0

파일을 읽으려는 스트림을 닫아야합니다. 코드를 볼 수있게되면 도움이됩니다. –

답변

2

, 다음 사용하여 블록 또는 .Dispose() 압축 해제를 담당하는 객체에서이 작업을 수행. 무슨 lib를 사용하고 있습니까?

2

파일의 잠금을 방지하기 위해,이 작업으로 이루어집니다 파일을 해제 할 using 문 : 당신이 파일을 삭제하는 경우 당신은 왜, 복사 직후, 다시 그 다음

using (FileStream stream = File.Open("path to file", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
{ 
    ... 
} 

그냥 움직이지 않는가? 바이러스 검사가 .zip 파일로 전환하는 것이이 이론이 있기 때문에

File.Move(from, to); 
+0

을 기다려야 할 수도 있습니다.하지만 동일한 예외가 발생했습니다 ... 파일이 있기 때문에 액세스 할 수 없습니다. 다른 프로세스에서 사용하는 .. – Pinky

0

, 당신은 할 수는

string pathString1 = FullFilePath; 
string sourceFileName = Path.GetFileName(pathString1); 
string foldername = Path.GetDirectoryName(pathString1); 
string pathString = Path.Combine(foldername, "Uploaded"); 
if (!System.IO.Directory.Exists(pathString)) 
{ 
    System.IO.Directory.CreateDirectory(pathString); 
    string destFile = System.IO.Path.Combine(pathString, sourceFileName); 
    File.Copy(pathString1, destFile); 

    int itries = 0; 
    int maxtries = 30; //suitable time of retrying 
    while (itries++ < maxtries) 
    { 
     try 
     { 
      File.Delete(pathString1); 
      itries = 999999; 
     } 
     catch (Exception ex) 
     { 
      if (itries > maxtries) throw ex; 
      Thread.Sleep(1000); 
     }  

    } 

    //File.Delete(FileName); 
} 
관련 문제