首页  >  文章  >  php教程  >  c#读取文件写文件

c#读取文件写文件

WBOY
WBOY原创
2016-06-06 20:00:591202浏览

/// 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();
        }
    }

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn