gpt4 book ai didi

c# - 进程无法访问该文件,因为它正在被另一个进程在第二次调用时使用

转载 作者:太空宇宙 更新时间:2023-11-03 12:12:08 31 4
gpt4 key购买 nike

我有一个 Azure 函数 (HttpTrigger),它在我的 Azure 存储中创建一个 tar.gz 文件。

当我触发一次时,文件就成功创建了。但如果我再次点击它,我就会出错:

[Info] The process cannot access the file 'D:\local\Temp\myDir\tarArchive.tar.gz' because it is being used by another process.

我必须重新启动 Azure 门户中的功能才能创建另一个文件。

这是我的代码:

FileStream fs = new FileStream(myDir+"/firstFile", FileMode.Create);
fs.Write(bytesToCompress, 0, bytesToCompress.Length);
fs.Dispose();
FileStream sfs = new FileStream(myDir + "/secondfile", FileMode.Create);
sfs.Dispose();

DirectoryInfo DirectoryOfFilesToBeTarred = new DirectoryInfo(myDir);
FileInfo[] filesInDirectory = DirectoryOfFilesToBeTarred.GetFiles();

string tarArchiveName = myDir + "/tarArchive.tar.gz";

using (Stream targetStream = new GZipOutputStream(File.Create(tarArchiveName)))
{
using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(targetStream, TarBuffer.DefaultBlockFactor))
{
foreach(FileInfo fileToBeTarred in filesInDirectory)
{
log.Info(fileToBeTarred.FullName);
TarEntry entry = TarEntry.CreateEntryFromFile(fileToBeTarred.FullName);
tarArchive.WriteEntry(entry, true); // Error thrown here

}
}
}

我认为再次调用该函数时 fileToBeTarred 仍在使用中(我错了吗?)但我已尝试从此 FileInfo 创建一个流以便 Dispose() 它,但没有解决我的问题。我也试过 Delete() 它,但没有任何效果。

有人看到了我没看到的东西吗?

谢谢你的帮助

更新

这是 Wim Coenen 给出的修正代码

using (Stream fileStream = File.Create(tarArchiveName))
using (Stream targetStream = new GZipOutputStream(fileStream))
using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(targetStream, TarBuffer.DefaultBlockFactor))
{
foreach (FileInfo fileToBeTarred in filesInDirectory)
{
log.Info(fileToBeTarred.FullName);
TarEntry entry = TarEntry.CreateEntryFromFile(fileToBeTarred.FullName);
tarArchive.WriteEntry(entry, true); // Error thrown here
}
}

以及错误日志(e = Exception 对象)e.message =>

2018-08-01T11:59:46.887 [Info] The process cannot access the file 'D:\local\Temp\myDir\tarArchive.tar.gz' because it is being used by another process.

e.ToString() =>

2018-08-01T11:59:47.152 [Info] System.IO.IOException: The process cannot access the file 'D:\local\Temp\myDir\tarArchive.tar.gz' because it is being used by another process.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)

at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)

at ICSharpCode.SharpZipLib.Tar.TarArchive.WriteEntryCore(TarEntry sourceEntry, Boolean recurse)

at ICSharpCode.SharpZipLib.Tar.TarArchive.WriteEntry(TarEntry sourceEntry, Boolean recurse)

at UploadFileFromBCText.Function1.d__4.MoveNext()

最佳答案

看起来您不是在 Azure 存储中创建它,而是在 Function App 实例的本地磁盘上创建它。这可能不是一个好主意,因为实例是短暂的,不会长期保留您的文件。

相反,请查看 Azure Blob Storage Output binding - 它的目的是在不使用低级文件 API 或 SDK 的情况下将文件存储在 Azure 存储中。

关于c# - 进程无法访问该文件,因为它正在被另一个进程在第二次调用时使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51630372/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com