简体   繁体   English

使用在richtextbox中输入的给定内容创建文本文件,但会缩小所有文本

[英]Creating text file with given content entered in richtextbox but it shrinks all text

I am trying to create a simple win application(C#, VS 2010) which creates a text file(at the location where the executable is) with the given filename(provided in a textbox) and content provided in a richtextbox. 我正在尝试创建一个简单的Win应用程序(C#,VS 2010),该应用程序使用给定的文件名(在文本框中提供)和一个RichTextBox中提供的内容创建一个文本文件(在可执行文件所在的位置)。 The code is like: 代码如下:

string executableLocation = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
String path = System.IO.Path.Combine(executableLocation, tbtextname.Text + ".txt");
System.IO.File.WriteAllText(@path, richTextBox1.Text);
richTextBox1.Clear();
tbtextname.Clear();

But it is mixing up everything like if i want to save text: 但这就像我要保存文本一样混淆了所有内容:

my 我的

name 名称

is

superman 超人

then it will give text file content in the format like: mynameissuperman . 然后它将以以下格式提供文本文件内容: mynameissuperman How do I get the text in the format it is entered? 如何获得输入格式的文本? Please help. 请帮忙。

RichTextBox has a method called SaveFile() which saves the contents as a RTF. RichTextBox有一个称为SaveFile()的方法,可将内容另存为RTF。

This will save all your text including your formatting. 这将保存所有文本,包括格式。

http://msdn.microsoft.com/en-us/library/aa335487(v=vs.71).aspx http://msdn.microsoft.com/zh-CN/library/aa335487(v=vs.71).aspx

你想做这样的事情

richTextBox1.SaveFile("Test.txt or your file path",RichTextBoxStreamType.PlainText);

答案已经解决,但我的道歉是指streamwritter不是textwritter

Try using the textwritter class system.io.textwritter writter = new system.io.textwritter("location where you want to save the file") writter.write("you content"); 尝试使用textwritter类system.io.textwritter writter = new system.io.textwritter(“您要保存文件的位置”)writter.write(“您内容”); writter.close(); writter.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM