示例#1
0
文件: File.cs 项目: etad4e5/coreclr
        internal static void InternalDelete(String path, bool checkHost)
        {
            String fullPath = Path.GetFullPath(path);

            if (checkHost)
            {
                FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Write, path, fullPath);
                state.EnsureState();
            }

            bool r = Win32Native.DeleteFile(fullPath);

            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, fullPath);
                }
            }
        }
示例#2
0
        // Deletes a file. The file specified by the designated path is deleted.
        // If the file does not exist, Delete succeeds without throwing
        // an exception.
        //
        // On NT, Delete will fail for a file that is open for normal I/O
        // or a file that is memory mapped.  On Win95, the file will be
        // deleted irregardless of whether the file is being used.
        //
        // Your application must have Delete permission to the target file.
        //
        /// <include file='doc\File.uex' path='docs/doc[@for="File.Delete"]/*' />
        public static void Delete(String path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            String fullPath = Path.GetFullPathInternal(path);

            // For security check, path should be resolved to an absolute path.
            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { fullPath }, false, false).Demand();

            if (Directory.InternalExists(fullPath)) // Win9x hack to behave same as Winnt. Win9x fails silently for directories
            {
                throw new UnauthorizedAccessException(String.Format(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), path));
            }

            bool r = Win32Native.DeleteFile(fullPath);

            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, path);
                }
            }
        }
        internal static void Delete(String path)
        {
            Contract.Requires(path != null);

            String fullPath = LongPath.NormalizePath(path);

            // For security check, path should be resolved to an absolute path.
            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { fullPath }, false, false).Demand();

            String tempPath = Path.AddLongPathPrefix(fullPath);
            bool   r        = Win32Native.DeleteFile(tempPath);

            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, fullPath);
                }
            }
        }
示例#4
0
文件: fileinfo.cs 项目: ydunk/masters
        // Deletes a file. The file specified by the designated path is deleted.
        // If the file does not exist, Delete succeeds without throwing
        // an exception.
        //
        // On NT, Delete will fail for a file that is open for normal I/O
        // or a file that is memory mapped.  On Win95, the file will be
        // deleted irregardless of whether the file is being used.
        //
        // Your application must have Delete permission to the target file.
        //
        /// <include file='doc\FileInfo.uex' path='docs/doc[@for="FileInfo.Delete"]/*' />
        public override void Delete()
        {
            // For security check, path should be resolved to an absolute path.
            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { FullPath }, false, false).Demand();

            if (System.IO.Directory.InternalExists(FullPath)) // Win9x hack to behave same as Winnt. Win9x fails silently for directories
            {
                throw new UnauthorizedAccessException(String.Format(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), OriginalPath));
            }

            bool r = Win32Native.DeleteFile(FullPath);

            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, OriginalPath);
                }
            }
        }
示例#5
0
        // Deletes a file. The file specified by the designated path is deleted.
        // If the file does not exist, Delete succeeds without throwing
        // an exception.
        //
        // On NT, Delete will fail for a file that is open for normal I/O
        // or a file that is memory mapped.
        public override void Delete()
        {
            bool r = Win32Native.DeleteFile(FullPath);

            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, DisplayPath);
                }
            }
        }
示例#6
0
        internal static void InternalDelete(String path)
        {
            String fullPath = Path.GetFullPath(path);
            bool   r        = Win32Native.DeleteFile(fullPath);

            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, fullPath);
                }
            }
        }
示例#7
0
        public override void Delete()
        {
            FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Write, DisplayPath, FullPath);

            state.EnsureState();

            bool r = Win32Native.DeleteFile(FullPath);

            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, DisplayPath);
                }
            }
        }
示例#8
0
        public override void Delete()
        {
#if FEATURE_MONO_CAS
#if FEATURE_CORECLR
            FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Write, DisplayPath, FullPath);
            state.EnsureState();
#else
            // For security check, path should be resolved to an absolute path.
            FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, FullPath, false, false);
#endif
#endif

#if MONO
            MonoIOError error;

            if (MonoIO.ExistsDirectory(FullPath, out error))
            {
                __Error.WinIOError(Win32Native.ERROR_ACCESS_DENIED, DisplayPath);
            }

            if (!MonoIO.DeleteFile(FullPath, out error))
            {
                int hr = (int)error;
#else
            bool r = Win32Native.DeleteFile(FullPath);
            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
#endif
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, DisplayPath);
                }
            }
        }
示例#9
0
        public override void Delete()
        {
#if FEATURE_CORECLR
            FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Write, DisplayPath, FullPath);
            state.EnsureState();
#else
            // For security check, path should be resolved to an absolute path.
            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { FullPath }, false, false).Demand();
#endif

            bool r = Win32Native.DeleteFile(FullPath);
            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, DisplayPath);
                }
            }
        }
        private static void DeleteHelper(String fullPath, String userPath, bool recursive, bool throwOnTopLevelDirectoryNotFound)
        {
            bool      r;
            int       hr;
            Exception ex = null;

            // Do not recursively delete through reparse points.  Perhaps in a
            // future version we will add a new flag to control this behavior,
            // but for now we're much safer if we err on the conservative side.
            // This applies to symbolic links and mount points.
            // Note the logic to check whether fullPath is a reparse point is
            // in Delete(String, String, bool), and will set "recursive" to false.
            // Note that Win32's DeleteFile and RemoveDirectory will just delete
            // the reparse point itself.

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

                String searchPath = null;
                if (fullPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
                {
                    searchPath = fullPath + "*";
                }
                else
                {
                    searchPath = fullPath + Path.DirectorySeparatorChar + "*";
                }

                // Open a Find handle
                using (SafeFindHandle hnd = Win32Native.FindFirstFile(searchPath, data))
                {
                    if (hnd.IsInvalid)
                    {
                        hr = Marshal.GetLastWin32Error();
                        __Error.WinIOError(hr, userPath);
                    }

                    do
                    {
                        bool isDir = (0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY));
                        if (isDir)
                        {
                            // Skip ".", "..".
                            if (data.cFileName.Equals(".") || data.cFileName.Equals(".."))
                            {
                                continue;
                            }

                            // Recurse for all directories, unless they are
                            // reparse points.  Do not follow mount points nor
                            // symbolic links, but do delete the reparse point
                            // itself.
                            bool shouldRecurse = (0 == (data.dwFileAttributes & (int)FileAttributes.ReparsePoint));
                            if (shouldRecurse)
                            {
                                String newFullPath = LongPath.InternalCombine(fullPath, data.cFileName);
                                String newUserPath = LongPath.InternalCombine(userPath, data.cFileName);
                                try
                                {
                                    DeleteHelper(newFullPath, newUserPath, recursive, false);
                                }
                                catch (Exception e)
                                {
                                    if (ex == null)
                                    {
                                        ex = e;
                                    }
                                }
                            }
                            else
                            {
                                // Check to see if this is a mount point, and
                                // unmount it.
                                if (data.dwReserved0 == Win32Native.IO_REPARSE_TAG_MOUNT_POINT)
                                {
                                    // Use full path plus a trailing '\'
                                    String mountPoint = LongPath.InternalCombine(fullPath, data.cFileName + Path.DirectorySeparatorChar);
                                    r = Win32Native.DeleteVolumeMountPoint(mountPoint);
                                    if (!r)
                                    {
                                        hr = Marshal.GetLastWin32Error();
                                        if (hr != Win32Native.ERROR_PATH_NOT_FOUND)
                                        {
                                            try
                                            {
                                                __Error.WinIOError(hr, data.cFileName);
                                            }
                                            catch (Exception e)
                                            {
                                                if (ex == null)
                                                {
                                                    ex = e;
                                                }
                                            }
                                        }
                                    }
                                }

                                // RemoveDirectory on a symbolic link will
                                // remove the link itself.
                                String reparsePoint = LongPath.InternalCombine(fullPath, data.cFileName);
                                r = Win32Native.RemoveDirectory(reparsePoint);
                                if (!r)
                                {
                                    hr = Marshal.GetLastWin32Error();
                                    if (hr != Win32Native.ERROR_PATH_NOT_FOUND)
                                    {
                                        try
                                        {
                                            __Error.WinIOError(hr, data.cFileName);
                                        }
                                        catch (Exception e)
                                        {
                                            if (ex == null)
                                            {
                                                ex = e;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            String fileName = LongPath.InternalCombine(fullPath, data.cFileName);
                            r = Win32Native.DeleteFile(fileName);
                            if (!r)
                            {
                                hr = Marshal.GetLastWin32Error();
                                if (hr != Win32Native.ERROR_FILE_NOT_FOUND)
                                {
                                    try
                                    {
                                        __Error.WinIOError(hr, data.cFileName);
                                    }
                                    catch (Exception e)
                                    {
                                        if (ex == null)
                                        {
                                            ex = e;
                                        }
                                    }
                                }
                            }
                        }
                    } while (Win32Native.FindNextFile(hnd, data));
                    // Make sure we quit with a sensible error.
                    hr = Marshal.GetLastWin32Error();
                }

                if (ex != null)
                {
                    throw ex;
                }
                if (hr != 0 && hr != Win32Native.ERROR_NO_MORE_FILES)
                {
                    __Error.WinIOError(hr, userPath);
                }
            }

            r = Win32Native.RemoveDirectory(fullPath);

            if (!r)
            {
                hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // A dubious error code.
                {
                    hr = Win32Native.ERROR_PATH_NOT_FOUND;
                }
                // This check was originally put in for Win9x (unfortunately without special casing it to be for Win9x only). We can't change the NT codepath now for backcomp reasons.
                if (hr == Win32Native.ERROR_ACCESS_DENIED)
                {
                    throw new IOException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", userPath));
                }

                // don't throw the DirectoryNotFoundException since this is a subdir and there could be a ----
                // between two Directory.Delete callers
                if (hr == Win32Native.ERROR_PATH_NOT_FOUND && !throwOnTopLevelDirectoryNotFound)
                {
                    return;
                }

                __Error.WinIOError(hr, userPath);
            }
        }