Mike G
Mike G

Reputation: 11

c# how to end streamreader

I am doing a project Windows form for assignment in Uni, I want to search an already created text file to match a first name and last name then write some additional information if the name and last name exist. I have the code constructed and showing no errors, however when I run and attempt to add information I am being provided with an error which essentially says the next process (Streamreader writer can not access the file as it is already in use by another process) I assume this process is streamreader, I have tried to code it to stop reading to no avail. I am in my first 3 months learning coding and would appreciate some assistance if possible, I have put a snippet of my code below.

//check if there is a file with that name
        if (File.Exists(sFile))
        {

            using (StreamReader sr = new StreamReader(sFile))
            {
                //while there is more data to read
                while (sr.Peek() != -1)
                {
                    //read first name and last name
                    sFirstName = sr.ReadLine();
                    sLastName = sr.ReadLine();
                }
                {
                    //does this name match?
                    if (sFirstName + sLastName == txtSearchName.Text)

                        sr.Close();
                }

                //Process write to file
                    using (StreamWriter sw = new StreamWriter(sFile, true))

                        {
                            sw.WriteLine("First Name:" + sFirstName);
                            sw.WriteLine("Last Name:" + sLastName);
                            sw.WriteLine("Gender:" + sGender);
                        }

Upvotes: 1

Views: 4420

Answers (4)

Wheels73
Wheels73

Reputation: 2890

As per my comment regarding the using statement. Rearrange to the below. I've tested locally and it seems to work.

   using (StreamReader sr = new StreamReader(sfile))
   {
        //while there is more data to read
        while (sr.Peek() != -1)
        {
            //read first name and last name
           sFirstName = sr.ReadLine();
           sLastName = sr.ReadLine();

             //does this name match?
            if (sFirstName + sLastName == txtSearchName.Text)
                break;
        }
     }

    using (StreamWriter sw = new StreamWriter(sfile, true))
    {
        sw.WriteLine("First Name:" + sFirstName);
        sw.WriteLine("Last Name:" + sLastName);
        sw.WriteLine("Gender:" + sGender);
    }

I've replaced the sr.Close with a break statement to exit out. Closing the reader causes the subsequent peek to error as it's closed.

Also, I've noticed that you are not setting gender? unless its set elsewhere. hope that helps

Upvotes: 1

blaze_125
blaze_125

Reputation: 2317

I think this is what you want/need. You can't append to a file the way you are trying to do it. Instead you'll want to read your input file, and write a temp file as you are reading through. And, whenever your line matches your requirements, then you can write the line with your modifications.

        string inputFile = "C:\\temp\\StreamWriterSample.txt";
        string tempFile = "C:\\temp\\StreamWriterSampleTemp.txt";

        using (StreamWriter sw = new StreamWriter(tempFile))//get a writer ready
        {
            using (StreamReader sr = new StreamReader(inputFile))//get a reader ready
            {
                string currentLine = string.Empty;
                while ((currentLine = sr.ReadLine()) != null)
                {
                    if (currentLine.Contains("Clients"))
                    {
                        sw.WriteLine(currentLine + " modified");
                    }
                    else
                    {
                        sw.WriteLine(currentLine);
                    }
                }
            }
        }

        //now lets crush the old file with the new file
        File.Copy(tempFile, inputFile, true);

Upvotes: 0

Farzin Kanzi
Farzin Kanzi

Reputation: 3435

You can use FileStream. It gives you many options to work with file:

var fileStream = new FileStream("FileName", FileMode.Open,
 FileAccess.Write, FileShare.ReadWrite);


var fileStream = new FileStream("fileName", FileMode.Open, 
FileAccess.ReadWrite, FileShare.ReadWrite);

Upvotes: 0

Tobias Theel
Tobias Theel

Reputation: 3217

You are using your writer inside the reader, using the same file. A using disposes the object inside it, after the closing curly braces.

 using(StreamReader reader = new StreamReader("foo")){
 //... some stuff
   using(Streamwriter writer = new StreamWriter("foo")){
   }
 }

Do it like so :

 using(StreamReader reader = new StreamReader("foo")){
 //... some stuff 
 }

 using(Streamwriter writer = new StreamWriter("foo")){
 }

Upvotes: 1

Related Questions