首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#读取和保存文本文件时怎么保持字体大小

2012-01-16 
C#读取和保存文本文件时如何保持字体大小正在用c#做记事本的功能,但读取和保存文件时总是不能保持修改后的

C#读取和保存文本文件时如何保持字体大小
正在用c#做记事本的功能,但读取和保存文件时总是不能保持修改后的字体大小。请教高手,有什么方法?以下是我的读取和保存的代码:
------------------------------------------------------

                private   void   fileOpen_Click(object   sender,   EventArgs   e)
                {
                        SaveFileDialog   filedlg   =   new   SaveFileDialog();
                        filedlg.Filter   =   "Text   Document(*.txt)|*.txt|All   Files(*.*)|*.* ";
                        filedlg.Title   =   "打开文件 ";
                        filedlg.ValidateNames   =   true;
                        try
                        {
                                if   (filedlg.ShowDialog()   ==   DialogResult.OK)
                                {
                                        //StreamReader   sr   =   new   StreamReader(filedlg.FileName,System.Text.Encoding.Default);
                                        FileStream   fs   =   new   FileStream(filedlg.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);
                                        StreamReader   sr   =   new   StreamReader(fs);
                                        ArrayList   buffer   =   new   ArrayList();
                                        //从文件中读取内容并赋给显示文件内容对象框
                                        while   (sr.Peek()   !=   -1)
                                        {
                                                buffer.Add(sr.ReadLine());       //一次读取一行数据
                                        }
                                        newForm   child2   =   new   newForm();
                                        child2.MdiParent   =   this;
                                        child2.textBox.Lines   =   (String[])buffer.ToArray(Type.GetType( "System.String "));


                                        sr.Close();
                                        child2.Text   =   filedlg.FileName;
                                        child2.textBox.Select(0,   0);
                                        child2.Show();

                                }
                        }
                        catch(Exception   ex)
                        {
                                MessageBox.Show(this,ex.Message, "打开文件错误 ",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        }
                }

                private   void   fileAnother_Click(object   sender,   EventArgs   e)
                {
                        //另存为
                        SaveFileDialog   sfd   =   new   SaveFileDialog();
                        sfd.CheckFileExists   =   true;
                        sfd.Filter   =   "Text   Document(*.txt)|*.txt|All   Files(*.*)|*.* ";
                        sfd.Title   =   "打开文件 ";
                        sfd.ValidateNames   =   true;
                        if   (sfd.ShowDialog()   ==   DialogResult.OK)
                        {
                                string   fileName   =   sfd.FileName;
                                SaveFile(fileName);
                        }

                }
                protected   void   SaveFile(string   fileName)
                {
                        try
                        {
                                Stream   stream   =   File.OpenWrite(fileName);


                                using   (StreamWriter   writer   =   new   StreamWriter(stream))
                                {
                                        writer.Write(this.ActiveMdiChild.ActiveControl.Text);
                                        this.ActiveMdiChild.Text   =   fileName;
                                }
                                stream.Close();
                        }
                        catch   (IOException   ex)
                        {
                                MessageBox.Show(ex.Message,   "SimpleText ",   MessageBoxButtons.OK,   MessageBoxIcon.Exclamation);
                        }
                }

--------------------

谢谢指教!


[解决办法]
文本没有字体大小,所谓的字体大小只是编辑器显示的问题

热点排行