Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Close a text file in c#

Posted on 2013-12-03
2
Medium Priority
?
450 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 25

Accepted Solution

by:
chaau earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

886 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