C# 实用文件存储

这里的实例是C# 编写记事本

第一:解决文件读写问题

1.读:文件-打开 click时间是新建一个OpenFileDialog提示框选取文件,读取的方式是采用了FileStream文件流的方式

click方法

<span style="white-space:pre">	</span>OpenFileDialog dlg = new OpenFileDialog();

             if (dlg.ShowDialog() == DialogResult.OK)//判断是否点击了确定按钮
                 textBox1.Text=tools.FileRW.ReadText(dlg.FileName,EncodingUse);
             fName = dlg.SafeFileName;//全局文件名
             FilePath = dlg.FileName.Substring(0, dlg.FileName.IndexOf(fName));//全局文件路径
FileStream读方法

        /// <summary>
        /// //c#文件流读文件 
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="ReadEncoding">{"Default","UTF-8","ANSI","Unicode","Unicode big encode"}--0,1 ,2 ,3 ,4</param>
        /// <returns></returns>
        public static string ReadText(string FilePath,int ReadEncoding)
        {
            using (FileStream fsRead = new FileStream(@FilePath, FileMode.Open))
            {
                int fsLen = (int)fsRead.Length;
                byte[] heByte = new byte[fsLen];
                int r = fsRead.Read(heByte, 0, heByte.Length);
                string myStr="";
                myStr = System.Text.Encoding.Default.GetString(heByte);
                
                return myStr;
            }
        }
2.存:直接写入文件即可

<span style="white-space:pre">	</span>/// <summary>
        /// C#文件流写文件,默认追加FileMode.Append ,但是这里没有追加Append
        /// </summary>
        /// <param name="text">要写入的字符串</param>
        /// <param name="FilePath">文件路径+文件名</param>
        public static void WriteText(string text,string FilePath)
        {
            byte[] myByte = System.Text.Encoding.Default.GetBytes(text);
            using (FileStream fsWrite = new FileStream(@FilePath, FileMode.Create, FileAccess.Write))
            {
                fsWrite.Write(myByte, 0, myByte.Length);
            };
        }


3,另存:采用了saveFileDialog和Filestream进行操作

saveFileDialog方法

<span style="white-space:pre">	</span>    SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "另存为What?";//
            sfd.InitialDirectory =FilePath;//设置打开路径
            sfd.Filter = "txt files(*.txt)|*.txt|xls files(*.xls)|*.xls|All files(*.*)|*.*";//设置可选文件保存类型
            sfd.FileName = fName;//set value file name
            sfd.DefaultExt = "txt";//set value file suffix
            sfd.AddExtension = true;//<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名</span>
            sfd.RestoreDirectory = true;//set y or n remenber last file open path
            if (sfd.ShowDialog() == DialogResult.OK)
            {
               string localFilePath = sfd.FileName.ToString();//get the dialog save file path
               string savename= localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//get file save name
                tools.FileRW.WriteText(textBox1.Text, FilePath, savename);//write out
                
            }
文件另存方法

 <span style="white-space:pre">	</span>/// <summary>
        /// File other write
        /// </summary>
        /// <param name="text">要写入的文件内容</param>
        /// <param name="FilePath">文件路径</param>
        /// <param name="FileName">文件名</param>
        public static void WriteText(string text, string FilePath, string FileName)
        {
            byte[] myByte = System.Text.Encoding.Default.GetBytes(text);
            string bufferName=FilePath+FileName;
            using (FileStream fsWrite = new FileStream(@bufferName, FileMode.Create, FileAccess.Write))
            {
                fsWrite.Write(myByte, 0, myByte.Length);
            };
        }

你可能感兴趣的:(C#,实例,实战,记事本)