如何关闭其他进程正在使用的文件 [英] how to close the file which is using by other process

查看:104
本文介绍了如何关闭其他进程正在使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发一个Windows应用程序。在此进行排序。首先,我正在阅读所有文件行,并在最后写入该文件的所有行,但它给了我错误这个pocess无法访问该文件,因为它被另一个进程使用

请帮助我,我是c#的新手。试着学习。



Ia developing a windows application. In this for sorting. first I am reading all the lines of file and at the last writing all lines to that file but it is giving me error"this pocess can not access the file because its is used by another process"
Please help me I am new to c#. trying to learn.

try
{
string[] input_lines = File.ReadAllLines("C:\\maq\\" + main1.text + "\\" + main1.text + ".txt");
              List<string>  input_lines_list  = input_lines.ToList();
                input_lines_list.RemoveAt(0);
                input_lines_list = input_lines_list.OrderBy((s) => s.Split(',')[0]).ThenByDescending((s) => DateTime.ParseExact(s.Split(',')[1], "yyyyMMdd", null)).ToList();
                int j = 0;
                while (j < input_lines_list.Count - 1)
                {
                    string[] line_detail = input_lines_list[j].Split(',');
                    string[] next_line_detail = input_lines_list[j + 1].Split(',');
                    if (line_detail[2] == line_detail[3] && line_detail[3] == line_detail[4] && line_detail[4] == line_detail[5] && line_detail[5] == input_lines_list[j + 1].Split(',')[5])
                    {
                        input_lines_list.RemoveAt(j);
                        continue;
                    }
                    else if (line_detail[0] == next_line_detail[0] && line_detail[2] == next_line_detail[2] && line_detail[3] == next_line_detail[3] && line_detail[4] == next_line_detail[4] && line_detail[5] == next_line_detail[5])
                    {
                        input_lines_list.RemoveAt(j + 1);
                        continue;
                    }
                    else if (line_detail[0] == next_line_detail[0] && line_detail[1] == next_line_detail[1])
                    {
                        input_lines_list.RemoveAt(j + 1);
                        continue;
                    }
                    j += 1;
                }
          }
          catch(Exception ex)
          {
                  MessageBox.Show(ex.Message);
          }

                input_lines_list.Insert(0, iplut_lines[0])

            File.WriteAllText("C:\\maq\\" + main1.text + "\\" + main1.text + ".txt", "\n");
            System.IO.File.WriteAllLines("C:\\maq\\" + main1.text + "\\" + main1.text + ".txt", input_lines_list);
 }

推荐答案

你不能只关闭它。您需要找出问题的根源并进行排序。有关更多详细信息和一些实用建议,请参阅我之前的回答:使用的文件另一个流程异常 [ ^ ]。



-SA
You cannot just close it. You need to find out the root of the problem and sort it out. For further detail and some practical recommendations, please see my past answer: File used by another process exception[^].

—SA


这篇关于如何关闭其他进程正在使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆