Esempio n. 1
0
        /// <summary>
        /// Mainly a copy from the internal .Net System.IO.Directory.DeleteHelper
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="recursive"></param>
        private static void DeleteHelper(string fullPath, bool recursive)
        {
            int lastWin32Error;
            Exception exception = null;

            if (recursive)
            {
                WIN32_FIND_DATA data = new WIN32_FIND_DATA();

                var handle = NativeWin32Methods.FindFirstFileW(fullPath + System.IO.Path.DirectorySeparatorChar + "*", out data);

                try
                {

                    #region Check FindFirstFileW handle

                    if (handle.ToInt64() < 0)
                    {
                        lastWin32Error = Marshal.GetLastWin32Error();
                        throw new System.ComponentModel.Win32Exception(lastWin32Error);
                    }

                    #endregion

                    #region Go through each entry

                    do
                    {
                        if (0 != ((int)data.dwFileAttributes & 0x10))
                        {

                            #region This is a directory

                            if (!data.cFileName.Equals(".") && !data.cFileName.Equals(".."))
                            {

                                if (0 == ((int)data.dwFileAttributes & 0x400))//& FileAttributes.ReparsePoint))
                                {

                                    #region Regular directory

                                    string str = System.IO.Path.Combine(fullPath, data.cFileName);
                                    try
                                    {
                                        DeleteHelper(str, recursive);
                                    }
                                    catch (Exception exception2)
                                    {
                                        if (exception == null)
                                        {
                                            exception = exception2;
                                        }
                                    }

                                    #endregion

                                }
                                else
                                {

                                    #region VolumeMountPoint

                                    if ((data.dwReserved0 == -1610612733) && !NativeWin32Methods.DeleteVolumeMountPoint(System.IO.Path.Combine(fullPath, data.cFileName + System.IO.Path.DirectorySeparatorChar)))
                                    {
                                        lastWin32Error = Marshal.GetLastWin32Error();
                                        throw new System.ComponentModel.Win32Exception(lastWin32Error);
                                    }
                                    if (!NativeWin32Methods.RemoveDirectoryW(System.IO.Path.Combine(fullPath, data.cFileName)))
                                    {
                                        lastWin32Error = Marshal.GetLastWin32Error();
                                        throw new System.ComponentModel.Win32Exception(lastWin32Error);
                                    }

                                    #endregion

                                }

                            }

                            #endregion

                        }
                        else if (!NativeWin32Methods.DeleteFileW(System.IO.Path.Combine(fullPath, data.cFileName)))
                        {
                            lastWin32Error = Marshal.GetLastWin32Error();
                            throw new System.ComponentModel.Win32Exception(lastWin32Error);
                        }
                    }
                    while (NativeWin32Methods.FindNextFileW(handle, out data));

                    #endregion

                    #region Check for erros & clean up

                    lastWin32Error = Marshal.GetLastWin32Error();

                    if (lastWin32Error != 18) //ERROR_NO_MORE_FILES
                    {
                        throw new System.ComponentModel.Win32Exception(lastWin32Error);
                    }

                    #endregion

                }
                finally
                {
                    NativeWin32Methods.FindClose(handle);
                }

            }

            #region Remove directory

            if (!NativeWin32Methods.RemoveDirectoryW(fullPath))
            {
                lastWin32Error = Marshal.GetLastWin32Error();
                switch (lastWin32Error)
                {
                    case 2:
                        lastWin32Error = 3;
                        break;

                    case 5:
                        //throw new IOException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), new object[] { userPath }));
                        break;
                }
                throw new System.ComponentModel.Win32Exception(lastWin32Error);
            }

            #endregion
        }
Esempio n. 2
0
 internal static extern bool FindNextFileW(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);
Esempio n. 3
0
        public static IEnumerable<String> EnumerateFiles(string myPath, string mySearchPattern)
        {
            #if(__MonoCS__)

            return Directory.EnumerateFiles(myPath, mySearchPattern);

            #else

            var path = PathHandler.GetFullPathInternal(myPath);
            if (!Exists(path))
            {
                throw new DirectoryNotFoundException(path);
            }

            int lastWin32Error;
            var data   = new WIN32_FIND_DATA();
            var handle = NativeWin32Methods.FindFirstFileW(path + mySearchPattern, out data);

            try
            {
                #region Check FindFirstFileW handle

                if (handle.ToInt64() < 0)
                {
                    lastWin32Error = Marshal.GetLastWin32Error();
                    if (lastWin32Error == 2)
                    {
                        yield break; // no files found for this pattern
                    }
                    else
                    {
                        throw new System.ComponentModel.Win32Exception(lastWin32Error);
                    }
                }

                #endregion

                #region Go through each entry

                do
                {

                    yield return System.IO.Path.Combine(myPath, data.cFileName);

                }
                while (NativeWin32Methods.FindNextFileW(handle, out data));

                #endregion

                #region Check for erros & clean up

                lastWin32Error = Marshal.GetLastWin32Error();

                if (lastWin32Error != 18) //ERROR_NO_MORE_FILES
                {
                    throw new System.ComponentModel.Win32Exception(lastWin32Error);
                }

                #endregion

            }
            finally
            {
                NativeWin32Methods.FindClose(handle);
            }

            #endif
        }
Esempio n. 4
0
 internal static extern IntPtr FindFirstFileW(string lpFileName, out WIN32_FIND_DATA lpFindFileData);