ASP.NET檢查檔案是否存在

來源:互聯網
上載者:User

本教程表明我們如何能夠驗證檔案是否存在,或沒有收到執行代碼錶示,有關檔案。 C #中的版本。


檢查是否存在一個檔案之前執行某些行代碼可以是非常有益的。它可以防止錯誤資訊所造成的試圖操縱的檔案不存在,例如。使用System.IO ,我們可以查看是否存在一個檔案在某一目錄提供方便。

using System.IO;

伺服器智力助攻,各種規模的企業與他們主辦的需求,提供完全配置的伺服器解決方案加上主動伺服器管理服務。伺服器智力專門提供完整的互連網伺服器解決方案,隨時準備支援他們的專家三百六十五分之二十四積極支援小組。

ASPX頁面看起來就像這樣:

<form id="form1" runat="server">
Search for a file (inc. extension to see if it exists in the media directory.<br />
Hint: try delete.gif<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Go See" /><br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>

我們遷移的網站伺服器智力的一個周末和安裝是如此順利,我們正在啟動和運行了。他們協助我們的一切,我們需要做的所有的應用程式。隨著伺服器智力的協助下,我們能夠避免任何麻煩www.111cn.net/!

邏輯檢查,看看是否有任何文字的文本時,按下按鈕,首先。如果有文本目前,它會檢查,看看是否這個文字檔是一個在媒體上的檔案夾中。使用者將得到通知,如果存在一個檔案或沒有。
程式碼後置看起來就像這樣:

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 Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
doesFileExist(TextBox1.Text);
}

public void doesFileExist(string searchString)
{
if (TextBox1.Text != "")
{
string imageFolder;
imageFolder = Server.MapPath("/media/") + searchString.ToString();
if (File.Exists(imageFolder))
{
Label1.Text = "File <b>" + searchString + "</b> <u>does</u> exist in '/media/' folder.";
}
else
{
Label1.Text = "File <b>" + searchString + "</b> <u>does not</u> exist in '/media/' folder.";
}
}
else
Label1.Text = "Please enter some text.";
}

}

轉載請註明: www.111cn.net/net/net.html   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.