Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Close a text file in c#

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

719 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