C#远程读取服务器上的文本内容

WebClient client = new WebClient();
byte[] buffer = client.DownloadData("http://xxx.xxx.xxx.xx:8888/announcement.txt");
string res = Encoding.GetEncoding("GB2312").GetString(buffer);
//string res = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
rtxtNotice.Text = res;

C# 文本读取和保存

private void btSave_Click(object sender, EventArgs e)  //保存txt文件
{
                saveFileDialog1.Filter = ("文本文件(*.txt)|*.txt");  //创建一个筛选器
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)  //判断按下的是保存还是取消
                {
                    string path = saveFileDialog1.FileName;  //获得文件名和路径
                    using (StreamWriter sw = new StreamWriter(path, false))  //保存文件到指定路径
                    {
                        sw.WriteLine(txtNote.Text);
                    }
}
 

private void btOpen_Click(object sender, EventArgs e)  //打开txt文件
{
            openFileDialog1.Filter = ("文本文件(*.txt)|*.txt");  //创建一个筛选器
            if (openFileDialog1.ShowDialog() == DialogResult.OK)  //判断按下的是打开还是取消
            {
                string path = openFileDialog1.FileName;  //获得文件名和路径
                using (StreamReader sr = new StreamReader(path, Encoding.UTF8))  //打开文件
                {
                    txtNote.Text = sr.ReadToEnd();
                }
            }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文地址:https://blog.csdn.net/qq_43024228/article/details/108968462

相关文章: