本文介绍了如何通过以编程方式重新加载来将文本写入记事本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在编写用于写入文本到notepad ++的程序,这个程序可以工作,但我的问题是当我的数据写在文本文件中时它总是覆盖第一行的文本,而不是我每次都要在换行中写文本程序运行。为此,我尝试了一些代码,请看看。 感谢您的帮助! I am writing program for write text into notepad++ , and this programs works but my question is when my data written in text file it always override the text of first line only, instead I want to write text in newline every-time when program runs. For that I have tried with some code , please have look.Thanks for any help!if (res.StartsWith("*")){ using (StreamWriter sw = new StreamWriter(@"C:\Users\MyPC\Desktop\test.txt")) { sw.WriteLine(res); sw.Flush(); SendKeys.SendWait("{ENTER}"); SendKeys.Flush(); }} 1>在上面的代码 res 是一个字符串,我从socket获取并检查它并将其写入测试文件。当此方法再次调用第二个输入时,此输入应写入下一行,第一行数据保持不变。 2>在调试中,代码中的上述粗线给出错误对象引用未设置为实例,nullreferenceException。 现在结果: * abc 并且在第二次运行时被* xyz覆盖在同一行。 所需(样本)结果: * abc * xyz 1> In above code res is a string which I am getting from socket and I checked for it and write it in test file. and when this method calls again at 2nd input this input should written in nextline with 1st line data remains constant.2> In debug the above bold line in code gives error "object reference not set to an instance, nullreferenceException".Now result :*abc and at 2nd time run this overriden by *xyz at same line.Desired (sample) result:*abc*xyz推荐答案 public StreamWriter( string path, bool append) 例如 new StreamWriter(file.txt,true / * append提交* /); E.g.new StreamWriter("file.txt", true /*append to file*/); Environment.NewLine 赞这样 like this sw.WriteLine(res + Environment.NewLine); 所以当在记事本中写文本时环境.NewLine带来第二行,所以如果你再写在同一个记事本中,它会自动从第二行开始。 或者试试这个链接 [ ^ Click ] br /> 而不是缓冲区你可以传递字符串,我的意思是你的文字, 试试这段代码 so when write text in notepad Environment.NewLine brings second line,so if u write again in same notepad ,it automatically start from second line.Or try this link[^Click]Instead of buffer u can pass string,i mean your text,Try this codestring res = "*AAA"; if (res.StartsWith("*")) { using (StreamWriter sw = new StreamWriter(@"C:\report.txt",true)) { sw.WriteLine(res); sw.Flush(); } } 问候 Aravindb Regards Aravindb 这篇关于如何通过以编程方式重新加载来将文本写入记事本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 10:52