Reputation: 3297
I'm writing a little program, which displays my to do's, the priority etc. In this program I got 2 tabs: tab 1 is for create a new to do and the other tab is for displaying the current to do's. So when I put in some text into a textbox in tab 1 and after I click a "Save" button, it should save it in the tab2's combobox. And when I restart the program, it should still be saved. Google told me to do this with a file so I can save it into a file. And I found a short code, which I adjusted for my program. Here you can see my code:
private void Form1_Load(object sender, EventArgs e)
{
// Check if directory exists
if (Directory.Exists(@"C:\Users\rs\Desktop\Test\)"))
{
// Do nothing
}
else
{
Directory.CreateDirectory(@"C:\Users\rs\Desktop\Test\");
}
if (File.Exists(@"C:\Users\rs\Desktop\Test\test.txt"))
{
// Do nothing
}
else
{
File.Create(@"C:\Users\rs\Desktop\Test\test.txt");
}
StreamReader sr = new StreamReader(@"C:\Users\rs\Desktop\Test\test.txt");
while (sr.Peek() >= 0)
{
combox_Name2.Items.Add(sr.ReadLine());
}
sr.Close();
}
private void btn_Save_Click(object sender, EventArgs e)
{
StreamWriter writer = new StreamWriter(@"C:\Users\rs\Desktop\Test\test.txt", true);
try
{
Process.Start(combox_Name2.Text, txt_Name.Text);
if (combox_Name2.Items.Contains(txt_Name.Text))
{
// Do nothing
}
else
{
writer.WriteLine(combox_Name2.Text);
writer.Flush();
writer.Dispose();
combox_Name2.Items.Add(txt_Name.Text);
}
}
catch
{
MessageBox.Show("The file '" + txt_Name.Text + "' could not be located", "File could not be located");
}
}
And my problem now is: Everytime I start the program, put some text into the textbox and click on save button, there comes an error on line
StreamReader sr = new StreamReader(@"C:\Users\rs\Desktop\Test\test.txt");
VS2012 say that the process can't get on C:\Users\rs\Desktop\Test\test.txt
because it's already started in another process.
Can someone give me a hint?
Cheers
Upvotes: 1
Views: 2168
Reputation: 832
Following code will achieve your objective. Do mark as answer if it works.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace FileHandling
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Check if directory exists
if (Directory.Exists(@"C:\Users\rs\Desktop\Test\)"))
{
// Do nothing
}
else
{
Directory.CreateDirectory(@"C:\Users\rs\Desktop\Test\");
}
if (File.Exists(@"C:\Users\rs\Desktop\Test\test.txt"))
{
// Do nothing
}
else
{
File.Create(@"C:\Users\rs\Desktop\Test\test.txt");
}
using (StreamReader writer = new StreamReader(@"C:\Users\rs\Desktop\Test\test.txt"))
{
while (writer.Peek() >= 0)
{
combox_Name2.Items.Add(writer.ReadLine());
}
writer.Close();
}
}
private void btn_Save_Click(object sender, EventArgs e)
{
using (StreamWriter writer = new StreamWriter(@"C:\Users\rs\Desktop\Test\test.txt", true))
{
try
{
if (combox_Name2.Items.Contains(txt_Name.Text))
{
MessageBox.Show("The task '" + txt_Name.Text + "' already exist in list", "Task already exists");
}
else
{
combox_Name2.Items.Add(txt_Name.Text);
writer.WriteLine(txt_Name.Text);
writer.Flush();
writer.Dispose();
}
}
catch
{
MessageBox.Show("The file '" + txt_Name.Text + "' could not be located", "File could not be located");
}
}
}
}
}
Upvotes: 2
Reputation: 22038
You forgot to close/dispose the writer. This also counts for the reader. You should get used to write it like this:
using(StreamWriter writer = new StreamWriter(@"C:\Users\rs\Desktop\Test\test.txt", true))
{
// do your thing here.
}
This will close the stream at all time even when an exception is thrown inside.
This will be compiled like this:
StreamWriter writer = new StreamWriter(@"C:\Users\rs\Desktop\Test\test.txt", true);
try
{
// do your thing here.
}
finally
{
writer.Dispose();
}
The actual problem lies here: File.Create(@"C:\Users\rs\Desktop\Test\test.txt");
The file is created, but you didn't referenced the result. Declaration public static FileStream Create(
string path
)
This will send the result (FileStream) into the garbage and is disposed when the garbage collector collects it. You don't know when is happens. The file will remain open until it's garbagecollected. You can remove the whole line. If the file doesn't exists, don't execute the StreamReader part.
combox_Name2.Items.Clear();
if (File.Exists(@"C:\Users\rs\Desktop\Test\test.txt"))
{
using(StreamReader sr = new StreamReader(@"C:\Users\rs\Desktop\Test\test.txt"))
{
while (sr.Peek() >= 0)
{
combox_Name2.Items.Add(sr.ReadLine());
}
}
}
Do not forget to remove the ! at the File.Exists.
Upvotes: 1
Reputation: 7105
Always place objects that implements the IDisposable interface within
using(IDisposable object goes here)
{
// Do something.
}
In your case the StreamWriter object.
Upvotes: 1
Reputation: 17003
StreamWriter block you to do that you have to close it before using the file in the process or you have to do the changes in string and than when on process exist update the changes in to the file.
Upvotes: 1