none
Another thread of my program is using a text file so my program throws expection RRS feed

  • Question

  • In one class I have this code:

    Class1{
    public static string reader = File.ReadAllText("rids.txt");
            public static StreamWriter rids = new StreamWriter("rids.txt");
            public static StreamWriter writer = new StreamWriter("ids.txt");
    
    
    ...
    
     writer.WriteLine(justid + "|" + key + "|" + mail);
                                writer.Flush();
                                rids.WriteLine(id);
                                rids.Flush();
                                Console.WriteLine(id);
    }

    In second class here is my code:

                int lineno = File.ReadLines("ids.txt").Count();
                string[] lines = File.ReadAllLines("ids.txt"); 

    Error I get is:

    The proccess can not access the file somepath because it is being used by another proccess

    Help

    Tuesday, April 2, 2019 3:02 PM

Answers

  • Hi

    Thank you for posting here.

    For your question, you want to solve the error you got.

    I have reproduced your problem, you could try to add two sentences in the right place.

    Code:               

                    writer.WriteLine(justid + "|" + key + "|" + mail);
                    writer.Flush();
                    rids.WriteLine(id);
                    rids.Flush();
                    writer.Close();
                    rids.Close();
                    Console.WriteLine(id);
    


                 

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, April 3, 2019 6:14 AM
    Moderator

All replies

  • Try this:

    public static FileStream fsw = new FileStream( "ids.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite );
    public static StreamWriter writer = new StreamWriter( fsw );
     
    . . .
    
    // Reading lines:
     
    using( FileStream fsr = new FileStream( "ids.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite ) )
    {
        using( StreamReader reader = new StreamReader( fsr ) )
        {
            string line;
            List<string> lines = new List<string>();
            while( ( line = reader.ReadLine() ) != null ) lines.Add( line );
            int lineno = lines.Count;
     
            // . . .
        }
    }
    

    Tuesday, April 2, 2019 5:39 PM
  • Hi

    Thank you for posting here.

    For your question, you want to solve the error you got.

    I have reproduced your problem, you could try to add two sentences in the right place.

    Code:               

                    writer.WriteLine(justid + "|" + key + "|" + mail);
                    writer.Flush();
                    rids.WriteLine(id);
                    rids.Flush();
                    writer.Close();
                    rids.Close();
                    Console.WriteLine(id);
    


                 

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, April 3, 2019 6:14 AM
    Moderator