每当我试图打开一个自定义文件到一个文本框或将显示代码的东西。我不知道我做错了什么。当我打开文件时,我希望我的程序显示文件中的内容,下面是这样的:
private void button1_Click(object sender, EventArgs e)
{
//Show Dialogue and get result
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "rbt files (*.rbt)|*.rbt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
File.WriteAllText("", CodeBox.Text);
}
}
}
catch (Exception ex)
{
MessageBox.Show("RBT7 file open");
}
}
}它只在一个不是我想要的消息框中显示RBT7,我希望该文件打开并将其信息显示给某种显示代码的文本框。
发布于 2013-10-03 20:17:01
请阅读File.WriteAllText文档。
第一个参数:
路径:要写入的文件。
你要把它传给""。那不是一条路。您是试图将文件中的所有文本写入CodeBox.Text,还是将CodeBox.Text中的所有文本写入文件?
在你的评论中,你指出了前者。试试这个:
string[] lines = System.IO.File.ReadAllLines(@"your file path");
foreach (string line in lines)
{
CodeBox.Text += line;
}您还没有显示CodeBox的代码,所以我不能保证结果。
发布于 2013-10-03 20:20:33
试试这个:
替换此代码
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
File.WriteAllText("", CodeBox.Text);
}
}有了这个
{
CodeBox.Text = File.ReadAllText(openFileDialog1.FileName);
}https://stackoverflow.com/questions/19168273
复制相似问题