惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Affairs
美团技术团队
量子位
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
Apple Machine Learning Research
Apple Machine Learning Research
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
Recent Announcements
Recent Announcements
Cisco Talos Blog
Cisco Talos Blog
L
LangChain Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 三生石上(FineUI控件)
U
Unit 42
T
Tenable Blog
Security Latest
Security Latest
Scott Helme
Scott Helme
B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
A
Arctic Wolf
S
Schneier on Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
酷 壳 – CoolShell
酷 壳 – CoolShell
I
Intezer
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
有赞技术团队
有赞技术团队
雷峰网
雷峰网
The Cloudflare Blog
Cloudbric
Cloudbric
Latest news
Latest news
Project Zero
Project Zero
S
Secure Thoughts
V
Visual Studio Blog
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity

博客园 - 小 强

小题大做之MySQL 5.0存储过程编程入门 车票,转让,杭州,武汉,2月3号[已过期] 2007即将过去,我很怀念它,2008 小强年,我很期待 附加数据库,并且迁移用户的方法 [收藏]ContentType类型大全 - 小 强 - 博客园 ASP.NET 1.1 Web Application Compilation and Pre-compilation 解决访问局域网内共享目录需要登陆的问题 [收藏]通过命令的方式调用系统的小工具 [收藏]Do All in Cmd Shell 解决文件无法上传的问题,错误信息:System.IO.DirectoryNotFoundException: Could not find a part of the path. 远程连接SQL Sever2000出错,1433端口未开放的解释 通过调用系统API获得网卡真实MAC地址 C#拖放操作获得文件名 如何添加IE右键菜单 [收藏]DOS 命令大全 查找目录下某特定后缀名的文件的简单方法 [转载] Windows Forms 实现安全的多线程详解 准备看的关于多线程的文章(临时记录) C#文件重命名的方法
用GB2312编码写文件
小 强 · 2006-03-29 · via 博客园 - 小 强

用以下两种方式写文件时,都是默认使用的UTF-8格式的编码

string csFileName = ""//文件名,完整路径
string html = "";   //文件内容

//方法一
using (StreamWriter sww = File.CreateText(csFileName)) {
    sww.Write(html);
    sww.Flush();
}


//方法二
FileInfo file = new FileInfo(csFileName);
using (StreamWriter sww = file.CreateText()) {
    sww.Write(html);
    sww.Flush();
}

以上两种CreateText方法都是使用UTF-8编码,并且没有办法指定编码。但是StreamWriter的构造函数中可以指定编码格式,所以可以使用下面的方法,实现用GB2312编码写文件的需求。

/// <summary>
/// 将字符串写入指定的文件中
/// </summary>
/// <param name="path">文件名,完整路径</param>
/// <param name="source">要写入文件的字符串</param>

private void WriteFile(string path, string source){
    
using(StreamWriter writer = new StreamWriter(path, false, Encoding.GetEncoding("GB2312"), 512)){
        writer.Write(source);
        writer.Flush();
    }

}