2013-02-20 1 views
0

방법에 관한 것이다. 내가 사용하는 경로에는 약 3800 개의 파일과 375 개의 하위 폴더가 있습니다. 페이지를 렌더링하는 데 약 45 초가 걸리지 만, 내 사용자에게는 받아 들일 수 없으므로이 시간을 줄일 수있는 방법이 있습니다.재귀 디렉토리 구조 리스팅 내가 GET 변수에 의해 제공되는 경로 (루트)에서 시작 아래의 코드를 사용하여 반복적으로 모든 하위 폴더로 가서 목록 항목으로 내용을 디스플레이하고

string output; 
protected void Page_Load(object sender, EventArgs e) { 
    getDirectoryTree(Request.QueryString["path"]); 
    itemWrapper.InnerHtml = output; 
} 

private void getDirectoryTree(string dirPath) { 
    try { 
     System.IO.DirectoryInfo rootDirectory = new System.IO.DirectoryInfo(dirPath); 
     foreach (System.IO.DirectoryInfo subDirectory in rootDirectory.GetDirectories()) { 
      output = output + "<ul><li><a>" + Regex.Replace(subDirectory.Name, "_", " "); 
      if (subDirectory.GetFiles().Length != 0 || subDirectory.GetDirectories().Length != 0) { 
       output = output + " +</a>"; 
      } else { 
       output = output + "</a>"; 
      } 
      getDirectoryTree(subDirectory.FullName); 
      if (subDirectory.GetFiles().Length != 0) { 
       output = output + "<ul>"; 
       foreach (System.IO.FileInfo file in subDirectory.GetFiles()) { 
        output = output + "<li><a href='" + file.FullName + "'>" + file.Name + "</a></li>"; 
       } 
       output = output + "</ul>"; 
      } 
      output = output + "</li></ul>"; 
     } 
    } catch (System.UnauthorizedAccessException) { 
     //This throws when we don't have access. 
    } 
} 
+0

사용자가 확장 된 항목을 한 번에 모두 볼 수 있습니까? 사용자가 구조를 클릭 할 때 필요에 따라 폴더를 가져 오지 않는 이유는 무엇입니까? – Alex

+0

성능 적중의 대부분은 디스크 I/O에있을 것입니다. – Lloyd

+0

@Alex 어떻게 C# + ASP.NET이 새로운 것인가, 나는 Java C# + ASP.NET의 모든 측면을 배우는 Java Certified Associate입니다. –

답변

0
  • 대신 문자열 CONCATENATE (불변) 나쁜 성능 System.Text.StringBuilder (좋은 성능)를 사용합니다.
  • 일반적인 문자열 바꾸기 기능을 사용해야합니다. 복잡한 검색을 사용하고 있지 않습니다. subDirectory.Name.replace ("_", "");
0

코드 속도가 느린 주된 원인은 GetFiles 및 GetDirectories에 대한 여러 호출 일 가능성이 큽니다. 처음 조회 때와 마찬가지로 if 조건에서 반복적으로 전화를 걸고 있습니다. 카운트는 한 번만 필요합니다. 또한 문자열을 추가하는 것이 원인을 돕지 않습니다.

다음 코드는이 300ms 내 간단한 USB 드라이브를 통해 실행하고 400 개 이상의 폴더와 11000 개 파일을 반환 할 수 있었다. 느린 네트워크 드라이브에서는 300 개의 폴더에 4000 개의 파일을 9 초 안에 반환 할 수있었습니다. 재귀 중에 Parallel.ForEach으로 더 최적화 할 수 있습니다.

protected void Page_Load(object sender, EventArgs e) { 

    itemWrapper.InnerHtml = GetDirectory(Request.QueryString["path"]); 
} 

static string GetDirectory(string path) 
{ 
    StringBuilder output = new StringBuilder(); 
    var subdir = System.IO.Directory.GetDirectories(path); 
    var files = System.IO.Directory.GetFiles(path); 
    output.Append("<ul><li><a>"); 
    output.Append(path.Replace("_", " ")); 
    output.Append(subdir.Length > 0 || files.Length > 0 ? "+</a>" : "</a>"); 

    foreach(var sb in subdir) 
    { 
     output.Append(GetDirectory(sb)); 
    } 

    if (files.Length > 0) 
    { 
     output.Append("<ul>"); 
     foreach (var file in files) 
     { 
      output.AppendFormat("<li><a href =\"{0}\">{1}</a></li>", file, System.IO.Path.GetFileName(file)); 
     } 
     output.Append("</ul>"); 
    } 
    output.Append("</ul>"); 
    return output.ToString(); 
}