[英]C# "File is being used by another process"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lists
{
public partial class Form3 : Form
{
public List<string> storeNames = new List<string>();
//public List<string> data = new List<string>();
public string fullPath;
public string[] sr;
public int counter = 0;
// Change the path values to where names.txt is located on your computer
public string pathName = "C:\\Users\\xxxxxxxxxxx\\Desktop";
public string fileName = "\\names.txt";
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
readFileIO(pathName, fileName);
}
private void listNamesBtn_Click(object sender, EventArgs e)
{
MessageBox.Show($"{counter}", "Number of list names");
return;
}
private void addNameBtn_Click(object sender, EventArgs e)
{
writeFile(pathName, fileName);
}
public void writeFile(string pathName, string fileName)
{
string fullPath = pathName + fileName;
TextWriter txt = new StreamWriter(fullPath, true);
txt.Write("\n" + Input.Text);
curNamesBox.Items.Clear();
readFileIO(pathName, fileName);
txt.Dispose();
}
public void readFileIO(string pathName, string fileName)
{
fullPath = pathName + fileName;
var lines = File.ReadAllLines(fullPath);
foreach (string line in lines)
{
counter++;
curNamesBox.Items.Add(line);
}
}
private void deleteNameBtn_Click(object sender, EventArgs e)
{
searchName("meep morp");
deleteNameFunc();
}
public void searchName(string registeredName)
{
foreach(string searchName in curNamesBox.Items)
{
if (registeredName.Equals(searchName))
{
Console.WriteLine("I FOUND YOU :)");
}
}
string[] data = File.ReadAllLines(fullPath);
foreach (string line in data)
{
Console.WriteLine(line);
if (line.Equals("meep morp"))
{
Console.WriteLine("cry about it.");
}
}
}
public void deleteNameFunc()
{
foreach(string name in File.ReadAllLines(fullPath))
{
storeNames.Add(name);
}
int namesIndex = 0;
foreach(string x in storeNames.ToList())
{
namesIndex++;
Console.WriteLine(x);
Console.WriteLine(namesIndex);
if (x.Equals("meep morp"))
{
storeNames.Remove("Tabitha");
removeNCreateFile();
}
}
foreach(string y in storeNames)
{
Console.WriteLine(y);
}
}
public void removeNCreateFile()
{
File.Delete(fullPath);
TextWriter txt = new StreamWriter(fullPath, true);
File.Create(fullPath);
foreach (string name in storeNames)
{
txt.Write("\n" + name);
}
txt.Dispose();
}
private void curNamesBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
該代碼應該在單擊按鈕時為文件添加一個名稱,並且應該刪除舊文件並將其替換為列表框中的新信息但返回說該過程已被使用?
我的代碼說的是 ' var lines = File.ReadAllLines(fullPath);
周圍的行 ' 在 ReadFileIO 方法附近。 有人可以幫我解決這個問題並了解原因嗎?
單擊按鈕時,我試圖為文件添加一個名稱,但它說該文件已被另一個進程使用。
Streamwriter 正在打開文件
TextWriter txt = new StreamWriter(fullPath, true)
然后在這里再次嘗試打開
readFileIO(pathName, fileName);
您需要將文件傳遞給 readFileIO 或關閉文件 stream。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.