2013-03-03 2 views
0

이 프로그램은 디렉토리 및 디렉토리의 경로를 표시해야한다고 가정합니다. 디렉토리가 존재하면 그 디렉토리에 다음 확장자 (예 : .doc, .pdf, .jpg,. JPEG) 나는 코드가 실행되는 bin 디렉토리에 최대라는 폴더를 생성하지만 이것이 필요한 권한 코드를하도록해야내 폴더 또는 디렉토리에 액세스하지 않음

namespace sharp_lab_2 
{ 
    interface IFileOperation 
    { 
     //should handle the file 
     bool Accept(string fileName); 
     //handler function file 
     void Process(string fileName); 
    } 

    class FileProcByExt : IFileOperation 
    { 
     string extName; 
     string folderName; 
     public FileProcByExt(string ext = "") 
     { 
      extName = ext; 
      if (extName == "") 
       folderName = "MAXee"; 
      else 
       folderName = extName.ToUpper(); 
     } 
     public bool Accept(string fileName) 
     { 
      bool res = false; 
      if (extName == "") res = true; // all 
      if (Path.GetExtension(fileName) == "." + extName) res = true; // has extension 
      return res; 
     } 
     public void Process(string fileName) 
     { 
      Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(fileName), 
                folderName)); 
      File.Move(fileName, 
         Path.Combine(Path.GetDirectoryName(fileName), 
            folderName, 
            Path.GetFileName(fileName)));    
     } 
    } 
    class FileProcNameAfter10 : IFileOperation 
    { 
     static int cnt; 
     public bool Accept(string fileName) 
     { 
      return Path.GetFileNameWithoutExtension(fileName).Length > 10; 
     } 
     public void Process(string fileName) 
     { 
      File.Copy(fileName, 
         Path.Combine(Path.GetDirectoryName(fileName), 
            "longname_" + cnt + Path.GetExtension(fileName))); 
      cnt++; 
     } 
    } 
    class FileProcEnc: IFileOperation 
    { 
     public bool Accept(string fileName) 
     { 
      FileInfo fi = new FileInfo(fileName); 
      return fi.Length < 1024; 
     } 
     public void Process(string fileName) 
     { 
      File.Encrypt(fileName); 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      {    
       string directoryPath = args[0]; 
       string[] filesList, filesListTmp; 
       IFileOperation[] opList = { new FileProcNameAfter10(), 
              new FileProcEnc(), 
              new FileProcByExt("jpeg"), 
              new FileProcByExt("jpg"), 
              new FileProcByExt("doc"), 
              new FileProcByExt("pdf"), 
              new FileProcByExt("djvu") 
              }; 
       if (Directory.Exists(directoryPath)) 
       { 
        filesList = Directory.GetFiles(directoryPath); 
        while (true) 
        { 
         Thread.Sleep(500); 
         filesListTmp = Directory.GetFiles(directoryPath); 
         foreach (var elem in Enumerable.Except<string>(filesListTmp, filesList)) 
         { 
          Console.WriteLine(elem); 
          foreach (var op in opList) 
          { 
           if (op.Accept(elem)) op.Process(elem); 
          }  
         } 
         filesList = filesListTmp; 
         if (Console.KeyAvailable == true && Console.ReadKey(true).Key == ConsoleKey.Escape) break; 
        } 
       } 
       else 
       { 
        Console.WriteLine("There is no such directory."); 
        Console.ReadKey(); 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Error." + ex.Message); 

      } 
     } 
    } 
} 
+1

관련 코드 만 게시하십시오. 모든 코드가 적절하다고 생각되면 최소한의 코드로 문제를 재현 한 다음 게시하십시오. –

+0

@ MD.Unicorn try – thanks

+0

당신은 asp.net로 일하고 있습니까? 붙여 넣기 폴더 directoryName을 가져 오는 방법이 다를 수 있기 때문에. 올바른 붙여 넣기 주소를 넣었습니까? – MayogaX

답변

0

아래에있는 내 코드 디렉토리가 존재하지 않음을 말해. 디렉터리에 대한 읽기 전용 권한이 없으면 Exists 메서드는 false를 반환합니다. Exists 메서드는 지정된 파일이 있는지 확인하는 동안 오류가 발생하면 false를 반환합니다. 유효하지 않은 문자 또는 너무 많은 문자가 포함 된 파일 이름, 실패 또는 누락 된 디스크 또는 전달자에게 파일을 읽을 수있는 권한이없는 경우와 같은 예외를 발생시키는 상황에서 발생할 수 있습니다.

msdn

관련 문제