如果文件存在 [英] If File Exists
本文介绍了如果文件存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此刻,我正在根据下拉列表选择以下内容来更改pdf查看器的文件路径
At the moment I''m using the following to change the file path of a pdf viewer depending on a dropdownlist selection
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default : System.Web.UI.Page
{
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// Make sure you get a valid file path to a pdf in DropDownList1.SelectedValue
ShowPdf1.FilePath = "attachments/" + DropDownList1.SelectedValue + ".pdf#toolbar=0&navpanes=0&zoom=50";
}
}
我想知道是否可以执行以下操作:
I was wondering if it would be possible to do something like the following:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default : System.Web.UI.Page
{
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
If "attachments/" + DropDownList1.SelectedValue + ".pdf" exists
then
ShowPdf1.FilePath = "attachments/" + DropDownList1.SelectedValue + ".pdf#toolbar=0&navpanes=0&zoom=50";
else
ShowPdf1.FilePath = "attachments/NoFile.pdf#toolbar=0&navpanes=0&zoom=50";
}
}
推荐答案
我认为您的意思是MapPath.这就是您从虚拟地址转到文件地址的方式.
http://msdn.microsoft.com/en-us/library/ms178116.aspx [ ^ ]
祝你好运!
I think you mean MapPath. That''s how you can go from virtual address to file address.
http://msdn.microsoft.com/en-us/library/ms178116.aspx[^]
Good luck!
谢谢,它使我走上了通往解决方案的正确之路.
Thankyou it put me on the right path to a Solution.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class Default : System.Web.UI.Page
{
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string filePath = Request.PhysicalApplicationPath + "attachments/" + DropDownList1.SelectedValue + ".pdf";
FileInfo imageFile = new FileInfo(filePath);
bool fileExists = imageFile.Exists;
if (fileExists == false)
ShowPdf1.FilePath = "attachments/Nofile.pdf#toolbar=0&navpanes=0&zoom=100";
else
ShowPdf1.FilePath = "attachments/" + DropDownList1.SelectedValue + ".pdf#toolbar=0&navpanes=0&zoom=50";
}
}
这篇关于如果文件存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文