簡體   English   中英

Visual Studio 2010工作目錄

[英]Visual Studio 2010 working directory

我正在開發一個C#asp.net Web應用程序。 我基本上已經完成了,但是我有這個小問題。 我想將xml文件保存到項目中的“ Users”文件夾中,但是如果我不願意對項目中的路徑“ C:...... \\ Users”進行硬編碼,則希望將文件保存在此目錄中。 “ C:\\ Program Files(x86)\\ Common Files \\ microsoft shared \\ DevServer \\ 10.0 \\ Users”文件夾,這是一個令人討厭的問題,因為我無法在我們的Web主機服務器上使用硬編碼目錄。 另外,我有一個復選框列表,該列表從我的項目的“ DownloadLibrary”文件夾中填充,並且它假定是從該折疊中下載文件,但它也查找到“ C:\\ Program Files(x86)\\ Common Files \\ microsoft共享\\ DevServer \\ 10.0 \\”文件夾下載,即使它是從正確的文件夾中填充的。 我對此感到非常困惑,這是我第一次發生這樣的事情。 誰能幫我這個忙,這是我完成該項目的唯一途徑。

您根本不想使用工作目錄; 您想要使用相對於Web應用程序所在位置的目錄(可以從HttpRequest.ApplicationPath檢索到)。

HttpRequest request = HttpContext.Current.Request;
// get the physical path to the web application
string pathToApp = request.MapPath(request.ApplicationPath);
string usersPath = System.IO.Path.Combine(pathToApp, "Users");

更新資料
正如VincayC指出的; asp.net開發不是我最強的技能;)上面的代碼本質上等效於此代碼(簡單得多):

string usersPath = HttpRequest.Current.Request.MapPath("~/Users");

如果此代碼顯示在頁面的代碼后面,則由於該頁面具有Request屬性,因此您也可以剪切HttpContext.Current

那確實解決了我遇到的一個問題,但是下載仍然沒有從正確的位置下載,程序仍然希望從“ C:\\ Program Files(x86)\\ Common Files \\ microsoft shared \\ DevServer \\ 10.0 \\“目錄是我正在使用的代碼

-填充復選框的代碼-

HttpRequest request = HttpContext.Current.Request;
// get the physical path to the web application
string appPath = request.MapPath(request.ApplicationPath);
string directory = System.IO.Path.Combine(appPath, "DownloadLibrary/");

// Get the list of files into the CheckBoxList
var dirInfo = new DirectoryInfo(directory);

cblFiles.DataSource = dirInfo.GetFiles();
cblFiles.DataBind();

-下載按鈕代碼-

// Tell the browser we're sending a ZIP file!
            var downloadFileName = string.Format("Items-{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));
            Response.ContentType = "application/zip";
            Response.AddHeader("Content-Disposition", "filename=" + downloadFileName);

// Zip the contents of the selected files using (var zip = new ZipFile()) { // Add the password protection, if specified /*if (!string.IsNullOrEmpty(txtZIPPassword.Text)) { zip.Password = txtZIPPassword.Text; // 'This encryption is weak! Please see http://cheeso.members.winisp.net/DotNetZipHelp/html/24077057-63cb-ac7e-6be5-697fe9ce37d6.htm for more details zip.Encryption = EncryptionAlgorithm.WinZipAes128; }*/ // Construct the contents of the README.txt file that will be included in this ZIP var readMeMessage = string.Format("Your ZIP file {0} contains the following files:{1}{1}", downloadFileName, Environment.NewLine); // Add the checked files to the ZIP foreach (ListItem li in cblFiles.Items) if (li.Selected) { // Record the file that was included in readMeMessage readMeMessage += string.Concat("\t* ", li.Text, Environment.NewLine); // Now add the file to the ZIP (use a value of "" as the second parameter to put the files in the "root" folder) zip.AddFile(li.Value, "Your Files"); } // Add the README.txt file to the ZIP //zip.AddEntry("README.txt", readMeMessage, Encoding.ASCII); // Send the contents of the ZIP back to the output stream zip.Save(Response.OutputStream);</pre></code>

我不確定如何使下載指向我的應用程序目錄,我嘗試了所有可以想到的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM