以C#Windows形式显示所选路径中的文件 [英] display file from selected path in C# windows form
本文介绍了以C#Windows形式显示所选路径中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在C#Windows base上执行程序,我在其中编写代码以获取文本框中文件的路径,但是当我尝试在另一个文本框中打开文件文件时,它没有打开
下面我给出代码
Hi All,
I m doing program on C# windows base where I code for getting the path for a file in text box but when I try to open the file file in another text box it is not opening
below I give the code
private void btnShow_Click(object sender, EventArgs e)
{
//001: Check the user selected a file and pressed ok button. Before opening the dialog
//set the filters
dlgFileOpen.Filter = "Bitmap Files(*.bmp)|*.bmp|Other Image Files(*.jpg)|*.jpg|" +
"Text Files(*.txt) | *.txt|All Files(*.*)|*.*";
if (dlgFileOpen.ShowDialog() == DialogResult.OK)
{
txtSelectedFile.Text = dlgFileOpen.FileName;
string ext = Path.GetExtension(dlgFileOpen.FileName);
if (ext != ".txt")
{
//002: If the extension is Bmp or Jpg display it in the Picture box
txtFileContent.Visible = false;
//txtFileContent.Visible = false;
pict.Visible = true;
pict.SizeMode = PictureBoxSizeMode.StretchImage;
pict.Load(dlgFileOpen.FileName);
}
else
{
//003: The extension is txt. Read the file and display the content
txtFileContent.Visible = true;
pict.Visible = false;
FileStream fstream = new FileStream(dlgFileOpen.FileName, FileMode.Open);
StreamReader reader = new StreamReader(fstream);
while (reader.EndOfStream != true)
{
//here it is reading but not open in the text box [txtFileContent]
txtFileContent.AppendText(reader.ReadLine());
txtFileContent.AppendText(Environment.NewLine);
}
reader.Close();
}
}
}
推荐答案
用以下代码替换else块:
Replace your else block with this:
txtFileContent.Visible = true;
pict.Visible = false;
TextReader reader = new StreamReader(dlgFileName.FileName);
txtFileContent.Text = reader.ReadToEnd();
reader.Close();
尝试一下
private void button1_Click(object sender, EventArgs e)
{
//001: Check the user selected a file and pressed ok button. Before opening the dialog
//set the filters
dlgFileOpen.Filter = "Bitmap Files(*.bmp)|*.bmp|Other Image Files(*.jpg)|*.jpg|" +
"Text Files(*.txt) | *.txt|All Files(*.*)|*.*";
if (dlgFileOpen.ShowDialog() == DialogResult.OK)
{
txtSelectedFile.Text = dlgFileOpen.FileName;
string ext = Path.GetExtension(dlgFileOpen.FileName);
//Changes are done in below line
if (ext.ToLower() == ".bmp" || ext.ToLower() == ".jpg")
{
//002: If the extension is Bmp or Jpg display it in the Picture box
txtFileContent.Visible = false;
//txtFileContent.Visible = false;
pict.Visible = true;
pict.SizeMode = PictureBoxSizeMode.StretchImage;
pict.Load(dlgFileOpen.FileName);
}
else
{
//003: The extension is txt. Read the file and display the content
txtFileContent.Visible = true;
pict.Visible = false;
FileStream fstream = new FileStream(dlgFileOpen.FileName, FileMode.Open);
StreamReader reader = new StreamReader(fstream);
while (reader.EndOfStream != true)
{
//here it is reading but not open in the text box [txtFileContent]
txtFileContent.AppendText(reader.ReadLine());
txtFileContent.AppendText(Environment.NewLine);
}
reader.Close();
}
}
}
这篇关于以C#Windows形式显示所选路径中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文