| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 803 人关注过本帖
标题:各位,我在用C#写记事本的时候出现了个问题,求教各位,万分感激!
只看楼主 加入收藏
浩翔云宇
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-11-11
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
各位,我在用C#写记事本的时候出现了个问题,求教各位,万分感激!
各位,我在看视频教程学开发记事本程序的时候发现一个问题,目前简单的实现了“新建”、“打开”、“保存”、“另存为”功能,但发现有个乱码问题,就是在运行程序后编辑了一段文本后另存为保存在一个地址中,在用程序打开刚才的那保存的TXT文件就全都是乱码,打开不是用程序另存为的TXT文件就不会出现这种情况,还请各位赐教。下面附上源码:


using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using


namespace Notepad
{
    public partial class FormMain : Form
    {
        string currentFileName = "新建文本文档.txt";
        public FormMain()
        {
            InitializeComponent();
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("test123");
            this.richTextBox1.Text = "";
            using (StreamWriter sw = new StreamWriter(currentFileName))
            {
               
            }
        }

        /// <summary>
        /// read file
        /// </summary>
        /// <param name="file">file name</param>
        void openFile(string file)
        {
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(file, Encoding.GetEncoding(0)))
                {
                    string text = "";
                    String line;
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        text += line + "\n";
                    }
                    this.richTextBox1.Text = text;
                }
            }
            catch (Exception ex)
            {
                // Let the user know what went wrong.
                //Console.WriteLine("The file could not be read:");
                //Console.WriteLine(e.Message);
                MessageBox.Show(ex.Message);
            }
        }
        void writeFile(string file)
        {
            using (StreamWriter sw = new StreamWriter(file))
            {
                foreach (string s in this.richTextBox1.Lines)
                {
                    sw.WriteLine(s);
                }
            }

        }
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            DialogResult dr = this.openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                this.currentFileName = this.openFileDialog1.FileName;
                openFile(this.openFileDialog1.FileName);
            }
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.saveFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                this.currentFileName = this.openFileDialog1.FileName;
                writeFile(this.saveFileDialog1.FileName);
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            writeFile(this.currentFileName);
        }
    }
}
搜索更多相关主题的帖子: 困扰 
2009-07-30 19:16
mycsy
Rank: 2
等 级:论坛游民
帖 子:147
专家分:14
注 册:2009-2-6
收藏
得分:14 
把 读写编码都改成默认即可!
2009-07-30 21:00
浩翔云宇
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-11-11
收藏
得分:0 
using (StreamReader sr = new StreamReader(file, Encoding.GetEncoding(0)))
其他的怎么改啊
2009-08-01 11:10
ginpq
Rank: 2
等 级:论坛游民
帖 子:34
专家分:50
注 册:2007-8-21
收藏
得分:0 
读时使用:richTextBox1.LoadFile(FileName,RichTextBoxStreamType.PlainText );
写时使用:richTextBox1.SaveFile(Filename,RichTextBoxStreamType.PlainText);

(参考https://bbs.bccn.net/thread-73095-1-3.html)
2009-08-01 13:19
快速回复:各位,我在用C#写记事本的时候出现了个问题,求教各位,万分感激!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031776 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved