社区
C#
帖子详情
如何获取文件名和打开文件
chinaggyy9
2003-07-23 03:46:40
我想在我应用程序中做出文件选择对话框!!
...全文
83
8
打赏
收藏
如何获取文件名和打开文件
我想在我应用程序中做出文件选择对话框!!
复制链接
扫一扫
分享
举报
写回复
配置赞助广告
8 条
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
打赏红包
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 组件
发帖
C#
C#
.NET技术 C#
复制链接
扫一扫
10.8w+
社区成员
64.2w+
社区内容
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
帖子事件
创建了帖子
2003-07-23 03:46
社区公告
让您成为最强悍的C#开发者