簡體   English   中英

C#異常。 文件正由另一個進程使用

[英]C# exception. File is being used by another process

我正在玩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

FileStream,提供對path中指定的文件的讀/寫訪問權限。

因此,從技術上講,它已經在使用中。

只需刪除File.Create 如果文件不存在, StreamWriter將處理創建文件。

此外,還要投資using構造來正確處理文件連接。 將來有助於避免這種情況。

使用

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");

根據MSDN, File.Create方法(String)使用FileStream,在您的情況下不會關閉。 使用這樣的東西:

FileStream fs = new FileStream(NameTb.Text + ".txt");
File.Create(fs);
fs.Close();

或@Muctadir Dinar

var fileStream = File.Create(NameTb.Text + ".txt");
//... do all the writing using fileStream
fileStream.Close();

那是因為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為Disposed。 dispose將關閉文件句柄(win32非托管句柄)。 否則,垃圾收集器將關閉該文件,但您永遠不知道何時。

我看到兩種方式:1)使用System.IO.File.WriteAllText http://msdn.microsoft.com/en-us/library/vstudio/8bh11f1k.aspx 2)閱讀有關處理http://msdn.microsoft.com /en-us/library/system.idisposable.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM