none
C#: Having trouble closing and releasing FileStream and StreamWriter... Purple Squiggly? RRS feed

  • Question

  • I'm not sure what the purple line beneath the "close()" statements mean other than "there is an issue and we don't know what it is?"

    I thought it may be centered around closing or disposing. I'm tried both and I cannot seem to get it to clear. I need for the file text file to completely be released so, if the user need to rerun the application, it will recreate the file again.  I do get a "file in use error" when I try that, so I'm not sure what I'm doing wrong in releasing the objects???

            private void Loading()
            {
                int t = 1;
                DateTime dt = DateTime.Now.AddDays(Double.Parse("-" + range));
                if (File.Exists(@"C:\Temp\DSU.txt"))
                    File.Delete(@"C:\Temp\DSU.txt");
                FileStream DSU = new FileStream(@"C:\Temp\DSU.txt", FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(DSU);
                foreach (string line in scripts)
                {
                    string[] listValues = line.Split(',');
                    ListViewItem values = new ListViewItem(listValues);
                    
                    if (t == 1)
                    {
                        AddColumn("Script Name", 200);      // Creates column headings
                        AddColumn("Date and Time", 150);
                        AddColumn("SID", 75);
                        AddColumn("Environment", 75);
                        AddColumn("Client", 75);
                        sw.WriteLine("Script Name,Date and Time,SID,Enviroment,Client,UserIP");
                        t++;
                    }
                    else
                    {
                        if ((values.Text != "") && (values.Text != "Script Name"))
                            if((DateTime.Parse(listValues[1]) >= dt))
                            {
                                AddItem(values);
                                sw.WriteLine(line);
                                if (!dictScript.Contains(values.Text))
                                {
                                    dictScript.Add(values.Text);
                                    AddScript(values.Text);
                                }
                            }
                    }
                    
                }
                sw.Close();
                DSU.Close();
            }



    SV

    Thursday, January 29, 2015 8:47 PM

Answers

  • Using is your friend.  If you were wanting to use close, then you really should have put this whole thing in a try finally block, where you close your objects in the finally block.  By using using, pun intended, it will close and dispose of the resource for you no matter what happens. Here is my modified code, hope that it helps you.

      private void Loading()
      {
       int t = 1;
       DateTime dt = DateTime.Now.AddDays(Double.Parse("-" + range));
       if (File.Exists(@"C:\Temp\DSU.txt"))
        File.Delete(@"C:\Temp\DSU.txt");
       using (FileStream DSU = new FileStream(@"C:\Temp\DSU.txt", FileMode.Create, FileAccess.Write))
       {
        using (StreamWriter sw = new StreamWriter(DSU))
        {
         foreach (string line in scripts)
         {
          string[] listValues = line.Split(',');
          ListViewItem values = new ListViewItem(listValues);
    
          if (t == 1)
          {
           AddColumn("Script Name", 200);      // Creates column headings
           AddColumn("Date and Time", 150);
           AddColumn("SID", 75);
           AddColumn("Environment", 75);
           AddColumn("Client", 75);
           sw.WriteLine("Script Name,Date and Time,SID,Enviroment,Client,UserIP");
           t++;
          }
          else
          {
           if ((values.Text != "") && (values.Text != "Script Name"))
            if ((DateTime.Parse(listValues[1]) >= dt))
            {
             AddItem(values);
             sw.WriteLine(line);
             if (!dictScript.Contains(values.Text))
             {
              dictScript.Add(values.Text);
              AddScript(values.Text);
             }
            }
          }
         }
        }
    
       }
      }
    


    Of course there are cleaner ways to create a STreamWriter, but I chose to stick with your code as closely as possible.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.


    • Edited by TSoftware-Old Thursday, January 29, 2015 9:48 PM Added another using
    • Marked as answer by svMSDN Friday, January 30, 2015 12:25 PM
    Thursday, January 29, 2015 9:43 PM

All replies

  • Using is your friend.  If you were wanting to use close, then you really should have put this whole thing in a try finally block, where you close your objects in the finally block.  By using using, pun intended, it will close and dispose of the resource for you no matter what happens. Here is my modified code, hope that it helps you.

      private void Loading()
      {
       int t = 1;
       DateTime dt = DateTime.Now.AddDays(Double.Parse("-" + range));
       if (File.Exists(@"C:\Temp\DSU.txt"))
        File.Delete(@"C:\Temp\DSU.txt");
       using (FileStream DSU = new FileStream(@"C:\Temp\DSU.txt", FileMode.Create, FileAccess.Write))
       {
        using (StreamWriter sw = new StreamWriter(DSU))
        {
         foreach (string line in scripts)
         {
          string[] listValues = line.Split(',');
          ListViewItem values = new ListViewItem(listValues);
    
          if (t == 1)
          {
           AddColumn("Script Name", 200);      // Creates column headings
           AddColumn("Date and Time", 150);
           AddColumn("SID", 75);
           AddColumn("Environment", 75);
           AddColumn("Client", 75);
           sw.WriteLine("Script Name,Date and Time,SID,Enviroment,Client,UserIP");
           t++;
          }
          else
          {
           if ((values.Text != "") && (values.Text != "Script Name"))
            if ((DateTime.Parse(listValues[1]) >= dt))
            {
             AddItem(values);
             sw.WriteLine(line);
             if (!dictScript.Contains(values.Text))
             {
              dictScript.Add(values.Text);
              AddScript(values.Text);
             }
            }
          }
         }
        }
    
       }
      }
    


    Of course there are cleaner ways to create a STreamWriter, but I chose to stick with your code as closely as possible.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.


    • Edited by TSoftware-Old Thursday, January 29, 2015 9:48 PM Added another using
    • Marked as answer by svMSDN Friday, January 30, 2015 12:25 PM
    Thursday, January 29, 2015 9:43 PM
  • BTW, if you hover your mouse over the squiggly, a tooltip should pop up to show you what it is complaining about.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by svMSDN Friday, January 30, 2015 12:25 PM
    • Unmarked as answer by svMSDN Friday, January 30, 2015 12:25 PM
    Thursday, January 29, 2015 10:00 PM
  • TSoftware:

    That worked great! Thanks for taking the time to edit and post the code. Your suggestions on this project have been a great help.

    The using statement crossed my mind, and I'm really not sure why I didn't attempt it since I know how to use it. I believe I was just thinking it would interfere with the Async/Await thread this was happening on, but I guess it would have been on the same thread and wouldn't have failed. Now if I can resolve the flickering I will be in business with this app.

    Thanks again, especially with going above and beyond with posting the solution.


    SV


    • Edited by svMSDN Friday, January 30, 2015 12:30 PM Edited for clarification
    Friday, January 30, 2015 12:29 PM
  • Glad I could be of assistance...Happy Coding!!

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Friday, January 30, 2015 2:14 PM