当前位置:编程学习 > C#/ASP.NET >>

保存文本文件

[csharp]
public static void SaveTxt(string sFileName, string sContent) 
    { 
        try 
        { 
            if (!System.IO.File.Exists(sFileName)) 
            { 
                FileStream fs; 
                fs = File.Create(sFileName); 
                fs.Close(); 
            } 
 
            FileStream fsTxtWrite = new FileStream(sFileName, FileMode.Create, FileAccess.Write); 
            StreamWriter srWrite = new StreamWriter(fsTxtWrite, System.Text.Encoding.UTF8); 
            //StreamWriter srWrite = new StreamWriter(fsTxtWrite, System.Text.Encoding.Default);  
 
            srWrite.Write(sContent); 
 
            srWrite.Close(); 
            srWrite.Dispose(); 
            fsTxtWrite.Dispose(); 
 
        } 
        catch (Exception ex) 
        { 
            //MessageBox.Show(ex.Message); 
 
            #region ### error info  
            try 
            { 
                string sFolder = Directory.GetParent(sFileName).ToString(); 
                string sErrFullName = sFolder + "_err_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"; 
                if (!System.IO.File.Exists(sErrFullName)) 
                { 
                    FileStream fs; 
                    fs = File.Create(sErrFullName); 
 
                    StreamWriter srWrite = new StreamWriter(fs, System.Text.Encoding.UTF8); 
 
                    srWrite.Write(sFileName); 
 
                    srWrite.Close(); 
                    srWrite.Dispose(); 
 
                    fs.Close(); 
                } 
            } 
            catch (Exception ex2) 
            { } 
            #endregion ###error  
 
        } 
 
    } 
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,