创建文件后发生文件共享冲突? [英] File sharing violation occurs after creation of file?

查看:72
本文介绍了创建文件后发生文件共享冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试创建一个 .txt 文件,然后向其中写入一些愚蠢的数据.但我收到了共享违规.我感觉这可能是因为我试图在创建文件后直接为文件创建 StreamWriter,但这没有意义.所以我有点失落.除了错误的行外,我已经尝试删除类中的所有其他 StreamWriter 和 Reader,但我仍然遇到违规情况.我得到的错误,

So, I'm attempting to create a .txt file, and then write some silly data to it. But I'm getting a sharing violation. I sense that it may be because I'm attempting to create a StreamWriter for a file directly after creating it, but that doesn't make sense. So I'm a bit lost. I've tried removing all the other StreamWriters and Readers in the class except for the erroneous line, and I'm still getting a violation. The error I'm getting,

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;
    }

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

Any idea why that dog won't hunt?

推荐答案

您是否有任何原因想要创建文件然后重新打开它以写入文件.StreamWriter 有一个方法可以做到只是.如果它不存在,它将创建一个新文件.

Is there a reason why you are wanting to create the file then reopen it to write to it. StreamWriter has an method that will do just that. It will create a new file if it doesn't exist.

来自上面的链接:

使用默认编码和缓冲区大小为指定路径上的指定文件初始化 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.

这篇关于创建文件后发生文件共享冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆