C#记事本自定义类(保存,统计汉字,查找,TreeView调用)

自己写的一个记事本类,都是静态方法,感觉静态方法好用

至于诸君是否看得头晕目眩,小弟就无能为力了.这只是为以后方便自己找到这个类罢了.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace Mxdr_NotBook
{
    /// <summary> 〖新建-打开-保存〗〖统计字数〗〖~隐藏-只读〗文档 Class <para></para>
    /// TreeView 加载、添加、删除--节点文档</summary>
    public static class MxdrSave
    {
        #region =★=*=★=*=★= 自定义全局变量 =★=*=★=*=★=
        /// <summary> -- txtName 用于储存文件名 -- </summary>
        private static string txtName = "";
        /// <summary> -- txtName 用于储存文件路径 -- </summary>
        public static string filePath = "";
        /// <summary> -- 判断是否为打开的文件,true为真 -- </summary>
        public static bool FbOpen = false;
        /// <summary> -- 判断是否为第一次打开的文件,true为真 -- </summary>
        public static bool FbOpens = false;
        /// <summary> -- 判断文本是否未保存,true为保存 -- </summary>
        public static bool FbSave = true;
        /// <summary> -- 判断文本内容是否改变,true为改变 -- </summary>
        public static bool FbRiSave = false;
        /// <summary> 设置文件或文件夹的(隐藏与只读)属性 </summary>
        //roHidDirFile roHidDirfile = new roHidDirFile();

        #endregion

        #region =★=*=★=*=★= 自定义委托事件 =★=*=★=*=★=
        /* 在主窗体中实现委托 */
        /// <summary> 清空RichTextBox内容委托事件 </summary>
        public static event NullRitxtbox Nulltxt;
        /// <summary> 加载文本内容委托事件 </summary>
        public static event Ritxtbox readFile;
        /// <summary> 保存文本文档委托事件 </summary>
        public static event SaveFileHandler FileSaves;
        / <summary> 另存为文本文档委托事件 </summary>
        //public event SaveFileHandler FilesSaveAs;
        //public event SaveFileHandler SaveAseFile;
        #endregion

        /**=**=**=**=**=**=**=**=**=***=**=**=**=**=**/

        #region =★=*=★=*=★= 获取光标行列 =★=*=★=*=★=
        /// <summary> 获取光标行列<para> <para>通过移动鼠标和
        /// 方向键获取坐标位置</para></para><para> <para>注:建议用文本的 KeyUp 和
        /// MouseUp 事件调用方法</para></para> </summary>
        /// <param name="rbox">获取坐标的 RichTextBox 文本</param>
        /// <param name="str">用于向用户显示行列</param>
        public static void Ranks(RichTextBox rbox, ToolStripStatusLabel toolStripStatusLabel)
        {
            /* 光标行第一个字符索引 */
            int index = rbox.GetFirstCharIndexOfCurrentLine();
            /* 获取光标行的行号 (+1)索引是从0开始的 */
            int line = rbox.GetLineFromCharIndex(index) + 1;
            /* 光标位置索引 - 当前行第一个索引 = 光标所在列(+1) */
            int cols = rbox.SelectionStart - index + 1;
            toolStripStatusLabel.Text = string.Format("行 {0} 列 {1}", line, cols);
        }
        #endregion

        #region =★=*=★=*=★= 统计文本字数数量 =★=*=★=*=★=
        /// <summary> 去除空格\n\r\t等符号,统计文本字数数量<para> <para>
        /// 参数1(tbox);RichTextBox 类型<para>参数2~4 参数类型ToolStripStatusLabel</para><para>
        /// 参数2(toLabel0);用于显示内容字节大小(KB/MB/GB)<para>
        /// 参数3(toLabel1);显示内容中符号,数字,字母,汉字数量<para>
        /// 参数4(toLabel2);内容字符数量统计(不含空格,\n\r\t等符号)
        /// </para></para></para></para></para></summary>
        /// <param name="tbox">RichTextBox 类型</param>
        /// <param name="toLabel0">用于显示内容字节大小(KB/MB/GB)</param>
        /// <param name="toLabel1">显示内容中符号,数字,字母,汉字数量</param>
        /// <param name="toLabel2">内容字符数量统计(不含空格,\n\r\t等符号)</param>
        public static void calculates(RichTextBox tbox, ToolStripStatusLabel toLabel0,
            ToolStripStatusLabel toLabel1, ToolStripStatusLabel toLabel2)
        {
            try
            {
                //RichTextBox tbox = form.RichTxtBox;
                bool skipSpace = true;
                int cn0 = 0, cn1 = 0, cn2 = 0, cn3 = 0, cn4 = 0, cn5 = 0;
                double cn6 = 0;
                if (tbox.Text.Length <= 0) FbSave = true;
                else if (tbox.Text != null)
                {
                    String sLine, ls = tbox.Text, lss = tbox.Text;
                    if (skipSpace)
                    {
                        sLine = ls.Replace(" ?", "").Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
                        cn0 += getWordsLength(sLine);   //字数数量(文本长度)
                        cn1 += getByteLength(sLine);    //字节数量(不含空格,\r\n\t等字符)
                        cn2 += getdigitalLength(sLine); //数字数量
                        cn3 += getcharLength(sLine);    //字母数量
                        cn4 = cn1 - cn0;                //汉字数量
                        cn5 = cn1 - cn2 - cn3 - cn4 * 2;//符号数量
                        cn6 = lss.Length + cn1 - cn0;
                        //cn6 = Math.Round(((double)(lss.Length + cn1 - cn0) / 1000), 2);
                        if (cn6 < 1000)
                        {
                            toLabel0.Text = string.Format("{0} 字节", cn6);
                        }
                        else if (cn6 >= 1000 && cn6 < 1000000)
                        {
                            double i = Math.Round(cn6 / 1000, 2);
                            toLabel0.Text = string.Format("{0} KB", i);
                        }
                        else if (cn6 >= 1000000 && cn6 < 1000000000)
                        {
                            double i = Math.Round((cn6 / (1000 * 1000)), 2);
                            toLabel0.Text = string.Format("{0} MB", i);
                        }
                        else if (cn6 >= 1000000000)
                        {
                            double i = Math.Round((cn6 / (1000 * 1000 * 1000)), 2);
                            toLabel0.Text = string.Format("{0} GB", i);
                        }
                        toLabel1.Text = string.Format("符号{0},字母{1},数字{2},汉字{3}", cn5, cn3, cn2, cn4);
                        toLabel2.Text = string.Format("{0}字", cn0);
                    }
                    //if(!FbOpens)
                    //FbSave = false;
                    //else FbSave = true;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }
        #endregion

        #region  =★*★*★=  统计〖字符〗 函数  =★*★*★=
        #region =★*★*★= 返回数字(0~9)数量 =★*★*★=
        /// <summary> 返回数字(0~9)数量<para> 
        /// <para>获得文本中所有含有数字数量</para></para> </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static int getdigitalLength(string s)
        {
            int lxs = 0;
            char[] str = s.ToCharArray();
            for (int i = 0; i < str.Length; i++)
            {
                if ((int)str[i] >= 48 && (int)str[i] <= 57)
                { lxs++; }
            }
            return lxs;
        }
        #endregion

        #region =★*★*★= 统计文本中的字母(A~Za~z)数量 =★*★*★=
        /// <summary> 统计文本中的字母(A~Za~z)数量 </summary>
        /// <param name="s">统计字数的文本框</param>
        /// <returns></returns>
        private static int getcharLength(string s)
        {
            int lxm = 0;
            char[] str = s.ToLower().ToCharArray();
            for (int i = 0; i < str.Length; i++)
            {
                if ((int)str[i] >= 97 && (int)str[i] <= 122)
                { lxm++; }
            }
            return lxm;
        }
        #endregion

        #region =★*★*★= 返回字数(文本长度) =★*★*★=
        /// <summary> 返回字数(文本长度) </summary>
        /// <param name="s"> 统计字数的文本框 </param>
        /// <returns></returns>
        private static int getWordsLength(string s)
        {
            if (s != null)
                return s.Length;
            else
                return 0;
        }
        #endregion

        #region =★*★*★= 返回字节数 =★*★*★=
        /// <summary> 返回字节数。<para> <para>计算双字节数量.</para></para><para> <para>
        /// 汉字结果+2,否则结果+1。
        /// </para></para> </summary>
        /// <param name="s">统计字数的文本</param>
        /// <returns></returns>
        private static int getByteLength(string s)
        {
            int lxh = 0;
            char[] str = s.ToCharArray();
            for (int i = 0; i < str.Length; i++)
            {
                if ((int)str[i] >= 0x4E00 && (int)str[i] <= 0x9FA5)
                {
                    lxh += 2;
                }
                else { lxh++; }
            }
            return lxh;
        }
        #endregion

        #region =★*★*★= 统计汉字数字 =★*★*★=
        /// <summary> 统计汉字数字 </summary>
        /// <param name="s">统计字数的文本</param>
        /// <returns></returns>
        private static int getByteLengths(string s)
        {
            int lxh = 0;
            char[] str = s.ToCharArray();
            for (int i = 0; i < str.Length; i++)
            {
                if ((int)str[i] >= 0x4E00 && (int)str[i] <= 0x9FA5)
                {
                    lxh += 2;
                }
            }
            return lxh;
        }

        #endregion
        #endregion


        /**=**=**=**=**=**=**=**=**=***=**=**=**=**=**/
        #region  =★*★*★= 〖新建-打开-保存〗〖复制-隐藏〗文档 =★*★*★=
        #region  =★*★*★= 新建文档 =★*★*★=
        /// <summary> 新建文档 </summary>
        /// <param name="rbox">RichTextBox 文本框</param>
        /// <param name="b">指示当前的文本是否属于打开的文档,true为打开false为新建文档</param>
        /// <param name="s">指示当前文档是否已经保存,true 为保存,false 未保存</param>
        public static void newFile(Form form, RichTextBox rbox)
        {
            if (!FbSave)
                AskDialog(form, rbox);
            else
            {
                txtName = string.Format("新建文档 - Mxdr记事本");
                filePath = "";
                Nulltxt();
                form.Text = txtName;
                FbOpen = false; FbSave = true;
            }
        }
        #endregion

        #region  =★*★*★= 打开文件 =★*★*★=
        /// <summary> 打开文件 </summary>
        /// <param name="rbox">RichTextBox 文本框</param>
        /// <param name="b">指示当前的文本是否属于打开的文档,true为打开false为新建文档</param>
        /// <param name="s">指示当前文档是否已经保存,true 为保存,false 未保存</param>
        public static void openFile(Form form, RichTextBox rbox)
        {
            if (FbSave)
                openthe(form);
            else
            {
                AskDialog(form, rbox);
                if (FbSave)
                    openthe(form);
            }
        }
        #endregion

        #region  =★*★*★= 为TreeView节点打开文件 =★*★*★=
        /// <summary> 为TreeView节点打开文件 </summary>
        /// <param name="form">窗口</param>
        /// <param name="path">文件名(含路径)</param>
        public static void openFiles(Form form, string path)
        {
            FileStream fs = new FileStream(path,
                FileMode.Open, FileAccess.Read, FileShare.None);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            readFile(sr.ReadToEnd());  //委托事件
            txtName = Path.GetFileName(path);           //获取文件名
            //filePath = fileInfo.FullName; //获取文件路径
            filePath = path;
            string str = Directory.GetParent(filePath).ToString();
            string strs = Path.GetExtension(filePath);
            txtName = Path.GetFileNameWithoutExtension(path);
            sr.Close();
            fs.Close();

            FbOpen = true; FbSave = true; FbOpens = false;
            form.Text = string.Format("『{0}』{2}〖Mxdr记事本〗 - 所在目录:[{1}]", txtName, str, strs);
        }
        #endregion

        #region  =★*★*★= 打开文件方法 =★*★*★=
        /// <summary> 打开文件方法 </summary>
        /// <param name="rbox">RichTextBox 文本框</param>
        /// <param name="b">指示当前的文本是否属于打开的文档,true为打开false为新建文档</param>
        /// <param name="s">指示当前文档是否已经保存,true 为保存,false 未保存</param>
        private static void openthe(Form form)
        {
            //Mxdr_Nots form = (Mxdr_Nots)this.Owner;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开文本文档 - Mxdr记事本";
            ofd.Filter = "文本文档|*.txt|XML文档|*.xml|所有文档|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                FbOpen = true; FbSave = true; FbOpens = true;
                FileStream fs = new FileStream(ofd.FileName,
                    FileMode.Open, FileAccess.Read, FileShare.None);
                StreamReader sr = new StreamReader(fs, Encoding.Default);
                readFile(sr.ReadToEnd()); //委托
                txtName = ofd.SafeFileName;           //获取文件名
                //filePath = fileInfo.FullName; //获取文件路径
                filePath = ofd.FileName;
                string str = Directory.GetParent(filePath).ToString();
                string strs = Path.GetExtension(filePath);
                txtName = Path.GetFileNameWithoutExtension(ofd.FileName);
                sr.Close();
                fs.Close();

                form.Text = string.Format("『{0}』{2}〖Mxdr记事本〗 - 所在目录:[{1}]", txtName, str, strs);
            }
        }
        #endregion

        #region  =★*★*★= 保存文档 =★*★*★=
        /// <summary>保存文档 <para> </para>
        /// 参数1(form):笔记本窗口<para></para>
        /// 参数2(sft):用于判断控件内容是否改变  </summary>
        /// <param name="form">笔记本窗口</param>
        /// <param name="sft">用于判断控件内容是否改变</param>
        public static void Save(Form form)
        {
            try
            {
                if (FbOpen || filePath != "")
                {
                    //SaveFileDialog ofg = new SaveFileDialog();
                    //rbox.SaveFile(filePath, RichTextBoxStreamType.PlainText);
                    FileSaves(filePath, RichTextBoxStreamType.PlainText);
                    //this.FilesSave(filePath);
                    FbSave = true;
                    FbRiSave = false;
                }
                else
                { SaveAs(form); }
            }
            catch (Exception e)
            { MessageBox.Show(e.ToString()); }
        }
        #endregion

        #region  =★*★*★= 另存为 =★*★*★=
        /// <summary> 另存为 <para></para>
        /// 新建文档另存为其他文档</summary>
        /// <param name="rbox">RichTextBox 文本框</param>
        /// <param name="s">指示当前文档是否已经保存,true 为保存,false 未保存</param>
        public static void SaveAs(Form form)
        {
            //Mxdr_Nots form = (Mxdr_Nots)this.Owner;
            //RichTe
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值