Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
url = string in my propities.settings.defult

my save code

C#
private void button1_Click(object sender, EventArgs e)
   {
       openFileDialog1.Filter = "txt files (*.txt)|*.txt";
       if (openFileDialog1.ShowDialog() == DialogResult.OK)
       {
           label2.Text = "File Open: " + openFileDialog1.FileName;
       Properties.Settings.Default.url = openFileDialog1.FileName;
       Properties.Settings.Default.Save();


       StreamReader objstream = new StreamReader(Properties.Settings.Default.url);
       richTextBox1.Text = objstream.ReadToEnd();

       }


this is my write code to edit and save the document
C#
private void richTextBox1_DoubleClick(object sender, EventArgs e)
      {
        File.WriteAllText(Properties.Settings.Default.url, richTextBox1.Text);
      }



when I double click the richtextbox after typing stuff, I get this error
This is my error

The process cannot access the file 'C:\Users\Kyle\Desktop\delete this test.txt' because it is being used by another process.

and this file infact to being used by any other program or it open, it is just open through the program. And I want the save to happen with-out having to look for the file again
Posted
Updated 1-Jun-12 1:17am
v2

Hi =)
First of all why you didn't close your stream after read a file ?)
C#
private void button1_Click(object sender, EventArgs e)
   {
       openFileDialog1.Filter = "txt files (*.txt)|*.txt";
       if (openFileDialog1.ShowDialog() == DialogResult.OK)
       {
           label2.Text = "File Open: " + openFileDialog1.FileName;
       Properties.Settings.Default.url = openFileDialog1.FileName;
       Properties.Settings.Default.Save();


       StreamReader objstream = new StreamReader(Properties.Settings.Default.url);
       richTextBox1.Text = objstream.ReadToEnd();

????????

       }

After reading you must invoke Close method on objstream member.
Or it would be better if you incorporate stream initialization in using scope.

C#
private void button1_Click(object sender, EventArgs e)
   {
       openFileDialog1.Filter = "txt files (*.txt)|*.txt";
       if (openFileDialog1.ShowDialog() == DialogResult.OK)
       {
           label2.Text = "File Open: " + openFileDialog1.FileName;
       Properties.Settings.Default.url = openFileDialog1.FileName;
       Properties.Settings.Default.Save();


       using(StreamReader objstream = new StreamReader(Properties.Settings.Default.url)){
       richTextBox1.Text = objstream.ReadToEnd();
}

       }
 
Share this answer
 
v2
Try using the SaveFileDialog. http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx[^] The OpenFileDialogue opens the file and this is why you are getting an error saying its being used!
 
Share this answer
 
Comments
Oleksandr Kulchytskyi 1-Jun-12 7:26am    
Are you sure that The OpenFileDialogue opens the file ?))
As far as i know, it just return path of picked file.
The problem is not in that.
Better look how he is using StreamReader object.
[no name] 2-Jun-12 9:54am    
the problem is the saving I get the error, and I even try the other save methods, i get the same error
Oleksandr Kulchytskyi 2-Jun-12 10:29am    
Yes, of course you will get an error if you performs doubleclick on richtextbox1.
If we will follow your logic, in event handler on button1 click, you create streamreader object,with help of which you read the content to richtextBox1, and forget to close file handle.

So, when you tries to save data, you will get an error, because the file handle
on which you tries to perform save action wiil fail , because it had already been opened. so as a result do not forget to close a handle on opened streams!!!!
[no name] 2-Jun-12 21:34pm    
how do i close it?
Oleksandr Kulchytskyi 3-Jun-12 3:18am    
Man, i already did it in Solution 1 , look at second code snippet!!
As you can see , i wrapped yours StreamReader objstream in scope of using(..){...} . After performing file reading, streamreader will automatically close file handle.

Read carefully about using on MSDN http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx.

Also i could add that, if you disassemble my code, you will find,
that using observed in next manner

StreamReader objstream=null;
try
{
objstream=new new StreamReader(Properties.Settings.Default.url);
richTextBox1.Text = objstream.ReadToEnd();
}
finally
{
objstream.Dispose(); //in this case Dispose method responsible for closing file handle.
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900