.net C#文件读写

//读文件

public string GetTemplateFile()
        {

            StringBuilder sb = new StringBuilder();

            string appPath = Application.StartupPath;
            string str = appPath + "\\test.htm";
            StreamReader objReader = new StreamReader(str, System.Text.Encoding.Default);
            string sLine = "";
            while (sLine != null)
            {
                sLine = objReader.ReadLine();
                if (sLine != null)
                    sb.Append(sLine + Environment.NewLine);
            }
            objReader.Close();

             return sb.ToString();

        }

//写文件

public void CreateFile(string fileContent)
        {
            string strDate = DateTime.Now.ToString("yyyymmddhhmmss");
            string filePath = appPath+"\\temp\\" + strDate + ".doc";
            //创建文件信息对象--------------------------------------------
            FileInfo finfo = new FileInfo(filePath);
            //以打开或者写入的形式创建文件流
            using (FileStream fs = finfo.OpenWrite())
            {
                //根据上面创建的文件流创建写数据流
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);                                
                sw.Write(fileContent);
                sw.Flush();
                sw.Close();

            }


 

posted on 2010-01-13 10:00  david.chen  阅读(423)  评论(0)    收藏  举报