IOException:该过程无法访问文件' filename/textfile.txt'因为另一个过程正在使用它

发布于 2025-01-31 19:39:10 字数 1540 浏览 2 评论 0原文

我看到了有关此问题的其他线程,但似乎没有解决我的确切问题。

static void RecordUpdater(string username,int points,string term) //Updates Record File with New Records.
        {
            int minPoints = 0;
            StreamWriter streamWriter = new StreamWriter($@"Record\{term}");
            Player playersRecord = new Player(points, username);
            List<Player> allRecords = new List<Player>();
            StreamReader reader = new StreamReader($@"Record\{term}");
            while (!reader.EndOfStream)
            {
                string[] splitText = reader.ReadLine().Split(',');
                Player record = new Player(Convert.ToInt32(splitText[0]), splitText[1]);
                allRecords.Add(record);
            }
            
            reader.Close();

            foreach (var playerpoint in allRecords )
            {
                if(minPoints > playerpoint.points)
                    minPoints = playerpoint.points;
            }
            if (points > minPoints)
            {

                allRecords.Add(playersRecord);
                allRecords.Remove(allRecords.Min());
            }
            allRecords.Sort();
            allRecords.Reverse();
            streamWriter.Flush();
            foreach (var player in allRecords)
            {
                streamWriter.WriteLine(player.points + "," + player.username);
            }
        }

因此,在运行程序并在代码中达到这一点后,我会收到一个错误消息: “该过程无法访问文件'fileName/textfile.txt',因为另一个过程正在使用它。”

I saw other threads about this problem, and non of them seems to solve my exact problem.

static void RecordUpdater(string username,int points,string term) //Updates Record File with New Records.
        {
            int minPoints = 0;
            StreamWriter streamWriter = new StreamWriter($@"Record\{term}");
            Player playersRecord = new Player(points, username);
            List<Player> allRecords = new List<Player>();
            StreamReader reader = new StreamReader($@"Record\{term}");
            while (!reader.EndOfStream)
            {
                string[] splitText = reader.ReadLine().Split(',');
                Player record = new Player(Convert.ToInt32(splitText[0]), splitText[1]);
                allRecords.Add(record);
            }
            
            reader.Close();

            foreach (var playerpoint in allRecords )
            {
                if(minPoints > playerpoint.points)
                    minPoints = playerpoint.points;
            }
            if (points > minPoints)
            {

                allRecords.Add(playersRecord);
                allRecords.Remove(allRecords.Min());
            }
            allRecords.Sort();
            allRecords.Reverse();
            streamWriter.Flush();
            foreach (var player in allRecords)
            {
                streamWriter.WriteLine(player.points + "," + player.username);
            }
        }

So after I run the program and get to that point in code I get an error message:
"The process cannot access the file 'fileName/textFile.txt' because it is being used by another process."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2025-02-07 19:39:10

You should use the using statement around disposable objects like streams.这将确保对象释放其持有的所有不受管理的资源。并且在需要之前,不要打开作者。首先需要阅读记录时,打开作者毫无意义,

static void RecordUpdater(string username,int points,string term) 
{
    Player playersRecord = new Player(points, username);
    List<Player> allRecords = new List<Player>();
    int minPoints = 0;
    try
    {
        using(StreamReader reader = new StreamReader($@"Record\{term}"))
        {
            while (!reader.EndOfStream)
            {
                .... load you data line by line
            }
        }        

        ..... process your data .....

        using(StreamWriter streamWriter = new StreamWriter($@"Record\{term}"))
        {
            ... write your data...
        }
    }
    catch(Exception ex)
    {
        ... show a message about the ex.Message or just log everything
            in a file for later analysis
    }
}

您也应该考虑使用文件是最可能的上下文之一,因为您的程序无法控制。<<<<<<<<< br>
最好将所有内容包装在try/catch block with proper handling of the exception

You should use the using statement around disposable objects like streams. This will ensure that the objects release every unmanaged resources they hold. And don't open the writer until you need it. Makes no sense to open the writer when first you need to read the records

static void RecordUpdater(string username,int points,string term) 
{
    Player playersRecord = new Player(points, username);
    List<Player> allRecords = new List<Player>();
    int minPoints = 0;
    try
    {
        using(StreamReader reader = new StreamReader($@"Record\{term}"))
        {
            while (!reader.EndOfStream)
            {
                .... load you data line by line
            }
        }        

        ..... process your data .....

        using(StreamWriter streamWriter = new StreamWriter($@"Record\{term}"))
        {
            ... write your data...
        }
    }
    catch(Exception ex)
    {
        ... show a message about the ex.Message or just log everything
            in a file for later analysis
    }
}

Also you should consider that working with files is one of the most probable context in which you could receive an exception due to external events in which your program has no control.
It is better to enclose everything in a try/catch block with proper handling of the exception

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文