我试图将文本框中的文本附加到文本文件中的新行,但遇到了问题。假设文本文件中已经有内容,看起来像这样
something
Something
Something<---- Cursor ends here
Run Code Online (Sandbox Code Playgroud)
光标结束于箭头指向的位置(在最后一个“某事”上的 g 之后。如果我尝试使用
File.AppendAllLines(@"Location", new[]{tb.Text});
Run Code Online (Sandbox Code Playgroud)
或者
File.AppendAllText(@"Location", tb.Text + Environment.NewLine);
Run Code Online (Sandbox Code Playgroud)
他们会将文本放在光标所在的位置,而不是放在文本文件中最后一项下的新行上。如果光标一开始位于新行上,但一旦它在单词末尾结束,所有内容都会在同一行上,它会起作用。
我有什么遗漏的吗?使用流写入器或其他方法可以解决此问题吗?
我的文本框出了问题.
我有一个表示GUI线程和工作线程,做一些网络的东西一类,然后必须将日志添加到GUI线程文本框,所以你可以看到什么是在后台发生的一类.
但是,我遇到的问题是GUI上没有任何反应,只有调用addLine()的调试信息才在控制台中.
应该添加日志的方法addLine()被调用,但似乎AppendText()什么都不做.
我很确定这必须与线程有关,但我不确定如何解决这个问题.
这是代码:
工人线程:
Form1 form = new Form1();
// This method gets called in the worker thread when a new log is available
private void HandleMessage(Log args)
{
// Using an instance of my form and calling the function addLine()
form.addLine(args.Message);
}
Run Code Online (Sandbox Code Playgroud)
GUI线程:
// This method gets called from the worker thread
public void addLine(String line)
{
// Outputting debug information to the console to see if the function gets called, it does get called
Console.WriteLine("addLine …Run Code Online (Sandbox Code Playgroud)