Solved

Close a text file in c#

Posted on 2013-12-03
2
409 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
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
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
Comment Utility
thanks so much i knew it was something silly
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now