2013-02-28 128 views
2

我有一個MVC4應用程序,一個控制器方法需要一些文件(這個文件有一些Json對象),讀它proccessit然後刪除它。獲取文件的相對路徑asp.net

這些文件上傳到我的網站使用FTP。我的項目結構是這樣的行爲

wwwroot 
    bin 
    content 
    images 
    scripts 
    suscribers // This is my ftp folder 
    _suscriber1 
     _proccess1 
     file1.txt //This is a Json file 
     file2.txt 
     ... 
     _proccess2 
     ... 
    _suscriber2 
    ... 

我的所有文件(file1.txt ...)都加載好了。在我的控制,我要看書FILE1.TXT這樣:

string suscriberDir= string.Format("_{0}", suscriber.Id); 
string[] laPath = {System.AppDomain.CurrentDomain.BaseDirectory, "suscribers", suscriberDir}; 
string lcPath = Path.Combine(laPath); 
string[] laPath2 = { lcPath, "_proccess1" , "_File1.txt" }; 
lcPath = Path.Combine(laPath2); 
StreamReader reader = new StreamReader(lcPath); 
string personas = reader.ReadToEnd(); 
reader.Close(); 

我problema是,它扔我一個filenotfound例外。

讀取file1.txt並獲取其內容的正確方法是什麼?

+1

您是否調試過此代碼?我認爲「CurrentDomain.BaseDirectory」是「bin \ Debug」文件夾。你應該嘗試在那裏拋出一個斷點,並做舊時調試 – 2013-02-28 02:58:26

+0

是的,我正在調試它..「舊時尚」 – 2013-02-28 03:05:18

回答

7

嘗試使用Server.MapPath("_File1.txt");

+1

_File1.txt可能存在於幾個迪爾..這是方式? – 2013-02-28 03:06:36

+1

是的,你說得對,只是我的控制器在bin dir。我的最終解決方案'Server.MapPath(「../ suscribers/_suscriber1/_proccess1/File1.txt」)'。 – 2013-02-28 03:23:19