2010-04-14 48 views
1

我正在開發一個c#web應用程序與VS 2008,我試圖捕獲所選文件的物理路徑。使用IE,我終於可以在文本變量中檢索這個了。但我想用字符串捕獲這些信息。我該怎麼做呢?將文件路徑轉換爲字符串

目前我正在使用: lb3.Text = Page.Request.PhysicalPath;

,這讓我: 文本= 「C:\ Documents和Settings \管理\我的文檔\ Visual Studio 2008的\項目\ AddFileToSQL \ AddFileToSQL \ Default.aspx的」

謝謝你們您的意見/諮詢。我只是試圖捕獲一個字符串中的文件路徑。但是當我嘗試: string fullpath = Page.Request.PhysicalPath;

在我的C#代碼中,並在這一行上設置一個斷點,我看着Watch窗口並輸入完整路徑,它表示fullpath不在上下文中。你能理解這個嗎?我如何獲得一個字符串變量的路徑?

馬文,不知道你的意思,但是這是在上下文中更我的代碼:

protected void btnAppend_Click(object sender, EventArgs e) 
     { 
      Label lb3 = new Label(); 
      lb3.Text = Page.Request.PhysicalPath; 
      string fullpath2 = Request.PhysicalPath; 
+0

你想做什麼? – SLaks 2010-04-14 20:52:42

+0

'Request.PhysicalPath'已經是一個字符串。 – 2010-04-14 20:53:44

+0

這不是你想要的嗎?我認爲你給你的問題的答案=) – Nayan 2010-04-14 20:54:26

回答

3
string fullpath = Label lb3 = new Label(); 
lb3.Text = Page.Request.PhysicalPath; 
string fullpath2 = Request.PhysicalPath; 

Label lb3 = new Label(); 
lb3.Text = Page.Request.PhysicalPath; 
string fullpath2 = Page.Request.PhysicalPath; 

我測試了它和它的作品它得到一個字符串完整路徑,如果您稍後做了某些操作,則可能需要從字符串的開頭和結尾除掉引號

+0

謝謝,但這不適合我。請參閱我編輯的問題。 – salvationishere 2010-04-14 21:09:18

+0

更改字符串fullpath = Request.PhysicalPath爲完整路徑= Page.Request.PhysicalPath – 2010-04-14 21:27:03

+0

謝謝謝謝謝謝謝謝!這工作。我只是在請求之前錯過了頁面! – salvationishere 2010-04-14 21:40:55

2

「完整路徑超出上下文」 - 這可能發生的原因很少,主要是因爲字符串對象在您試圖觀看時超出範圍。

無論如何,你提供的代碼是不正確的(如在compilable),但我明白了。你應該能夠找到路徑。

protected void btnAppend_Click(object sender, EventArgs e) { 
    System.Diagnostics.Trace.WriteLine("DEBUGGING! -> Page.Request.PhysicalPath = " 
     + Page.Request.PhysicalPath); 

    Label lb3 = new Label(); 
    lb3.Text = Page.Request.PhysicalPath; 
    string fullpath2 = Page.Request.PhysicalPath; 

    System.Diagnostics.Trace.WriteLine("DEBUGGING! -> fullpath2 = " + fullpath2); 
} 

然後在IDE的「輸出」窗口中查看以查看結果。