Home  >  Article  >  php教程  >  c#读取文件写文件

c#读取文件写文件

WBOY
WBOYOriginal
2016-06-06 20:00:591115browse

/// summary /// 读文件方法 /// /summary /// param name="filename"文件名/param /// returns文件内容/returns private string ReadFile(string filename) { string str = ""; string filenamepath = filename; //打开文件 StreamReader sr = File.OpenTex

     ///


    /// 读文件方法
    ///

    /// 文件名
    /// 文件内容
    private string ReadFile(string filename)
    {
        string str = "";
        string filenamepath = filename;
        //打开文件
        StreamReader sr = File.OpenText(filenamepath);
        str = sr.ReadToEnd();
        sr.Close();
        return str;
    }

    ///


    /// 写文件方法
    ///

    /// 文件名
    /// 文件内容
    public void WriteFile(string Path, string Strings)
    {
        StreamWriter sw = null;

        try
        {
            sw = new StreamWriter(Path, false, System.Text.Encoding.UTF8);
            sw.Write(Strings);
            sw.Flush();
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
    }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn