Solved

Close a text file in c#

Posted on 2013-12-03
2
437 Views
Last Modified: 2013-12-03
Hi all
i have a text file that needs to be reused in my app, what is does it reads in a rss writes to a text file and then searches that same text file, now this works fine on the first lop but it is connected to a timer that run this every hour, on the 2nd loop i get The process cannot access the file 'C:\adam\test.txt' because it is being used by another process.

I have tried dispose but this gives another error

here is my code how do i reuse the text file over and over again, i dont want it appended
  using (StreamWriter writer = new StreamWriter(@"C:\adam\test.txt",false))
            {
                StringBuilder line = new StringBuilder();
                foreach (ListViewItem item in listView1.Items)
                {
                    line.Clear();
                    for (int i = 0; i < item.SubItems.Count; i++)
                    {
                        if (i > 0)
                            line.Append(",");
                        line.Append(item.SubItems[i].Text);
                    }
                    writer.WriteLine(line);
                   // MessageBox.Show("Saved");
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();  
            
            
            StreamReader s = new StreamReader(@"C:\adam\test.txt");
            string currentLine;
            string searchString = "Ch4 Restoration Man Series 2";
            bool foundText = false;

            do
            {
               currentLine = s.ReadLine();
               
                if (currentLine != null && currentLine.Contains(searchString))
                {
                  //  foundText = currentLine.Contains(searchString);
                    
                   
                   string file = currentLine;

                    string Title = file.Split(',')[0].Trim();
                    string link = file.Split(',')[1].Trim();
                    string pubdate = file.Split(',')[2].Trim();



                    ListViewItem lvi = new ListViewItem(Title);
                    lvi.SubItems.Add(link);
                    lvi.SubItems.Add(pubdate);
                    listView1.Items.Add(lvi);

                    
                  

                    
                }
                
            }
            while (currentLine != null && !foundText);

            if (foundText)
            {
               
                
            
               
           //     textBox2.Text = (Title);
           //     textBox3.Text = (link);
             //   textBox4.Text = (pubdate);
            }

            else
            {
                MessageBox.Show("Title" + " " + textBox1.Text + " " + "not found");

            }

            
 private void button1_Click(object sender, EventArgs e)
        {
            
        

            
            
            
            using (StreamWriter writer = new StreamWriter(@"C:\adam\test.txt",false))
            {
                StringBuilder line = new StringBuilder();
                foreach (ListViewItem item in listView1.Items)
                {
                    line.Clear();
                    for (int i = 0; i < item.SubItems.Count; i++)
                    {
                        if (i > 0)
                            line.Append(",");
                        line.Append(item.SubItems[i].Text);
                    }
                    writer.WriteLine(line);
                   // MessageBox.Show("Saved");
                }
            }
        }

        }

Open in new window

0
Comment
Question by:awolarczuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 39694311
Add "writer.Close()" line to here:
using (StreamWriter writer = new StreamWriter(@"C:\adam\test.txt",false))
            {
                StringBuilder line = new StringBuilder();
                foreach (ListViewItem item in listView1.Items)
                {
                    line.Clear();
                    for (int i = 0; i < item.SubItems.Count; i++)
                    {
                        if (i > 0)
                            line.Append(",");
                        line.Append(item.SubItems[i].Text);
                    }
                    writer.WriteLine(line);
                   // MessageBox.Show("Saved");
                }
            writer.Close();
            }

Open in new window

And "s.Close()" to here:
       private void button5_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();  
            
            
            StreamReader s = new StreamReader(@"C:\adam\test.txt");
            string currentLine;
            string searchString = "Ch4 Restoration Man Series 2";
            bool foundText = false;

            do
            {
               currentLine = s.ReadLine();
               
                if (currentLine != null && currentLine.Contains(searchString))
                {
                  //  foundText = currentLine.Contains(searchString);
                    
                   
                   string file = currentLine;

                    string Title = file.Split(',')[0].Trim();
                    string link = file.Split(',')[1].Trim();
                    string pubdate = file.Split(',')[2].Trim();



                    ListViewItem lvi = new ListViewItem(Title);
                    lvi.SubItems.Add(link);
                    lvi.SubItems.Add(pubdate);
                    listView1.Items.Add(lvi);

                    
                  

                    
                }
                
            }
            while (currentLine != null && !foundText);
            s.Close();

            if (foundText)
            {
               
                
            
               
           //     textBox2.Text = (Title);
           //     textBox3.Text = (link);
             //   textBox4.Text = (pubdate);
            }

            else
            {
                MessageBox.Show("Title" + " " + textBox1.Text + " " + "not found");

            }

Open in new window

0
 

Author Comment

by:awolarczuk
ID: 39694571
thanks so much i knew it was something silly
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question