首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用FileStream在文件创建后第一次写入文件会出现异常

使用FileStream在文件创建后第一次写入文件会出现异常
EN

Stack Overflow用户
提问于 2018-05-31 02:32:42
回答 1查看 69关注 0票数 0

我正试着写一些json的文字。但是我得到了一个异常,比如进程不能访问文件C:\blah blah\SystemInActivity.json,因为其他进程正在使用它。但是,在创建json文件之后,当我第二次运行应用程序时,当我编写时,我不会得到异常。请帮帮忙。

代码语言:javascript
运行
复制
class ApplicationSettingsViewModel
    {
        ApplicationSettingsModel model;
        MemoryMappedFile mmf = null;
        public string FullPath = string.Empty;
        //This is not a singleton class but I guess it has to be one but its ok for demonstration.
        public ApplicationSettingsViewModel()
        {
            model = new ApplicationSettingsModel();
            CreateFileWithoutMemoryMap();
            //MemoryMapped();
        }

        public string GetDriectory()
        {
            return Path.GetDirectoryName(FullPath);
        }

        private void CreateFileWithoutMemoryMap()
        {
            var info = Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "/" + model.Data.Settings.OrcaUISpecificSettings.TimeOutFolder);
            string path = Path.Combine(info.FullName + @"\" + model.Data.Settings.OrcaUISpecificSettings.File);
            //mmf = MemoryMappedFile.CreateFromFile(path, FileMode.CreateNew, "MyMemoryFile", 1024 * 1024, MemoryMappedFileAccess.ReadWrite);
            FullPath = path;
            if (!File.Exists(path))
            {
                File.Create(path);
            }
        }

        public void WriteToFile(string json)
        {
            try
            {
                FileStream fileStream = File.Open(FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //This line giving Exception
                fileStream.SetLength(0);
                fileStream.Close(); // This flushes the content, too.
                using (StreamWriter sw = new StreamWriter(FullPath))
                {
                    sw.Write(json);
                }
            }
            catch (Exception ex)
            {

            }
        }

在MainWindow的构造函数中,我调用写方法

代码语言:javascript
运行
复制
private ApplicationSettingsViewModel AppViewModel;
public MainWindow()
        {
            InitializeComponent();
            //MessageBox.Show("App Started");
            AppViewModel = new ApplicationSettingsViewModel();
            WriteToFile("Active");

        }
public void WriteToFile(string status)
        {
            var root = new Root();
            string jsonString = string.Empty;
            root.AllApplications.Add(new DataToWrite() { AppName = "DevOrca", Status = status });
            try
            {
                jsonString = JsonConvert.SerializeObject(root, Formatting.Indented);
            }
            catch (Exception ex)
            {
                MessageBox.Show(jsonString);
                MessageBox.Show("Exception");
            }
            mutex.WaitOne();
            //Serialize Contents and write
            AppViewModel.WriteToFile(jsonString);
            //var access = AppViewModel.GetAccessor();
            //byte[] bytes = Encoding.ASCII.GetBytes(jsonString);
            //access.Write(bytes, 0, bytes.Length);
            mutex.ReleaseMutex();
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-31 02:42:21

File.Create()方法打开FileStream来创建一个文件,您需要关闭它,如下所示:

代码语言:javascript
运行
复制
File.Create(path).Close();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50615817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档