C#我的程序阻止我保存文档? [英] C# my program is stopping me from saving the document?

查看:91
本文介绍了C#我的程序阻止我保存文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

url =我的propies.settings.defult中的字符串

我的保存代码

url = string in my propities.settings.defult

my save code

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

private void richTextBox1_DoubleClick(object sender, EventArgs e)
      {
        File.WriteAllText(Properties.Settings.Default.url, richTextBox1.Text);
      }




在输入内容后双击RichTextBox时,出现此错误
这是我的错误

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

并且此文件实际上不被任何其他程序使用或已打开,它只是通过该程序打开的.而且我希望保存发生而不必再次查找文件




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

推荐答案

Hi =)
首先,为什么您在读取文件后没有关闭流?)
Hi =)
First of all why you didn''t close your stream after read a file ?)
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();

????????

       }


阅读后,必须在objstream成员上调用Close方法.
或者如果在使用范围中合并流初始化会更好.


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

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();
}

       }


尝试使用SaveFileDialog. http://msdn.microsoft.com/en-us/library/system. windows.forms.savefiledialog.aspx [
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!


这篇关于C#我的程序阻止我保存文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆