当前位置: 开源爱好者 » 编程教程 » C#教程 » 正文

C# 向文件追加内容指定保存格式 读写Win.ini配置文件

C# 向文件追加内容指定保存格式 读写Win.ini配置文件,C# System.IO.File

private void button1_Click(object sender, EventArgs e)
{//向现有文件的内容追加数据
    var MyWriter =System.IO.File.AppendText(Application.StartupPath + "\\MyAppend.txt");
    var MyInfo = Environment.NewLine + "France";
    MyWriter.Write(MyInfo);
    MyWriter.Close();
    MessageBox.Show("追加数据顺利完成!", "信息提示", MessageBoxButtons.OK);      
}
private void button2_Click(object sender, EventArgs e)
{//指定文件保存的编码方式
    var MyStream = new System.IO.FileStream("MyUTF8UTF16ASCII.bin", System.IO.FileMode.Create);
    var MyUTF8 = new System.IO.StreamWriter(MyStream, Encoding.UTF8);
    MyUTF8.Write("This is in UTF8");
    MyUTF8.Flush();
    var MyUTF16 = new System.IO.StreamWriter(MyStream, Encoding.Unicode);
    MyUTF16.Write("This is in UTF16");
    MyUTF16.Flush();
    var MyASCII = new System.IO.StreamWriter(MyStream, Encoding.ASCII);
    MyASCII.Write("This is in ASCII");
    MyASCII.Flush();
    MyASCII.Close();
}

Win.ini配置文件读写操作代码:

private void button3_Click(object sender, EventArgs e)
{//读写WIN.INI类型的配置文件
    WriteProfileString("演示程序", "产品名称", "Visual C# 2008 编程技巧大全");
    WriteProfileString("演示程序", "公司名称", "宝特软件公司");
    WriteProfileString("演示程序", "联系电话", "40405690");
    MessageBox.Show("成功写入C:\\Windows\\WIN.INI配置文件!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    string MyInfo = "C:\\Windows\\WIN.INI配置文件演示程序部分的内容如下:\n";
    StringBuilder MyString = new StringBuilder(256);
    GetProfileString("演示程序", "产品名称", "", MyString, 256);
    MyInfo+="\n"+ MyString.ToString();
    GetProfileString("演示程序", "公司名称", "", MyString, 256);
    MyInfo += "\n" + MyString.ToString();
    GetProfileString("演示程序", "联系电话", "", MyString, 256);
    MyInfo += "\n" + MyString.ToString();
    MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK);
}
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern bool GetProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize);
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern bool WriteProfileString(string lpApplicationName, string lpKeyName, string lpString);
相关内容:

CopyRight 2015© 开源爱好者 All Rights Reserved 豫ICP备16030607号 隐私声明 网站地图