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

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - 小 强

小题大做之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();
    }

}