简体   繁体   中英

"The process cannot access the file --- because it is being used by another process" when making txt file C#

string path = @$"c:\Users\asmet\Desktop\Database1/{username}";

if (File.Exists(path) == false)
{
    Console.WriteLine("Creating new file..");
    File.Create(path);
          
    // this is where the error is
    using (StreamWriter sw = new StreamWriter(path, true)) 
    {
        sw.WriteLine(DateTime.Now);
        sw.WriteLine($"rf = {rf}");
        sw.WriteLine($"pf = {pf}");
        sw.WriteLine($"sf = {sf}");
        sw.Close();
    }
}

I'm super new to C# and I don't really know what I am doing, but whenever I try to make a new file to safe rf, pf, and sf which are all number values it crashes.

This is in VS Code in a console application.

Username is from Console.ReadLine() in earlier code

Exact error message:

System.IO.IOException: 'The process cannot access the file 'c:\Users\asmet\Desktop\Database1\ausernameientered' because it is being used by another process.'

DataBase1 is just a folder

I've tried looking at forms relating to the issue but after awhile of searching I never found a solution, I want the code to make a new file at the location specified and then put the values in the text document, then close it, so I can save and look at the data later for a school project.

What happened is it crashes every time after I enter in the "username" and it tries to create a new file.

If you take out:

        File.Create(path);

it might work.

StreamWriter(String, Boolean)
Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file .

File.Create opens a file stream. You should switch

using (StreamWriter sw = new StreamWriter(path, true)) **// this is where the error is**
{
    sw.WriteLine(DateTime.Now);
    sw.WriteLine($"rf = {rf}");
    sw.WriteLine($"pf = {pf}");
    sw.WriteLine($"sf = {sf}");
    sw.Close();
}

With

using (StreamWriter sw = File.Create(path)) **// this is where the error is**
{
    sw.WriteLine(DateTime.Now);
    sw.WriteLine($"rf = {rf}");
    sw.WriteLine($"pf = {pf}");
    sw.WriteLine($"sf = {sf}");
    sw.Close();
}

You can't have 2 file streams open at once.

You can use File.WriteAllLines to create and write lines to a file. According to the documentation :

Creates a new file, writes one or more strings to the file, and then closes the file. :

if (!File.Exists(path))
{
    Console.WriteLine("Creating new file...");

    File.WriteAllLines(path,
        new[]
        {
            DateTime.Now.ToString(),
            $"rf = {rf}",
            $"pf = {pf}",
            $"sf = {sf}"
        });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM