2010-12-03 22 views
0

私は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 \ "にダウンロードしてください。私はこれで非常に混乱しています、これは初めてこれまで何かが起こったのです。誰もがこのプロジェクトを完了するために私の方法で立っている唯一のこと、これで私を助けてくださいできる?Visual Studio 2010作業ディレクトリ

+0

私はあなたが精神的にではなく、特に難しいことを望むかもしれないと思います... – Paddy

答えて

1

作業ディレクトリをまったく使用したくない場合。あなたは、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開発は私の最強のスキルではありません。)ページがを持っているので、このコードはページのコードビハインドに表示された場合、あなたはおそらく同様にHttpContext.Currentをカットすることができます

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

:上記のコードは、この(非常に簡単)のコードの本質的に同等ですプロパティ。

+2

+1、しかしおそらくもっと簡単な方法はrequest.MapPath( "〜/ Users")です! – VinayC

+0

@VinayC:そうです! :) –

+0

おかげでこれは大きな助けとなりました – Madao

0

これは、問題は解決しましたが、ダウンロードはまだ適切な場所からダウンロードされていないため、プログラムは "ここで共有\ DevServer \ 10.0 \」ディレクトリには、私は私がcheckbox--

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();

--downloadボタンCode-- に

// 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>

を移入する

--codeを使用しているコードです私のアプリケーションディレクトリを指すようにダウンロードを取得する方法がわからない、私は考えることができるすべてを試しました。