저는 VS2012, WinRT 및 C#으로 작업하고 있습니다.DeleteAsync가있는 파일을 삭제할 수 없습니다.
일부 파일을 압축 해제 한 후 삭제하려고합니다. "Access is denied"오류가 발생합니다. 응용 프로그램을 중지하고 다시 시작하면 동일한 코드가 제대로 작동하므로 여전히 핸들이 붙어있는 것 같습니다.
unZipFile 메서드를 호출하지 않으면 해당 파일을 삭제할 수 있습니다.
파일을 공개하는 확실한 방법이 있습니까? 나는 그것을 삭제하기 전에 null (file = null;)으로 설정했다.
여기에 압축 해제 방법을 호출하는 코드의 블록입니다 :
StorageFile file = await CreateOutputFile(fileName, path);
MemoryStream theMemStream = new MemoryStream();
theMemStream.Write(bytes, 0, bytes.Length);
await FileIO.WriteBytesAsync(file, bytes);
await theMemStream.FlushAsync();
theMemStream.Dispose();
var result = await unZipFile(file, path);
file = null;
다음은 unZipFile 방법입니다 :
private async Task<string> unZipFile(StorageFile file, string path)
{
StorageFolder sf = await GetOutputFolder(path);
using (var zipStream = await file.OpenStreamForReadAsync())
{
using (MemoryStream zipMemoryStream = new MemoryStream((int)zipStream.Length))
{
await zipStream.CopyToAsync(zipMemoryStream);
try
{
var archive = SharpCompress.Archive.ArchiveFactory.Open(file.Path);
foreach (var entry in archive.Entries)
{
entry.WriteTo(zipMemoryStream);
Stream fileData = entry.OpenEntryStream();
StorageFile outputFile = await sf.CreateFileAsync(entry.FilePath, CreationCollisionOption.ReplaceExisting);
using (Stream outputFileStream = await outputFile.OpenStreamForWriteAsync())
{
await fileData.CopyToAsync(outputFileStream);
await outputFileStream.FlushAsync();
outputFileStream.Dispose();
}
}
archive = null;
}
catch (Exception ex)
{
throw new IOException("Error writing decompressed output file: " + ex.Message);
}
await zipStream.FlushAsync();
zipStream.Dispose();
await zipMemoryStream.FlushAsync();
zipMemoryStream.Dispose();
}
}
return "success";
}
여기 삭제 방법입니다. 이것은 압축 해제 후 각 파일에 대해 호출됩니다 : 당신은 예외를받을 수 있나요 어떤 파일에
private async Task<string> deleteFile(string path, string filename)
{
StorageFolder folder = await GetOutputFolder(path);
var files = await folder.GetFilesAsync();
foreach (StorageFile file in files)
{
try
{
if (file != null)
{
if (file.Name == filename)
await file.DeleteAsync();
}
}
catch (Exception ex)
{
return ex.Message;
}
}
return "success";
}
그게 전부 였어! 위대한 캐치. 나는 당신에게 클릭을 주겠지 만 아직 충분한 점수가 없기 때문에 나를 풀어주지 않을 것입니다. 최대한 빨리 할 수 있습니다. – CodeChops