Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 进程无法访问文件';E:\test.txt';因为它正被另一个进程使用_C#_Winforms - Fatal编程技术网

C# 进程无法访问文件';E:\test.txt';因为它正被另一个进程使用

C# 进程无法访问文件';E:\test.txt';因为它正被另一个进程使用,c#,winforms,C#,Winforms,我正在尝试从文本文件中删除顺序不正确的空格,以在winForm中删除一个空格序列,即 从 到 我的实施是: private void buttonBrowse_Click(object sender, EventArgs e) { Stream myStream; OpenFileDialog openFileDialogImage = new OpenFileDialog(); openFileDialog

我正在尝试从文本文件中删除顺序不正确的
空格
,以
在winForm中删除一个空格
序列,即

我的实施是:

private void buttonBrowse_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog openFileDialogImage = new OpenFileDialog();
            openFileDialogImage.Filter = "Text files | .txt";
            openFileDialogImage.Multiselect = false;

            if (openFileDialogImage.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if ((myStream = openFileDialogImage.OpenFile()) != null)
                {
                    textBoxFileName.Text = openFileDialogImage.FileName;
                }
            }
        }              

    private void buttonGo_Click(object sender, EventArgs e)
        {
            string path = textBoxFileName.Text;
            string s = string.Empty;
            using (StreamReader reader = new StreamReader(path, true))
            {
                s = reader.ReadToEnd();
            }
            string[] parts = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.ShowDialog();
            string pathSave = saveFileDialog.FileName;
            File.CreateText(pathSave);
            using (StreamWriter sw = new StreamWriter(pathSave))
            {
                sw.Write(parts);
            }
        }
    }
}
我使用(StreamWriter sw=new StreamWriter(pathSave))联机
时出现的错误是:

The process cannot access the file 'E:\test.txt' because it is being used by another process.

我下载了ProcessWorker以查看哪个进程当前正在锁定
Test.txt
,但没有看到任何进程使用它。有没有办法解决这个问题

除了其他建议之外,您的问题是
File.CreateText()
将被锁定,因此需要释放锁。我已将对
File.CreateText()
的调用包装在一个using语句中,以释放锁

StreamWriter的输出有问题,因此我根据您的问题进行了一些更改以获得预期的输出

private void buttonGo_Click(object sender, EventArgs e)
{
    string path = textBoxFileName.Text;
    string s = string.Empty;
    string[] parts;
    using (StreamReader reader = new StreamReader(path, true))
    {
        parts = reader.ReadToEnd().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    }

    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.ShowDialog();
    string pathSave = saveFileDialog.FileName;

    using (File.CreateText(pathSave))
    { }

    using (StreamWriter sw = new StreamWriter(pathSave))
    {
        string result = string.Empty;
        foreach (string s in parts)
        {
            result += s + " ";
        }
        sw.Write(result);
    }
}

除了其他建议之外,您的问题是
File.CreateText()
将被锁定,因此需要释放锁。我已将对
File.CreateText()
的调用包装在一个using语句中,以释放锁

StreamWriter的输出有问题,因此我根据您的问题进行了一些更改以获得预期的输出

private void buttonGo_Click(object sender, EventArgs e)
{
    string path = textBoxFileName.Text;
    string s = string.Empty;
    string[] parts;
    using (StreamReader reader = new StreamReader(path, true))
    {
        parts = reader.ReadToEnd().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    }

    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.ShowDialog();
    string pathSave = saveFileDialog.FileName;

    using (File.CreateText(pathSave))
    { }

    using (StreamWriter sw = new StreamWriter(pathSave))
    {
        string result = string.Empty;
        foreach (string s in parts)
        {
            result += s + " ";
        }
        sw.Write(result);
    }
}

您的路径和pathSave是否是相同的test.txt文件?@Dr.Stitch No.
path
是上载原始文件时的路径
pathSave
是在创建新文件时使用的。保留或更改名称由您决定。如果用户为opendialog和SavedLog选择相同的路径,则可能会发生您所说的错误。这就是为什么我要问路径是否相同。您的路径和pathSave是相同的test.txt文件吗?@Dr.Stitch No.
path
是上载原始文件时的路径
pathSave
是在创建新文件时使用的。保留或更改名称由您决定。如果用户为opendialog和SavedLog选择相同的路径,则可能会发生您所说的错误。这就是为什么我要问路径是否相同。是的,它解决了这个问题。非常感谢。如果我在
SaveFileDialog
中将名称更改为新名称,它将文件另存为
文本文件
,但正在使用
WebMatrix
打开它。知道为什么吗?我不知道为什么它会以WebMatrix开张。WebMatrix不是我熟悉的技术。当
OpenFileDialog
打开时,窗口中不显示.txt文件。为什么?saveFileDialog.Filter=“txt文件(.txt)|.txt |所有文件(.)|***”;在调用saveFileDialog.ShowDialog()之前添加此选项;是的,它解决了这个问题。非常感谢。如果我在
SaveFileDialog
中将名称更改为新名称,它将文件另存为
文本文件
,但正在使用
WebMatrix
打开它。知道为什么吗?我不知道为什么它会以WebMatrix开张。WebMatrix不是我熟悉的技术。当
OpenFileDialog
打开时,窗口中不显示.txt文件。为什么?saveFileDialog.Filter=“txt文件(.txt)|.txt |所有文件(.)|***”;在调用saveFileDialog.ShowDialog()之前添加此选项;
private void buttonGo_Click(object sender, EventArgs e)
{
    string path = textBoxFileName.Text;
    string s = string.Empty;
    string[] parts;
    using (StreamReader reader = new StreamReader(path, true))
    {
        parts = reader.ReadToEnd().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    }

    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.ShowDialog();
    string pathSave = saveFileDialog.FileName;

    using (File.CreateText(pathSave))
    { }

    using (StreamWriter sw = new StreamWriter(pathSave))
    {
        string result = string.Empty;
        foreach (string s in parts)
        {
            result += s + " ";
        }
        sw.Write(result);
    }
}