如何获取文件名和打开文件

chinaggyy9 2003-07-23 03:46:40
我想在我应用程序中做出文件选择对话框!!
...全文
93 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinaggyy9 2003-07-24
  • 打赏
  • 举报
回复
liyj(好好学习 天天向上)

你可不可以再加上一些注释和说明啊
维她奶 2003-07-24
  • 打赏
  • 举报
回复
同意snof(雪狼)的方法。
panyee 2003-07-23
  • 打赏
  • 举报
回复
取得文件全路径名后

FileInfo info = new FileInfo(strFilePath);
string strFileName = info.Name; //取得纯文件名

FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();

这样就把文件读到了byte[]里
youngby 2003-07-23
  • 打赏
  • 举报
回复
可以捕捉OpenFileDialog.filename 最后“\"后面的字符串。。

用SUBSTRING方法

晕忘了。。。。
liyj 2003-07-23
  • 打赏
  • 举报
回复
打开.txt或者.rtf
private void open_Click(object sender, System.EventArgs e)
{
string lstrfilename="";
string lstrtemp="";
if( openFileDialog1.ShowDialog()==DialogResult.OK)
{
lstrfilename=openFileDialog1.FileName;
strdocumentname=lstrfilename;
}
else
{
return;
}
FileStream lfstream=null;
string lstrfilecontents="";
try
{
lstrtemp="";
int liretval=1;
lfstream=new FileStream(lstrfilename,FileMode.Open,FileAccess.Read);
Byte[] lbyte=new Byte[1024];
lbyte.Initialize();
for (;liretval!=0;)
{
lbyte.Initialize();
lstrtemp="";
liretval=lfstream.Read(lbyte,0,1024);
lstrtemp=Encoding.ASCII.GetString(lbyte,0,1024);
lstrfilecontents+=lstrtemp;
}
}
catch(FileNotFoundException el)
{string lstrMessage="File not found or permission denied";
MessageBox.Show(lstrMessage,"word pad",MessageBoxButtons.OK ,MessageBoxIcon.Error);
return;
}
catch(Exception e2)
{ string lstrError="Error:"+e2;
MessageBox.Show(lstrError,"word pad",MessageBoxButtons.OK ,MessageBoxIcon.Error);
return;
}
finally
{
if (lfstream!=null)
{lfstream.Close();
}
}
lstrtemp=lstrfilename.ToLower();
if(lstrtemp.EndsWith(".rtf"))
{
richTextBox1.Rtf=lstrfilecontents;
itypeofdocument=2;
}
else
{ richTextBox1.Text=lstrfilecontents;
itypeofdocument=1;
}
this.Text="word pad"+lstrfilename;
richTextBox1.Select(0,0);
bsaveflag=false;//这个用来判断是否保存的
}
chinaggyy9 2003-07-23
  • 打赏
  • 举报
回复
snof(雪狼) 谢谢你,不过你说的这些我都知道,我想的是一些用于打开文件的代码!
雪狼1234567 2003-07-23
  • 打赏
  • 举报
回复
OpenFileDialog openFileDialog1 = new OpenFileDialog();
this.openFileDialog1.InitialDirectory = "c:\\";

openFileDialog1.Filter = "txt files (*.txt)|*.txt" ;
openFileDialog1.FilterIndex = 1 ;
openFileDialog1.RestoreDirectory = true ;

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = this.openFileDialog1.FileName;
}
declude 2003-07-23
  • 打赏
  • 举报
回复
使用OpenFileDialog 组件

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧