2

我有一个托管 wcf 服务以允许远程文件和文件夹浏览的 Windows 服务。Windows 服务在本地系统帐户下运行。

浏览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;
    }
}
4

2 回答 2

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();
于 2013-01-08T21:43:48.090 回答
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;
    }
}

谢谢达摩

于 2013-01-13T15:29:59.050 回答