| c#怎么读写文件和获取文件的扩展名
 昨天周日,有好几个网友问我文件怎么读写的,而且都是一些细节问题,我估计是学生或者是初学者
 正好今天我贴几个方法,方便以后使用和查阅吧。
 先来看看写文件的方法吧
 
 
 [C#] 纯文本查看 复制代码  protected void Write_Txt(string FileName, string Content)
        {
            Encoding code = Encoding.GetEncoding("gb2312");
            string htmlfilename = HttpContext.Current.Server.MapPath("Precious\\" + FileName + ".txt"); //保存文件的路径
            string str = Content;
            StreamWriter sw = null;
            {
                try
                {
                    sw = new StreamWriter(htmlfilename, false, code);
                    sw.Write(str);
                    sw.Flush();
                }
                catch { }
            }
            sw.Close();
            sw.Dispose();
        }Encoding 是写文件时的编码
 本例子使用的是StreamWriter进行文件的写入
 读文件的方法如下
 
 
 [C#] 纯文本查看 复制代码 protected string Read_Txt(string filename)
        {
            Encoding code = Encoding.GetEncoding("gb2312");
            string temp = HttpContext.Current.Server.MapPath("Precious\\" + filename + ".txt");
            string str = "";
            if (File.Exists(temp))
            {
                StreamReader sr = null;
                try
                {
                    sr = new StreamReader(temp, code);
                    str = sr.ReadToEnd(); // 读取文件
                }
                catch { }
                sr.Close();
                sr.Dispose();
            }
            else
            {
                str = "";
            }
            return str;
        }以上是使用流的方式,其实还有另外一种方法创建文件,方法如下
 
 [C#] 纯文本查看 复制代码   #region 写文件
        /****************************************
         * 函数名称:WriteFile
         * 功能说明:当文件不存时,则创建文件,并追加文件
         * 参    数:Path:文件路径,Strings:文本内容
         * 调用示列:
         *           string Path = Server.MapPath("Default2.aspx");       
         *           string Strings = "这是我写的内容啊";
         *           DotNet.Utilities.FileOperate.WriteFile(Path,Strings);
        *****************************************/
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <param name="Strings">文件内容</param>
        public static void WriteFile(string Path, string Strings)
        {
            if (!System.IO.File.Exists(Path))
            {
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();
                f.Dispose();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
            f2.WriteLine(Strings);
            f2.Close();
            f2.Dispose();
        }
        #endregion
        #region 读文件
        /****************************************
         * 函数名称:ReadFile
         * 功能说明:读取文本内容
         * 参    数:Path:文件路径
         * 调用示列:
         *           string Path = Server.MapPath("Default2.aspx");       
         *           string s = DotNet.Utilities.FileOperate.ReadFile(Path);
        *****************************************/
        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <returns></returns>
        public static string ReadFile(string Path)
        {
            string s = "";
            if (!System.IO.File.Exists(Path))
                s = "不存在相应的目录";
            else
            {
                StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
                s = f2.ReadToEnd();
                f2.Close();
                f2.Dispose();
            }
            return s;
        }
        #endregion上面是使用StreamReader 来读取文件的,同样也需要设置编码,要和上面写文件的编码进行一样。否则可能会出现编码问题,就是
 乱码
 获取文件的扩展名的方法如下
 
 
 [C#] 纯文本查看 复制代码   /****************************************
         * 函数名称:GetPostfixStr
         * 功能说明:取得文件后缀名
         * 参    数:filename:文件名称
         * 调用示列:
         *           string filename = "aaa.aspx";        
         *           string s = DotNet.Utilities.FileOperate.GetPostfixStr(filename);         
        *****************************************/
        /// <summary>
        /// 取后缀名
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns>.gif|.html格式</returns>
        public static string GetPostfixStr(string filename)
        {
            int start = filename.LastIndexOf(".");
            int length = filename.Length;
            string postfix = filename.Substring(start, length - start);
            return postfix;
        }其实原理也很简单就是根据.进行获取的,只要取最后一个点后面的字符,就肯定是文件的扩展名了。
 
 
 |