写入文件,文件正被另一个进程使用 [英] Writing to file, file being used by another process

查看:40
本文介绍了写入文件,文件正被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我不明白为什么我不能写入文件.它说它正在被另一个进程使用.这是错误(IOException 未处理):

Alright, so I can't figure out why I can't write to a file. It says it's being used by another process. Here's the error (IOException was unhandled):

The process cannot access the file 'C:\Temp\TempFile.cfg' because it is being used by another process.

这是我用来写入文件的当前代码:

Here's the current code I'm using to write to the file:

Dim myConfig
    Dim saveFileDialog1 As New SaveFileDialog()

    saveFileDialog1.Filter = "Configuration Files (*.cfg)|*.cfg"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True

    If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        myConfig = saveFileDialog1.OpenFile()
        If (myConfig IsNot Nothing) Then
            System.IO.File.WriteAllText(saveFileDialog1.FileName, TextBox1_Output_Module.Text & vbCrLf & TextBox2_Output_Module.Text & vbCrLf & TextBox3_Output_Module.Text)
            myConfig.Close()
        End If
    End If

我不确定我错过了什么,因为我以为我昨天测试了它并且它有效.

I'm not sure what I am missing as I thought I tested this yesterday and it worked.

推荐答案

好吧,这就是我最终做的,似乎工作得很好.我取出了 if 条件并保留了其他所有内容.我以后可以随时为取消编码.

Well here's what I ended up doing, seems to be working just fine as is. I took out the if condition and left everything else as is. I can always code for the cancel later on.

    Dim myConfig
    Dim saveFileDialog1 As New SaveFileDialog()

    saveFileDialog1.Filter = "Configuration Files (*.cfg)|*.cfg"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True

    System.IO.File.WriteAllText(saveFileDialog1.FileName, TextBox1_Output_Module.Text & vbCrLf & TextBox2_Output_Module.Text & vbCrLf & TextBox3_Output_Module.Text)

这是确定/取消按钮的代码.

This codes for the ok/cancel button.

    If saveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        System.IO.File.WriteAllText(saveFileDialog1.FileName, TextBox1_Output_Module.Text & vbCrLf & TextBox2_Output_Module.Text & vbCrLf & TextBox3_Output_Module.Text)
    End If

这篇关于写入文件,文件正被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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