All pastes #778054 Raw Edit

Untitled

public text v1 · immutable
·published 2007-11-17 07:05 UTC
rendered paste body
internal static string[] InternalGetFileDirectoryNames(string path, string userPathOriginal, string searchPattern, bool includeFiles, bool includeDirs, SearchOption searchOption)
{
    string str5;
    int errorCode = 0;
    if ((searchOption != SearchOption.TopDirectoryOnly) && (searchOption != SearchOption.AllDirectories))
    {
        throw new ArgumentOutOfRangeException("searchOption", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
    }
    searchPattern = searchPattern.TrimEnd(new char[0]);
    if (searchPattern.Length == 0)
    {
        return new string[0];
    }
    Path.CheckSearchPattern(searchPattern);
    string fullPathInternal = Path.GetFullPathInternal(path);
    string[] pathList = new string[] { GetDemandDir(fullPathInternal, true) };
    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, pathList, false, false).Demand();
    string str2 = userPathOriginal;
    string directoryName = Path.GetDirectoryName(searchPattern);
    if ((directoryName != null) && (directoryName.Length != 0))
    {
        pathList = new string[] { GetDemandDir(Path.InternalCombine(fullPathInternal, directoryName), true) };
        new FileIOPermission(FileIOPermissionAccess.PathDiscovery, pathList, false, false).Demand();
        str2 = Path.Combine(str2, directoryName);
    }
    string str4 = Path.InternalCombine(fullPathInternal, searchPattern);
    char ch = str4[str4.Length - 1];
    if (((ch == Path.DirectorySeparatorChar) || (ch == Path.AltDirectorySeparatorChar)) || (ch == Path.VolumeSeparatorChar))
    {
        str4 = str4 + '*';
    }
    fullPathInternal = Path.GetDirectoryName(str4);
    bool flag = false;
    bool flag2 = false;
    ch = fullPathInternal[fullPathInternal.Length - 1];
    if ((ch == Path.DirectorySeparatorChar) || (ch == Path.AltDirectorySeparatorChar))
    {
        str5 = str4.Substring(fullPathInternal.Length);
    }
    else
    {
        str5 = str4.Substring(fullPathInternal.Length + 1);
    }
    Win32Native.WIN32_FIND_DATA win_find_data = new Win32Native.WIN32_FIND_DATA();
    SafeFindHandle hndFindFile = null;
    SearchData item = new SearchData(fullPathInternal, str2, searchOption);
    List<SearchData> list = new List<SearchData>();
    list.Add(item);
    List<string> list2 = new List<string>();
    int num2 = 0;
    int length = 0;
    string[] sourceArray = new string[10];
    int newMode = Win32Native.SetErrorMode(1);
    try
    {
        while (list.Count > 0)
        {
            string str6;
            item = list[list.Count - 1];
            list.RemoveAt(list.Count - 1);
            ch = item.fullPath[item.fullPath.Length - 1];
            flag = (ch == Path.DirectorySeparatorChar) || (ch == Path.AltDirectorySeparatorChar);
            if (item.userPath.Length > 0)
            {
                ch = item.userPath[item.userPath.Length - 1];
                flag2 = (ch == Path.DirectorySeparatorChar) || (ch == Path.AltDirectorySeparatorChar);
            }
            if (item.searchOption != SearchOption.TopDirectoryOnly)
            {
                try
                {
                    if (flag)
                    {
                        str6 = item.fullPath + "*";
                    }
                    else
                    {
                        str6 = item.fullPath + Path.DirectorySeparatorChar + "*";
                    }
                    hndFindFile = Win32Native.FindFirstFile(str6, win_find_data);
                    if (hndFindFile.IsInvalid)
                    {
                        errorCode = Marshal.GetLastWin32Error();
                        if (errorCode == 2)
                        {
                            continue;
                        }
                        __Error.WinIOError(errorCode, item.fullPath);
                    }
                    do
                    {
                        if ((((win_find_data.dwFileAttributes & 0x10) != 0) && !win_find_data.cFileName.Equals(".")) && !win_find_data.cFileName.Equals(".."))
                        {
                            SearchData data2 = new SearchData();
                            StringBuilder builder = new StringBuilder(item.fullPath);
                            if (!flag)
                            {
                                builder.Append(Path.DirectorySeparatorChar);
                            }
                            builder.Append(win_find_data.cFileName);
                            data2.fullPath = builder.ToString();
                            builder.Length = 0;
                            builder.Append(item.userPath);
                            if (!flag2)
                            {
                                builder.Append(Path.DirectorySeparatorChar);
                            }
                            builder.Append(win_find_data.cFileName);
                            data2.userPath = builder.ToString();
                            data2.searchOption = item.searchOption;
                            list.Add(data2);
                        }
                    }
                    while (Win32Native.FindNextFile(hndFindFile, win_find_data));
                }
                finally
                {
                    if (hndFindFile != null)
                    {
                        hndFindFile.Dispose();
                    }
                }
            }
            try
            {
                if (flag)
                {
                    str6 = item.fullPath + str5;
                }
                else
                {
                    str6 = item.fullPath + Path.DirectorySeparatorChar + str5;
                }
                hndFindFile = Win32Native.FindFirstFile(str6, win_find_data);
                if (hndFindFile.IsInvalid)
                {
                    errorCode = Marshal.GetLastWin32Error();
                    if (errorCode == 2)
                    {
                        continue;
                    }
                    __Error.WinIOError(errorCode, item.fullPath);
                }
                num2 = 0;
                do
                {
                    bool flag3 = false;
                    if (includeFiles)
                    {
                        flag3 = 0 == (win_find_data.dwFileAttributes & 0x10);
                    }
                    if ((includeDirs && ((win_find_data.dwFileAttributes & 0x10) != 0)) && (!win_find_data.cFileName.Equals(".") && !win_find_data.cFileName.Equals("..")))
                    {
                        flag3 = true;
                    }
                    if (flag3)
                    {
                        num2++;
                        if (length == sourceArray.Length)
                        {
                            string[] strArray3 = new string[sourceArray.Length * 2];
                            Array.Copy(sourceArray, 0, strArray3, 0, length);
                            sourceArray = strArray3;
                        }
                        sourceArray[length++] = Path.InternalCombine(item.userPath, win_find_data.cFileName);
                    }
                }
                while (Win32Native.FindNextFile(hndFindFile, win_find_data));
                errorCode = Marshal.GetLastWin32Error();
                if (num2 > 0)
                {
                    list2.Add(GetDemandDir(item.fullPath, true));
                }
                continue;
            }
            finally
            {
                if (hndFindFile != null)
                {
                    hndFindFile.Dispose();
                }
            }
        }
    }
    finally
    {
        Win32Native.SetErrorMode(newMode);
    }
    if (((errorCode != 0) && (errorCode != 0x12)) && (errorCode != 2))
    {
        __Error.WinIOError(errorCode, item.fullPath);
    }
    if (list2.Count > 0)
    {
        pathList = new string[list2.Count];
        list2.CopyTo(pathList, 0);
        new FileIOPermission(FileIOPermissionAccess.PathDiscovery, pathList, false, false).Demand();
    }
    if (length == sourceArray.Length)
    {
        return sourceArray;
    }
    string[] destinationArray = new string[length];
    Array.Copy(sourceArray, 0, destinationArray, 0, length);
    return destinationArray;
}