C#获取图片像素把他变成java字符编码转字符写入记事本

用c#把一个字符串写到记事本中_百度知道
用c#把一个字符串写到记事本中
要求不要覆盖以前的内容,具体点。要不我看不懂。高手来啊!!!...
要求不要覆盖以前的内容, 具体点。 要不我看不懂。
高手来啊!!!
答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
* 参数是你要写入数据的记事本的地址,如果没有这个记事本,File会自动创建
* 所以不用向楼上一样,还要判断。
StreamWriter sw= File.AppendText(&D:\\123.txt&);
* 你要追加的数据
* 不用设置编码格式,原因是StreamWriter可以写入中文
sw.Write(&爱上对方家啥地方abcdefgABCDEFG0123456&);
sw.Flush();//清空缓冲区
sw.Close();//关闭流
String errPath = @&c:\Error.txt&;FileStream errStr =//文件流StreamWriter errWri =//写入流try{
if (File.Exists(errPath))//判断文件是否存在
errStr = new FileStream(errPath, FileMode.Append);//打开文件搜寻到文件尾
errStr = new FileStream(errPath, FileMode.Create);//创建文件
errWri = new StreamWriter(errStr, System.Text.Encoding.UTF8);//new写入流指定编码
errWri.Write(&test\r\n&);//写入文件}catch (UnauthorizedAccessException err) {
System.Windows.Forms.MessageBox.Show(err.Message);}catch (IOException err){
System.Windows.Forms.MessageBox.Show(err.Message);}
if (errWri != null)
errWri.Close();
if(errStr!=null){
errStr.Close();
这里好像根本就不需要用到流的,如果要插入的字符串本来就是string型的话 只用insert方法就可以实现插入了 比如说记事本的文本框id是txt 那么这样写就可以了 txt.Text = txt.Text.Insert(txt.SelectStart, &要插入的字符串&); 手写的代码 可能有些字母不对
为你推荐:
其他类似问题
您可能关注的内容
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。C#读取HTML文件内容写入记事本
int totalFile = 0;
//string dirPath = @"E:\chfuMetarnet\BSC6810 alarm\";
if (this.textBox1.Text.Trim() == "")
MessageBox.Show("请输入HTML文件路径!");
string dirPath = this.textBox1.Text.Trim();
if (!dirPath.Substring(dirPath.Length - 1).Contains("\\"))
dirPath = dirPath+"\\";
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
FileInfo[] files = dirInfo.GetFiles();
string filename = dirPath + "告警经验库信息.txt";
if (File.Exists(filename))
sw = File.AppendText(filename);
sw = File.CreateText(filename);
foreach (FileInfo fileinfo in files)
if (fileinfo.Extension.Equals(".htm"))//遍历所有htm文件
totalFile = totalFile + 1;
WebRequest myWebRequest = WebRequest.Create(dirPath + fileinfo.Name);
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream myStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
StreamReader myStreamReader = new StreamReader(myStream, encode);
string strhtml = myStreamReader.ReadToEnd();
myWebResponse.Close();
string stroutput =
Regex regex = new Regex(@"&[^&]+&|&/[^&]+&");//去掉HTML标记的正则表达式
string tmpStr = "&h4&([^&]*)&/h4&";
//获取&h4&之间内容的表达式
Match TitleMatch = Regex.Match(strhtml, tmpStr, RegexOptions.IgnoreCase | RegexOptions.Multiline);
string causename = TitleMatch.Value.ToString();//包含&h4&和&/h4&标记
causename = Regex.Replace(causename, "[\n|\r|\t]", " ");//去掉换行和TAB键符号
causename = causename.Trim();
string cause = causename.Substring(4, causename.Length - 9);//得到告警原因
string titleStr = "&title&([^&]*)&/title&";
TitleMatch = Regex.Match(strhtml, titleStr, RegexOptions.IgnoreCase | RegexOptions.Multiline);
string titlename = TitleMatch.Value.ToString();
titlename = Regex.Replace(titlename, "[\n|\r|\t]", "");//去掉换行和TAB键符号
titlename = titlename.Trim();
string regexStr = "&ul&&li&(?&key&.*?)&/ul&";//获取&ul&&li&后边的内容,直到&/ul&结尾
Regex r = new Regex(regexStr, RegexOptions.None);
strhtml = Regex.Replace(strhtml, "[\n|\r|\t]", "");//去掉换行和TAB键符号
Match mc = r.Match(strhtml);
string dataStr = mc.Groups["key"].V
dataStr = "&ul&&li&" + dataStr + "&/ul&";//得到完整的&ul&&/ul&之间的源码
strhtml = strhtml.Replace(dataStr, "");//将去掉换行符和tab键的源码中去除&ul&&/ul&部分源码
strhtml = strhtml.Replace(titlename, "");//去掉&title&&/title&
strhtml = regex.Replace(strhtml, " ");//过滤掉HTML标记
strhtml = strhtml.Replace("&", "");//去掉空格字符
string[] arr = cause.Split(' ');
string zhCause = arr[arr.Length - 1];//获取数组最后一个元素:告警原因
sw.WriteLine("第" + totalFile + "个文件:" + fileinfo.Name);
sw.WriteLine("-----告警原因------:");
//sw.WriteLine(cause);// ALM-1 网元启动
zhCause=this.chinaString(zhCause);
sw.WriteLine(zhCause);//网元启动
sw.WriteLine("-----处理经验------:");
sw.WriteLine(strhtml);
sw.WriteLine();
sw.Flush();
sw.Close();
MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
catch (Exception ee)
MessageBox.Show("操作失败:" + ee.Message);
阅读(...) 评论()急求c#怎么把程序中的数据保存到记事本中?重谢
作者:用户
浏览:413 次
c#怎么把程序中的数据保存到记事本中?例如,我把程序中运行的结果保存到记事本中,谁有源代码给我借鉴一下,重谢1###可以用writestream写。###你想保存什么数据IO里面有写文件的操作###我
问题描述c#怎么把程序中的数据保存到记事本中?例如,我把程序中运行的结果保存到记事本中,谁有源代码给我借鉴一下,重谢1解决方案解决方案二:可以用writestream写。解决方案三:你想保存什么数据IO里面有写文件的操作解决方案四:我的想法很简单,就是要保存一个数组的数据到记事本中,怎么写?谢谢了解决方案五:我的运行结果出来个个数组的数据然后保存到记事本中,解决方案六:FileStreamfs=newFileStream("c:\test.txt",......);fs.WriteLine("helloworld");fs.Close();解决方案七:string[]strs={"1","2"};FileStreamfs=newFileStream(path,FileMode.OpenOrCreate);StreamWritersw=newStreamWriter(fs);foreach(stringstrinstrs){sw.WriteLine(str);}sw.Close();fs.Close();解决方案八:谢谢,大哥!解决方案九:6楼正确,用Stream类来操作就行了解决方案十:system.io.writestream解决方案十一:如果只是简单的保存文本那么用个IO就可以了,但是如果你要保存对象的话,那么需要用到序列化。下面是我随手写的代码,你看看吧。usingSystem.Runtime.Serialization.Formatters.BusingSystem.IO;usingSystem.CstaticvoidMain(string[]args){Here:if(File.Exists("ok.txt")){Console.WriteLine("在这里反序列化");FileStreamfs=newFileStream("ok.txt",FileMode.Open,FileAccess.Read);BinaryFormatterbf=newBinaryFormatter();ArrayListal=bf.Deserialize(fs)asArrayLfs.Flush();fs.Close();foreach(intsinal){Console.WriteLine(s);}}else{Console.WriteLine("在这里序列化");ArrayListal=newArrayList();for(inti=0;i&100;i++){al.Add(i);}FileStreamfs=newFileStream("ok.txt",FileMode.Create,FileAccess.Write);BinaryFormatterbf=newBinaryFormatter();bf.Serialize(fs,al);fs.Flush();fs.Close();gotoH}解决方案十二:绝大多数对象是可以序列化的,object不支持序列化。但是,如果你保存点什么HashTable,自定义类(需要继承自MarshalByRefObject)也是可以做得到的。解决方案十三:如果只是保存基本类型,上面两位朋友的代码足够了。解决方案十四:你说的重谢哈,我还就差这点分升级=。=!!解决方案十五:数组的话,也可以保存对象,这样比你逐个写入,再逐个读出资源消耗要小很多。。而且你可以在读出之后直接还原成为数组,不需要再进行转换记入数组,上面的代码是保存ArrayList,这个比数组方便吧。解决方案:给你50分,可以把,还有两外两个一个人20,30把,别介意哈,jhkemail!
【云栖快讯】Apache旗下顶级开源盛会 HBasecon Asia 2018将于8月17日在京举行,现场仅600席,免费赠票领取入口&&
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率
40+云计算产品,6个月免费体验
稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一
云服务器9.9元/月,大学必备posts - 71,&
comments - 20,&
trackbacks - 0
写入文本文件
class WriteTextFile
static void Main()
//如果文件不存在,则创建;存在则覆盖
//该方法写入字符数组换行显示
string[] lines = { "first line", "second line", "third line","第四行" };
System.IO.File.WriteAllLines(@"C:\testDir\test.txt", lines, Encoding.UTF8);
//如果文件不存在,则创建;存在则覆盖
string strTest = "该例子测试一个字符串写入文本文件。";
System.IO.File.WriteAllText(@"C:\testDir\test1.txt", strTest, Encoding.UTF8);
//在将文本写入文件前,处理文本行
//StreamWriter一个参数默认覆盖
//StreamWriter第二个参数为false覆盖现有文件,为true则把文本追加到文件末尾
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\testDir\test2.txt",true))
foreach (string line in lines)
if (!line.Contains("second"))
file.Write(line);//直接追加文件末尾,不换行
file.WriteLine(line);// 直接追加文件末尾,换行
&读取文本文件
class ReadTextFile
static void Main()
//直接读取出字符串
string text = System.IO.File.ReadAllText(@"C:\testDir\test1.txt");
Console.WriteLine(text);
//按行读取为字符串数组
string[] lines = System.IO.File.ReadAllLines(@"C:\testDir\test.txt");
foreach (string line in lines)
Console.WriteLine(line);
//从头到尾以流的方式读出文本文件
//该方法会一行一行读出文本
using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\testDir\test.txt"))
while ((str = sr.ReadLine()) != null)
Console.WriteLine(str);
Console.Read();
阅读(...) 评论()随笔 - 2029&
文章 - 1&评论 - 149&trackbacks - 0
该样例为追加 C盘中的&file1.txt&的文本内容
完整代码例如以下:
引入命名空间:
using&System.IO;&&
完整代码:
namespace&FileStreamWrite&&
&&&&class&Program&&
&&&&&&&&static&void&Main(string[]&args)&&
&&&&&&&&{&&
&&&&&&&&&&&&FileStream&fs&=&null;&&
&&&&&&&&&&&&string&filePath&=&"C:\\file1.txt";&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&Encoding&encoder&=&Encoding.UTF8;&&
&&&&&&&&&&&&byte[]&bytes&=&encoder.GetBytes("Hello&World!&\n\r");&&
&&&&&&&&&&&&try&&
&&&&&&&&&&&&{&&
&&&&&&&&&&&&&&&&fs&=&File.OpenWrite(filePath);&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&fs.Position&=&fs.L&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&fs.Write(bytes,&0,&bytes.Length);&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&catch&(Exception&ex)&&
&&&&&&&&&&&&{&&
&&&&&&&&&&&&&&&&Console.WriteLine("文件打开失败{0}",&ex.ToString());&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&finally&&
&&&&&&&&&&&&{&&
&&&&&&&&&&&&&&&&fs.Close();&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&Console.ReadLine();&&
&&&&&&&&}&&
以上为完整代码!
fs&=&File.OpenWrite(filePath);&&
fs.Position&=&fs.L&&
fs&=&File.Open(filePath,&FileMode.Append,&FileAccess.ReadWrite);&&
执行。。。没效果如,呵呵,直接追加进去了,点击文本就可以看到效果了。
若以上代码编译有问题,可下载项目文件直接编译:&
阅读(...) 评论()

我要回帖

更多关于 免费的手机记事本 的文章

 

随机推荐