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