为什么我的对话框不能打开文件的?

mhq731694994 2009-01-21 03:03:38
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string filename = null;
public Form1()
{
InitializeComponent();
}

private void openToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{ filename = openFileDialog1.FileName;
openFileDialog1.OpenFile();
}
}
}
}

问什么这里的OpenFile()不能打开TXT文件的??
...全文
73 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
云飞何处 2009-01-23
  • 打赏
  • 举报
回复
你打开文件总的用吧,不会就是为了打开吧,如果只是想打开,就再调一个System.Diagnostics.Process.Start(filename);就可以了,如果是要在程序里面用的数据,就要写代码处理读到的数据流。
king19840811 2009-01-21
  • 打赏
  • 举报
回复
设置openfiledialog的filter属性可以限制打开文件的类型
pangxiaoliang 2009-01-21
  • 打赏
  • 举报
回复
。。。。。
使用FileStream,StreamReader
分别读取二进制和文本,搜一下就明白

yeahspyme 2009-01-21
  • 打赏
  • 举报
回复
您如何判断出无法打开txt文件呢?
zgke 2009-01-21
  • 打赏
  • 举报
回复
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog1.FileName;
System.IO.Stream _Strem = openFileDialog1.OpenFile();
byte[] _ByteValue =new byte[_Strem.Length];
_Strem.Read(_ByteValue, 0, _ByteValue.Length);

string _Text = System.Text.Encoding.Default.GetString(_ByteValue);
}
king19840811 2009-01-21
  • 打赏
  • 举报
回复
OpenFile()打开了。你得要有东西接收吧
jinjazz 2009-01-21
  • 打赏
  • 举报
回复
他返回的是stream,你需要自己读取stream内容。

110,537

社区成员

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

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

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