2013-01-08 64 views
2

我有一個Windows服務託管一個wcf服務,以允許遠程文件和文件夾瀏覽。 Windows服務在本地系統帳戶下運行。c#wcf文件和文件夾瀏覽器

當瀏覽c:\驅動器時,服務報告該文件夾中的2800多個文件。 我單步穿過代碼,它確實報告> 2800個文件。

這怎麼可能是正確的?

C#代碼

//Files Manager 

public ReturnClass FindSubFiles(String Folder_To_Search, String User, String SessionId) 
{ 
    ReturnClass myReturnClass = new ReturnClass(-1, String.Empty, String.Empty, null, null, null, null); 
    try 
    { 
     Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", "", User, SessionId, 1); 
     string[] filePaths = Directory.GetFiles(Folder_To_Search); 
     int count = 0; 
     foreach (string Folder in filePaths) 
     { 
      filePaths[count] = Path.GetFileName(filePaths[count]); 

      count++; 
     } 
     myReturnClass.ErrorCode = 1; 
     myReturnClass.FilePaths = filePaths; 
     Logging.Write_To_Log_File("Exit", MethodBase.GetCurrentMethod().Name, "", "", "", "", User, SessionId, 1); 
     return myReturnClass; 
    } 
    catch (Exception ex) 
    { 
     Logging.Write_To_Log_File("Error", MethodBase.GetCurrentMethod().Name, "", "", ex.ToString(), "", User, SessionId, 2); 
     myReturnClass.ErrorCode = -1; 
     myReturnClass.ErrorMessage = ex.ToString(); 
     return myReturnClass; 
    } 
} 
+1

你考慮到隱藏文件或系統文件,如果有..? – MethodMan

+0

是的,但當我查看隱藏的文件只有大約30個文件左右。記得服務運行在本地系統帳戶,可能會看到甚至管理員無法看到的其他文件 - 我不知道... – user1438082

回答

1

我經過的是c中的路徑: 我應該怎麼傳遞是c:\\

C#代碼

public ReturnClass FindSubFiles(String Folder_To_Search , 
           String User, String SessionId) 
{ 
    ReturnClass myReturnClass = new ReturnClass(-1, String.Empty, String.Empty, 
               null, null, null, null); 
    try 
    { 
     Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, 
            "", "", "", "", User, SessionId, 1); 
     string[] filePaths = Directory.GetFiles(Folder_To_Search + "\\"); 
     int count = 0; 
     foreach (string Folder in filePaths) 
     { 
      filePaths[count] = Path.GetFileName(filePaths[count]); 

      count++; 
     } 
     myReturnClass.ErrorCode = 1; 
     myReturnClass.FilePaths = filePaths; 
     Logging.Write_To_Log_File("Exit", MethodBase.GetCurrentMethod().Name, 
            "", "", "", "", User, SessionId, 1); 
     return myReturnClass; 
    } 
    catch (Exception ex) 
    { 
     Logging.Write_To_Log_File("Error", MethodBase.GetCurrentMethod().Name, 
            "", "", ex.ToString(), "", User, SessionId, 2); 
     myReturnClass.ErrorCode = -1; 
     myReturnClass.ErrorMessage = ex.ToString(); 
     return myReturnClass; 
    } 
} 

感謝 達摩

1

粘貼此爲C#控制檯應用程序,看看它吐出來。您必須傳遞意想不到的事情在你的folder_To_Search,或有更多的文件比你想象中的C:\

var Folder_To_Search = @"c:\"; 
string[] filePaths = Directory.GetFiles(Folder_To_Search); 
int count = 0; 
foreach (string Folder in filePaths) 
{ 
    filePaths[count] = Path.GetFileName(filePaths[count]); 

    count++; 
} 
Console.WriteLine(count); 
Console.ReadKey();