Dev2.PathOperations.Dev2FileSystemProvider.ListDirectoriesAccordingToType C# (CSharp) Method

ListDirectoriesAccordingToType() private method

private ListDirectoriesAccordingToType ( IActivityIOPath src, ReadTypes type ) : IList
src IActivityIOPath
type ReadTypes
return IList
        private IList<IActivityIOPath> ListDirectoriesAccordingToType(IActivityIOPath src, ReadTypes type)
        {
            IList<IActivityIOPath> result = new List<IActivityIOPath>();

            string path = src.Path;

            if (!path.EndsWith("\\") && PathIs(src) == enPathType.Directory)
            {
                path += "\\";
            }

            if (!RequiresAuth(src))
            {
                try
                {

                    IEnumerable<string> dirs;

                    if (!Dev2ActivityIOPathUtils.IsStarWildCard(path))
                    {
                        if (Directory.Exists(path))
                        {
                            dirs = GetDirectoriesForType(path, string.Empty, type);
                        }
                        else
                        {
                            throw new Exception("The Directory does not exist.");
                        }
                    }
                    else
                    {
                        // we have a wild-char path ;)
                        string baseDir = Dev2ActivityIOPathUtils.ExtractFullDirectoryPath(path);
                        string pattern = Dev2ActivityIOPathUtils.ExtractFileName(path);

                        dirs = GetDirectoriesForType(baseDir, pattern, type);
                    }

                    if (dirs != null)
                    {
                        foreach (string d in dirs)
                        {
                            result.Add(ActivityIOFactory.CreatePathFromString(d, src.Username, src.Password, true,src.PrivateKeyFile));
                        }
                    }
                }
                catch (Exception)
                {
                    throw new Exception("Directory not found [ " + src.Path + " ] ");
                }
            }
            else
            {

                try
                {
                    // handle UNC path
                    SafeTokenHandle safeTokenHandle;
                    bool loginOk = LogonUser(ExtractUserName(src), ExtractDomain(src), src.Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);

                    if (loginOk)
                    {
                        using (safeTokenHandle)
                        {

                            WindowsIdentity newID = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
                            using (WindowsImpersonationContext impersonatedUser = newID.Impersonate())
                            {
                                // Do the operation here

                                try
                                {

                                    IEnumerable<string> dirs;

                                    if (!Dev2ActivityIOPathUtils.IsStarWildCard(path))
                                    {
                                        dirs = GetDirectoriesForType(path, string.Empty, type);
                                    }
                                    else
                                    {
                                        // we have a wild-char path ;)
                                        string baseDir = Dev2ActivityIOPathUtils.ExtractFullDirectoryPath(path);
                                        string pattern = Dev2ActivityIOPathUtils.ExtractFileName(path);

                                        dirs = GetDirectoriesForType(baseDir, pattern, type);
                                    }

                                    if (dirs != null)
                                    {
                                        foreach (string d in dirs)
                                        {
                                            result.Add(ActivityIOFactory.CreatePathFromString(d, src.Username, src.Password, src.PrivateKeyFile));
                                        }
                                    }

                                }
                                catch (Exception)
                                {
                                    throw new Exception("Directory not found [ " + src.Path + " ] ");
                                }

                                // remove impersonation now
                                impersonatedUser.Undo();
                                newID.Dispose();
                            }
                        }
                    }
                    else
                    {
                        // login failed
                        throw new Exception("Failed to authenticate with user [ " + src.Username + " ] for resource [ " + src.Path + " ] ");
                    }
                }
                catch (Exception ex)
                {
                    Dev2Logger.Log.Error(ex);
                    throw;
                }

            }

            return result;
        }