gpt4 book ai didi

c# - 创建文件后发生文件共享冲突?

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:39 24 4
gpt4 key购买 nike

因此,我正在尝试创建一个 .txt 文件,然后向其中写入一些愚蠢的数据。但是我遇到了共享违规。我觉得这可能是因为我试图在创建文件后直接为文件创建 StreamWriter,但这没有意义。所以我有点迷路了。我已经尝试删除类(class)中除错误行之外的所有其他 StreamWriters 和 Readers,但我仍然遇到违规行为。我得到的错误,

IOException: Sharing violation on path C:\Users\USER\Desktop\Accessible\Assets\IO\Books\A community of learners\frog\frog_status.txt
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options)
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)

指向直线:

StreamWriter sw = new StreamWriter(statusTxtPath);

在下面的函数中:

private IEnumerator GatherStatusInfo(string folderPath, string mediaName) {

string statusTxtPath = folderPath + @"/" + mediaName + "_status.txt";
_currentInfoBeingCreated._readingStatusTxtPath = statusTxtPath;

if(BuildManager.instance._isResetStatusFilesWhenStarting) {
if(File.Exists(statusTxtPath))
File.Delete(statusTxtPath);
}

if(!File.Exists(statusTxtPath)) {
File.Create(statusTxtPath);
StreamWriter sw = new StreamWriter(statusTxtPath);
sw.WriteLine(MediaStatus.Unread);
sw.WriteLine("0");
_currentInfoBeingCreated._status = MediaStatus.Unread;
_currentInfoBeingCreated._pageLastRead = 0;
sw.Flush();
sw.Close();

} else {

StreamReader statusReader = new StreamReader(statusTxtPath);
_currentInfoBeingCreated._status = (MediaStatus) Enum.Parse(typeof(MediaStatus), statusReader.ReadLine());
_currentInfoBeingCreated._pageLastRead = (int) ConversionUtils.instance.String2Float(statusReader.ReadLine());
statusReader.Close();
}

yield return 0;
}

知道为什么那只狗不打猎吗?

最佳答案

您是否想要创建文件然后重新打开它以写入它。 StreamWriter有一种方法可以做到这一点。如果文件不存在,它将创建一个新文件。

来自上面的链接:

Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.

关于c# - 创建文件后发生文件共享冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14313303/

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