简体   繁体   English

生成 txt 文件时出现“进程无法访问文件 --- 因为它正被另一个进程使用” C#

[英]"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.我是 C# 的超级新手,我真的不知道我在做什么,但每当我尝试为安全的 rf、pf 和 sf 创建一个新文件时,它们都会崩溃。

This is in VS Code in a console application.这是在控制台应用程序的 VS Code 中。

Username is from Console.ReadLine() in earlier code用户名来自早期代码中的Console.ReadLine()

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.' System.IO.IOException:“进程无法访问文件 'c:\Users\asmet\Desktop\Database1\ausernameientered',因为它正被另一个进程使用。”

DataBase1 is just a folder DataBase1只是一个文件夹

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.我试过查看与该问题相关的 forms,但经过一段时间的搜索后我从未找到解决方案,我希望代码在指定的位置创建一个新文件,然后将值放入文本文档,然后关闭它,所以我可以保存并稍后查看学校项目的数据。

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) StreamWriter(字符串,布尔值)
Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size.使用默认编码和缓冲区大小为指定文件初始化 StreamWriter class 的新实例。 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 File.Create 打开一个文件 stream。你应该切换

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.您不能同时打开 2 个文件流。

You can use File.WriteAllLines to create and write lines to a file.您可以使用File.WriteAllLines创建行并将行写入文件。 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}"
        });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该进程无法访问文件'D:.txt',因为它正在由c#中的另一个进程使用 - The process cannot access the file 'D:.txt' because it is being used by another process in c# c#无法访问文件,因为它正被另一个进程使用 - c# Cannot access file because it is being used by another process 该进程无法访问文件'C:\\ file.txt',因为它正由另一个进程使用 - The process cannot access the file 'C:\file.txt' because it is being used by another process 复制文件时使用C#(sql localdb)_IOEXCEPTION_进程无法访问文件,因为该文件正在被另一个进程使用 - c# when copying file(sql localdb)_IOEXCEPTION_ Process cannot access file because the file is being used by another process 进程无法访问文件,因为它被另一个进程使用 - c# - Process cannot access file because it is 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# File.ReadAllText 进程无法访问该文件,因为它正被另一个进程使用 - C# File.ReadAllText The process cannot access the file because it is being used by another process C#IOException:该进程无法访问文件,因为它正在被另一个进程使用 - C# IOException: The process cannot access the file because it is being used by another process C#进程无法访问文件''',因为它正由另一个进程使用 - C# The process cannot access the file ''' because it is being used by another process 该进程无法访问文件,因为c#中的另一个进程正在使用该文件 - The process cannot access the file because it is being used by another process in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM