提问人:Golden_Eagle 提问时间:3/28/2023 最后编辑:Golden_Eagle 更新时间:3/28/2023 访问量:59
尝试删除.json文件时出现“System.IO.IOException”
'System.IO.IOException' when trying to delete .json files
问:
我正在尝试创建一个系统来删除一组文件夹,每个文件夹中都有一个.json文件(以及一堆其他文件/子目录)。但是,每次我尝试时,总会有一个随机的.json文件无法删除,因为另一个进程正在访问该文件。在有人因为被问到很多问题而删除这篇文章之前,我已经尝试了我读过的所有内容。我尝试将目录的 FileAttributes 设置为正常(当我遇到其他未链接到.json文件的目录出现相同问题时,这有效),我尝试将访问目录的线程设置为后台线程,以确保在应用程序停止后没有一个仍在运行,确保在运行程序之前关闭了文件资源管理器, 设置每个流的文件访问权限,将每个文件保存到 ReadWrite,释放写入保存 (.json) 文件而不是关闭它的 StreamWriter...没有任何效果,我没有想法。此时,只有在删除.json文件时,我才会出现此异常。我还错过了什么?
删除所有类型的每个文件的函数(嵌套函数不是错别字):
public static void DeleteBrains()
{
Console.WriteLine("Resetting brains...");
Dictionary<int, Thread> threads = new Dictionary<int, Thread>();
for (int i = 0; i < 100; i++)
{
DeleteDirProtector(i);
}
StartThreads(threads, false);
void DeleteDirProtector(int i)
{
Thread thread = new Thread(() => DeleteDir(i));
threads.Add(i, thread);
}
void DeleteDir(int i)
{
string CurrentDir = $@"{DebugFolderPath}\Brain {i}";
if (Directory.Exists(CurrentDir))
{
Console.WriteLine($"Deleting Brain {i}...");
File.SetAttributes(CurrentDir, FileAttributes.Normal);
string[] directories = Directory.GetDirectories(CurrentDir);
string[] files = Directory.GetFiles(CurrentDir);
for (int j = 0; j < directories.Length; j++)
{
File.SetAttributes(directories[j], FileAttributes.Normal);
}
for (int j = 0; j < files.Length; j++)
{
File.SetAttributes(files[j], FileAttributes.Normal);
}
Directory.Delete(CurrentDir, true);
}
}
保存每个.json文件的函数:
public void SaveBrain(bool isUpdate)
{
BrainSaveString = JsonConvert.SerializeObject(this, Formatting.Indented);
string path = "";
if (isUpdate)
{
path = BrainSavePath2;
}
else if (!isUpdate)
{
path = BrainSavePath;
}
FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
using (StreamWriter sw = new StreamWriter(stream))
{
sw.Write(BrainSaveString);
sw.Dispose();
}
}
这可能无关紧要,但这是 StartThreads() 方法:
public static void StartThreads(Dictionary<int, Thread> threads, bool isBackground)
{
int threadCount = threads.Count;
for (int i = 0; i < threadCount; i++)
{
try
{
if (threads[i].ThreadState == ThreadState.Unstarted)
{
while (!StartThread(threads[i], false, isBackground)){}
}
}
catch (KeyNotFoundException) { }
}
while (threads.Count > 0)
{
for(int i = 0; i < threadCount; i++)
{
try
{
if (threads[i].ThreadState == ThreadState.Stopped)
{
threads.Remove(i);
}
}
catch (KeyNotFoundException) { }
}
}
}
编辑:StartThread()(根据要求):
public static bool StartThread(Thread thread, bool ShouldRunAnyway, bool isBackground)
{
bool shouldStartThread = false;
if (NumberOfThreads_ < NumberOfProcessors * 3)
{
shouldStartThread = true;
}
else
{
if (ShouldRunAnyway)
{
shouldStartThread = true;
}
}
if (shouldStartThread)
{
thread.IsBackground = isBackground;
thread.Start();
lock (AllThreadsBeingUsed)
{
AllThreadsBeingUsed.Add(thread);
}
}
return shouldStartThread;
}
答: 暂无答案
评论