写入文件错误 IOException: Sharing violation on path

IOException: Sharing violation on path

首次运行将会报错。

原因

File.Create后也需要 关闭 ,否则再次访问文件时,会提示错误 占用。

把代码copy到工程试试吧

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;

public class Test
{
    [MenuItem("Test/123")]
    public static void DebugDebugEditorUserBuildSettingsMenu()
    {
        string str = "阿里山的空间法拉盛大家拉萨酱豆腐";
        WriteFile(str, Application.dataPath + "/123/123.txt");

        AssetDatabase.Refresh();
    }


    public static void WriteFile(string str, string fileFullPath)
    {
        string Dir = Path.GetDirectoryName(fileFullPath);

        if (Directory.Exists(Dir) == false)
        {
            Directory.CreateDirectory(Dir);
        }
        if (File.Exists(fileFullPath) == false)
        {
            File.Create(fileFullPath);
            //File.Create(fileFullPath).Dispose(); //加入Dispose就没问题
        }

        Stream stream = File.Open(fileFullPath, FileMode.Truncate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(stream, new UTF8Encoding(false));
        sw.Write(str);
        sw.Close();
    }
}
//运行生成文件 将会报错
/*
 * IOException: Sharing violation on path D:\WorkProject\AssetBundle\Assets\123\123.txt
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) (at <437ba245d8404784b9fbab9b439ac908>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
System.IO.File.Open (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) (at <437ba245d8404784b9fbab9b439ac908>:0)
Test.WriteFile (System.String str, System.String fileFullPath) (at Assets/Script/Test/Test.cs:33)
Test.DebugDebugEditorUserBuildSettingsMenu () (at Assets/Script/Test/Test.cs:14)
*/

 

点赞
  1. Google Chrome Windows 10

    Very nice post. I simply stumbled upon your weblog and wished to mention that I've truly enjoyed surfing around your blog posts.

  2. Google Chrome Windows 10

    I am commenting to let you know of the fine encounter my friend's daughter found reading your site. She figured out numerous issues, not to mention what it's like to have a wonderful giving mindset to have men and women quite simply have an understanding of selected hard to do subject areas. You really exceeded our expectations. Many thanks for giving the insightful, safe, explanatory and as well as easy thoughts on the topic to Janet.

  3. Google Chrome Windows 10

    I am only writing to make you understand what a wonderful experience our princess went through visiting yuor web blog. She realized too many things, with the inclusion of what it is like to possess an incredible teaching style to make the rest without problems understand various very confusing topics. You undoubtedly surpassed visitors' expected results. Thanks for supplying those warm and helpful, trustworthy, revealing not to mention unique tips on your topic to Tanya.

  4. remeron buy说道:
    Google Chrome Windows 10

    My husband and i felt absolutely excited Emmanuel could conclude his inquiry from your ideas he gained in your site. It's not at all simplistic just to be giving for free ideas that most people have been making money from. And we all do understand we have the website owner to be grateful to because of that. The explanations you've made, the easy site menu, the friendships you can assist to engender - it's everything exceptional, and it's assisting our son and our family do think that idea is cool, and that is highly important. Many thanks for all the pieces!

  5. buy imipramine说道:
    Google Chrome Windows 10

    My wife and i have been really more than happy when Louis managed to finish off his researching using the precious recommendations he made through your web site. It is now and again perplexing to simply find yourself making a gift of strategies which often some other people may have been trying to sell. Therefore we realize we need the writer to give thanks to for this. The specific illustrations you made, the straightforward web site navigation, the relationships you will help to instill - it is most remarkable, and it is leading our son in addition to us reason why this subject is interesting, which is seriously pressing. Thanks for the whole lot!

  6. doxepin tablets说道:
    Google Chrome Windows 10

    I simply wished to thank you very much yet again. I do not know what I would have worked on without those tricks shown by you relating to this topic. This was the troublesome situation in my view, nevertheless witnessing your well-written way you treated the issue took me to weep with joy. Now i am thankful for your work as well as wish you recognize what an amazing job your are undertaking training the rest with the aid of your websites. Probably you haven't met any of us.

remeron buy进行回复 取消回复

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Title - Artist
0:00