2014-06-23 2 views
0

좋아, 그래서 여기에 약간의 문제가 있습니다. 내 웹 서버에서 ASP.NET (C#)을 사용하여 다른 폴더 내의 특정 폴더를 삭제하려고합니다. 삭제되는 폴더는 텍스트 상자를 기반으로합니다.특정 폴더 및 그 파일 삭제 ASP.NET

디렉토리는 문제는 내가하려고 모든 이미지 폴더 안에 모든 한 가지를 삭제한다는 것입니다이

/images/folderx 

folderx = txtDelFolder.Text; 

같다. 나는 그것이 파일 경로 내 폴더를 인식하지 못합니다 같은데요

문자열 경로 = @ "\ httpdocs 이미지 \ + txtDelFolder.Text \;

또한

을 시도

문자열 경로 = @ "\ httpdocs \ images \ + txtDelFolder.Text +"\ ";

는 모두 하나의 모든이 시도 '\' 더블 를 '\'

에 어떤 도움을 주셔서 감사합니다겠습니까 실제로 파일 경로 밖으로 입력 한 <directfilepath> 또한 그것은 말한다 곳 , 단지 그것을 여기에서 공유하고 싶지 않았다.

**** 편집 ****

string path = Server.MapPath("~/imagestest/" + txtEditTitle.Text); 

    if(Directory.Exists(path)) 
    { 
    DeleteDirectory(path); 
    } 
} 
} 
private void DeleteDirectory(string path) 
{ 
foreach(string filename in Directory.GetFiles(path)) 
{ 
File.Delete(filename); 
} 
foreach(string subfolders in Directory.GetDirectories(path)) 
{ 
Directory.Delete(subfolders, true); 
} 
} 
+0

코드를 보여주십시오. – tnw

+0

Server.MapPath()를 사용하여 폴더의 실제 위치를 가져옵니다. – malkam

+0

@MikeTech 코드를 주석으로 추가하지 마십시오. 완전히 읽을 수 없습니다. 원본 게시물을 서식을 지정하고 코드 서식 도구를 사용하십시오. – tnw

답변

2

이 시도 :

private void DeleteFiles(string folder) 
     { 
      string path=Server.MapPath("~/httpdocs/images/" + folder); 
      string[] files=Directory.GetFiles(path, "*", SearchOption.AllDirectories); 
      foreach (string file in files) 
      { 
       File.Delete(file); 
      } 
      //then delete folder 
       Directory.Delete(path); 

     } 
+0

IIS 내부에있는 디렉터리를 삭제할 때마다 IIS는 ApplicationDomain을 즉시 언로드 (효과적으로 응용 프로그램 재검색)한다는 점을 이해해야합니다. 응용 프로그램 외부에서 폴더를 사용하거나 폴더 만 손상시키지 않고 파일 만 제거 할 수 있습니다. http://blogs.msdn.com/b/toddca/archive/2006/07/17/668412.aspx – Isantipov

+0

기술적으로 내 디렉토리가 사이트의 루트에 있습니다. 여전히 동일한 효과가 있습니까? – MikeTech

+0

내가 이것을 시도했을 때/images /와/images/folder/ – MikeTech

0

이 하나의 시도 :

public void DeleteFolder(string folderPath) 
    { 
     if (!Directory.Exists(folderPath)) 
      return; 
     // get the directory with the specific name 
     DirectoryInfo dir = new DirectoryInfo(folderPath); 
     try 
     { 
      foreach (FileInfo fi in dir.GetFiles()) 
       fi.Delete(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
+0

에서 파일을 삭제하는 중이었습니다.이 파일을 사용하면 전혀 삭제되지 않았습니다. – MikeTech

0

표시되지 않는 이유는 않을 것 직장 :

public static bool DeleteDirectory(string input) 
{ 
    if (Directory.Exists(input)) 
    { 
     Directory.Delete(input, true); 
     return !Directory.Exists(input); 
    } 
    else 
     return true; 
} 

string thePath = Server.MapPath(@"~/images/"); 
thePath = Path.Combine(Path.GetFullPath(thePath), txtInput.Text); 

if(DeleteDirectory(thePath)) 
    Console.WriteLine("YAY"); 
else 
    Console.WriteLine("BOO"); 
+0

고마워, 내가 기회를 얻 자마자 시험해 보겠다. – MikeTech

관련 문제