C# 异常.文件正由另一个进程使用
本文关键字:进程 另一个 异常 文件 | 更新日期: 2023-09-27 18:32:35
我正在玩C#,遇到了一个问题。当我尝试制作一个新文件时,程序中断并说该文件正被另一个进程使用。这可能是我忽略的愚蠢之处,但我无法弄清楚!
这是我的代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace myNameSpace
{
public partial class MainWindow : Form
{
public static string folderPath = @"fullpath";
public MainWindow()
{
InitializeComponent();
StoredTb.Text = folderPath;
String[] files = Directory.GetFiles(folderPath);
foreach (string file in files)
myDropDown.Items.Add(Path.GetFileNameWithoutExtension(file));
}
private void myDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke(new MethodInvoker(myDropDown_invokedMethod));
}
private void myDropDown_invokedMethod()
{
string fullpath = MainWindow.folderPath + myDropDown.SelectedText + ".txt";
StreamReader sr = new StreamReader(fullpath);
NameTb.Text = myDropDown.SelectedText;
DescriptionTb.Text = sr.ReadToEnd();
sr.Close();
}
private void SaveBtn_Click(object sender, EventArgs e)
{
File.Create(NameTb.Text + ".txt");
TextWriter tw = new StreamWriter(NameTb.Text + ".txt"); /* this is where the problem occurs */
tw.WriteLine("The very first line!");
tw.Close();
}
}
}
很抱歉代码片段很长,但由于我不确定问题的根源,我不得不包含几乎所有内容。
您的问题是File.Create
将打开一个stream
,允许您对文件执行所需的操作:
http://msdn.microsoft.com/en-us/library/d62kzs03.aspx
提供对路径中指定的文件的读/写访问权限的文件流。
因此,从技术上讲,它已经在使用中。
只需完全删除File.Create
即可。如果文件不存在,StreamWriter
将处理文件的创建。
此外,投资using
构造以正确处理文件连接。将有助于在将来避免这种情况。
根据MSDN,File.Create方法(字符串)使用FileStream,在您的情况下,它不会被关闭。使用类似这样的东西:
FileStream fs = new FileStream(NameTb.Text + ".txt");
File.Create(fs);
fs.Close();
或@Muctadir第纳尔
var fileStream = File.Create(NameTb.Text + ".txt");
//... do all the writing using fileStream
fileStream.Close();
使用
myDropDown_invokedMethod();
而不是
this.BeginInvoke(new MethodInvoker(myDropDown_invokedMethod));
File.Create 返回一个保存文件的 FileStream 对象。您应该将此对象用于进一步的任务。
var fileStream = File.Create(NameTb.Text + ".txt");
//... do all the writing using fileStream
fileStream.Close();
或者你可以只做
var fs = File.Create(NameTb.Text + ".txt");
fs.Close();
TextWriter tw = new StreamWriter(NameTb.Text + ".txt");
tw.WriteLine("The very first line!");
tw.Close();
您的问题似乎出在SaveBtn_Click
事件中,您两次使用目标文件进行写入:
File.Create(NameTb.Text + ".txt");
TextWriter tw = new StreamWriter(NameTb.Text + ".txt");
删除此行:
File.Create(NameTb.Text + ".txt");
这是因为File.Create(NameTb.Text + ".txt");使文件保持打开状态:
尝试改用这个:
private void SaveBtn_Click(object sender, EventArgs e)
{
using(Stream stream = File.Create(NameTb.Text + ".txt"))
{
TextWriter tw = new StreamWriter(stream); /* this is where the problem was */
tw.WriteLine("The very first line!");
tw.Close();
}
}
这将强制在方法退出时释放File.Create
。释放将关闭文件句柄(win32 非托管句柄)。否则,垃圾回收器将关闭该文件,但您永远不知道何时关闭。
我看到两种方法:1)使用System.IO.File.WriteAllText http://msdn.microsoft.com/en-us/library/vstudio/8bh11f1k.aspx2)阅读有关处置 http://msdn.microsoft.com/en-us/library/system.idisposable.aspx