private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "富文本文件|*.rtf|所有文件|*.*";
if (openFile.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFile.FileName, Encoding.Default);
rtbText.Rtf = sr.ReadToEnd().Replace("\r\n", "\n");
sr.Close();
a = openFile.FileName;
}
}
string path = null;
OpenFileDialog op = new OpenFileDialog();//实例openfiledialog
op.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (op.ShowDialog() == DialogResult.OK)//选则文件
{
path = op.FileName;//获取文件路径
}
if (path != null)
{
try
{
using (StreamReader sr = new StreamReader(path, Encoding.Default))
{
string line;
while ((line = sr.ReadLine()) != null)
{
SQLDB.DBsql.SQLinsertgongyingshang(line.Substring(0, 6).Trim(), line.Substring(6, 10).Trim(), line.Substring(line.Length - 20, 20));
}
MessageBox.Show("读出成功!!");
}
}
//用 RichTextBox 自带的 LadFile 方法。
if (openFile.ShowDialog() == DialogResult.OK)
{
rtbText.LoadFile(openFile.FileName, RichTextBoxStreamType.RichText);
}
试试这段代码:
if (openFile.ShowDialog() == DialogResult.OK)
{
string rtf = File.ReadAllText(openFile.FileName);
rtbText.Rtf = rtf.ReadToEnd().Replace("\r\n", "\n");
}