Solved

Close a text file in c#

Posted on 2013-12-03
2
419 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
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

770 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