简体   繁体   English

无法获取用户输入以在 C# 中写入我的文本文件(错误表示无法访问路径,因为它已在使用中)

[英]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.

相关问题 无法访问该文件,因为它正在被另一个进程c#使用 - The file cannot be accessed because it is being used by another process c# C#异常 - 无法访问文件,因为它正由另一个进程使用 - C# Exception - File cannot be accessed because it is being used by another process IOException:该进程无法访问文件“文件路径”,因为它正在被C#控制台应用程序中的另一个进程使用 - IOException: The process cannot access the file 'file path' because it is being used by another process in Console Application in C# C# .NET BackgroundWorker 6 FileSystemWatcher 该进程无法访问文件“PATH”,因为它正被另一个进程使用 - C# .NET BackgroundWorker 6 FileSystemWatcher The process cannot access the file "PATH" because it is being used by another process 当我从目录中删除文件时,出现错误提示我无法访问文件 - When I delete a file from a directory I get an error which says that I cannot access my file 如何在C#表单应用程序中使用xml基于xml文件中的用户输入获取标签的文本 - How to use xml with c# form application To get text for Lables Based on user Input from xml file C#为变量赋值表示无法找到文件? - C# assigning value to variable says it cannot find the file? 该进程无法访问该文件,因为尝试写入文件时出错 - The process cannot access the file because error while trying to write to a file 由于错误 CS0111,无法正确编写我的代码 - Cannot write my code right, because of an error CS0111 无法将 JSON 写入文件 (Xamarin C#) - Cannot write JSON to file (Xamarin C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM