2010-12-03 2 views
0

C# asp.net 웹 응용 프로그램을 개발 중입니다. 나는 기본적으로 그걸로 끝났지 만, 나는이 작은 문제가 있습니다. 내 프로젝트 내에서 "Users"폴더에 XML 파일을 저장하고 싶지만 내 프로젝트에서 "C : ...... \ Users"경로에 정신적으로 하드 코드하지 않으면이 파일을이 파일에 저장하려고합니다. "C : \ Program Files (x86) \ Common Files \ microsoft shared \ DevServer \ 10.0 \ Users"폴더의 경우 웹 호스트 서버에서 하드 코딩 된 디렉토리를 사용할 수 없기 때문에 이것은 성가신 문제입니다. 또한, 내 프로젝트의 "DownloadLibrary"폴더에서 채우고 해당 폴더에서 파일을 다운로드한다고 가정하지만 해당 "C : \ Program Files (x86) \ Common Files \ microsoft 공유 \ DevServer \ 10.0 \ "올바른 폴더에서 채우는 경우에도 폴더를 다운로드하십시오. 나는 이것에 매우 혼란 스럽습니다. 처음으로 이런 일이 일어났습니다. 누구나이 프로젝트를 완료하는 유일한 방법은 이걸 도와주세요.Visual Studio 2010 작업 디렉토리

+0

나는 당신이 육체적으로 오히려 정신적으로, 특히 어려운 일이 될 것이라고 생각합니다 ... – Paddy

답변

1

작업 디렉토리를 전혀 사용하고 싶지 않습니다. 당신은 웹 응용 프로그램의 위치에 상대적인 디렉토리 사용하려면 (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

그게 내가 겪고있는 한 가지 문제를 해결했지만 다운로드가 여전히 올바른 장소에서 다운로드되지 않고 있으며 프로그램은 여전히 ​​"C : \ Program Files (x86) \ Common Files \ microsoft"파일을 가져오고 싶어합니다. 공유 \ 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을 사용하고 코드입니다 \ 내 응용 프로그램 디렉토리를 가리 키도록 다운로드하는 방법을 모르겠다. 나는 내가 생각할 수있는 모든 것을 시도했다.