如何在C#中打开文件 [英] How to open a file in C#
本文介绍了如何在C#中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我喜欢打开一个文件.打开选项应显示在单独的窗口中.我的代码工作正常,但打开的窗口未显示以打开文件.谁能帮我解决这个问题.这是代码:
Hi All,
I like to open a file. The open option should be shown in a separate window. My code works fine but the open window is not displaying to open a file. Can anyone help me to solve this problem. Here is the code:
private void OpenMyFile()
{
string path = GetPath() + ViewState["fileopen"];
FileInfo file = new FileInfo(ViewState["fileopen"].ToString());
SqlConnection con = new SqlConnection(@"server=servername;database=DBNAME;uid=ID;pwd=PWD;max pool size=250;Connect Timeout=0");
con.Open();
cmd = new SqlCommand("select * from filetypemaster where extension=''" + file.Extension + "''", con);
myReader = cmd.ExecuteReader();
if (myReader.Read())
{
Response.ContentType =(String)myReader["CONTENT_TYPE"];
myReader.Close();
}
else
{
myReader.Close();
Message("Cannot open selected file");
return;
}
Response.WriteFile(path);
Response.End();
con.Close();
}
;
在此先感谢.
;
Thanks in Advance.
推荐答案
您缺少标题.
You are missing header.
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.jpg");
Response.TransmitFile( Server.MapPath("~/images/sailbig.jpg") );
Response.End();
参考: http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET [ ^ ]
Ref:http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET[^]
也许您应该尝试类似的方法,进入目录,获取所有文件.然后过滤掉fileInfos列表以匹配您的扩展名.然后,您有一个List< fileinfo>具有扩展名类型的所有文件.那你就可以做你开放的事情.
或
您可能要查看Windows窗体中的OpenFileDialog工具箱项
Maybe you should try something like, looking into the directory, get all the files. Then filter out that list of fileInfos to match your extension. Then you have a List<fileinfo> of all the files with your extension type. Then you can do your open thing.
or
you might want to look into the OpenFileDialog toolbox item in windows Forms
这篇关于如何在C#中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文