[英]Cannot get user input to write to my text file in C# (error says path cannot be accessed because it is already in use)
I am at a loss as to what more I can do here.我不知道我还能在这里做什么。 I have attempted to use namespace StreamReadWrite but that did not work.我曾尝试使用命名空间 StreamReadWrite 但没有奏效。 I have tried to use file.Close();我曾尝试使用 file.Close(); in different areas but the most that would happen is I'd get "Success" written in the console, nothing displayed from my list and the text file completely empty.在不同的领域,但最可能发生的是我会在控制台中写下“成功”,我的列表中没有显示任何内容并且文本文件完全为空。 I'm not sure if there are several small errors I'm making, but I have been working on this for days and just cannot get the user input to write to the file.我不确定我是否犯了几个小错误,但我已经为此工作了好几天,只是无法让用户输入写入文件。 Any help is absolutely appreciated!任何帮助都非常感谢! I have written notes to hopefully explain well enough what each part of my code is referencing.我写了一些笔记,希望能很好地解释我的代码的每个部分所引用的内容。
public void writeReadTextFile(Car c) //another form is passing the user input into my list Car to this function
{
try
{
//string variables are all members of Car class
String vin;
String make;
String model;
String year;
String color;
char delim = ',';
StreamWriter fileWrite = new StreamWriter("../../Resources/CarInfo.txt");
foreach (Car car in myCars)
{
fileWrite.WriteLine(car.getVin());
fileWrite.Write(delim); //all attributes separated by comma in text file
fileWrite.Write(car.getMake());
fileWrite.Write(delim);
fileWrite.Write(car.getModel());
fileWrite.Write(delim);
fileWrite.Write(car.getYear());
fileWrite.Write(delim);
fileWrite.Write(car.getColor());
}
StreamReader file = new StreamReader("../../Resources/CarInfo.txt");
String line = file.ReadLine();
int comma;
Car newCar;
while (line != null)
{
//parsing data from text file which works when information is manually entered into text file
comma = line.IndexOf(delim);
vin = line.Substring(0, comma);
line = line.Substring(comma + 1);
comma = line.IndexOf(delim);
make = line.Substring(0, comma);
line = line.Substring(comma + 1);
comma = line.IndexOf(delim);
model = line.Substring(0, comma);
line = line.Substring(comma + 1);
comma = line.IndexOf(delim);
year = line.Substring(0, comma);
line = line.Substring(comma + 1);
color = line;
newCar = new Car(vin, make, model, year, color);
myCars.Add(newCar);
line = file.ReadLine();
Console.WriteLine(line);
}
Console.WriteLine("Success"); // test
}
catch (Exception e)
{
Console.WriteLine("Error loading file: " + e.Message);
}
}
You are simply not releasing the resources to the stream and in-turn not releasing the file handle .您只是没有将资源释放到流中,反过来也没有释放文件句柄。
Don't do this不要这样做
StreamWriter fileWrite = new StreamWriter(...)
Always use a using statement on an IDisposable
when possible, or (if you really need to) call Close
/ Dispose
manually.尽可能在IDisposable
上使用using 语句,或者(如果您确实需要)手动调用Close
/ Dispose
。 In-turn this will (in this case) close the stream and dispose the object, releasing the operating system file handle (which is most likely your problem)反过来,这将(在这种情况下)关闭流并处理对象,释放操作系统文件句柄(这很可能是您的问题)
using(StreamWriter fileWrite = new StreamWriter(...))
{
// code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.