Exemplo n.º 1
0
        internal static bool InternalExists(String path)
        {
            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(path, ref data, false, true);

            return((dataInitialised == 0) && (data.fileAttributes != -1) &&
                   ((data.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0));
        }
Exemplo n.º 2
0
        public static FileAttributes GetAttributes(String path)
        {
            String fullPath = Path.GetFullPath(path);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, true);

            if (dataInitialised != 0)
            {
                __Error.WinIOError(dataInitialised, fullPath);
            }

            return((FileAttributes)data.fileAttributes);
        }
Exemplo n.º 3
0
        public static FileAttributes GetAttributes(String path)
        {
            String fullPath = Path.GetFullPathInternal(path);

            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false).Demand();

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, true);

            if (dataInitialised != 0)
            {
                __Error.WinIOError(dataInitialised, fullPath);
            }

            return((FileAttributes)data.fileAttributes);
        }
Exemplo n.º 4
0
        private static DateTime InternalGetLastWriteTimeUtc(String path)
        {
            String fullPath = Path.GetFullPath(path);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);

            if (dataInitialised != 0)
            {
                __Error.WinIOError(dataInitialised, fullPath);
            }

            long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow);

            return(DateTime.FromFileTimeUtc(dt));
        }
Exemplo n.º 5
0
        public static DateTime GetLastWriteTimeUtc(String path)
        {
            String fullPath = Path.GetFullPathInternal(path);

            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false).Demand();

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);

            if (dataInitialised != 0)
            {
                __Error.WinIOError(dataInitialised, fullPath);
            }

            long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow);

            return(DateTime.FromFileTimeUtc(dt));
        }
Exemplo n.º 6
0
        internal static DateTimeOffset GetLastWriteTime(String path)
        {
            Contract.Requires(path != null);

            String fullPath = LongPath.NormalizePath(path);

            FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPath, false, false);

            String tempPath = Path.AddLongPathPrefix(fullPath);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = File.FillAttributeInfo(tempPath, ref data, false, false);

            if (dataInitialised != 0)
            {
                __Error.WinIOError(dataInitialised, fullPath);
            }

            DateTime dtLocal = DateTime.FromFileTimeUtc(data.ftLastWriteTime.ToTicks()).ToLocalTime();

            return(new DateTimeOffset(dtLocal).ToLocalTime());
        }
Exemplo n.º 7
0
        private static DateTime InternalGetLastWriteTimeUtc(String path, bool checkHost)
        {
            String fullPath = Path.GetFullPath(path);

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

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);

            if (dataInitialised != 0)
            {
                __Error.WinIOError(dataInitialised, fullPath);
            }

            long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow);

            return(DateTime.FromFileTimeUtc(dt));
        }
Exemplo n.º 8
0
        /// <include file='doc\File.uex' path='docs/doc[@for="File.GetLastWriteTimeUtc"]/*' />
        public static DateTime GetLastWriteTimeUtc(String path)
        {
            String fullPath = Path.GetFullPathInternal(path);

            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false).Demand();

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false);

            if (dataInitialised != 0)
            {
                __Error.WinIOError(dataInitialised, path);
            }

            if (data.fileAttributes == -1)
            {
                throw new IOException(String.Format(Environment.GetResourceString("IO.PathNotFound_Path"), path));
            }

            long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow);

            return(DateTime.FromFileTimeUtc(dt));
        }
Exemplo n.º 9
0
        internal static DateTimeOffset GetLastAccessTime(String path)
        {
            Contract.Requires(path != null);

            String fullPath = LongPath.NormalizePath(path);

            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false).Demand();

            String tempPath = Path.AddLongPathPrefix(fullPath);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = File.FillAttributeInfo(tempPath, ref data, false, false);

            if (dataInitialised != 0)
            {
                __Error.WinIOError(dataInitialised, fullPath);
            }

            long     dt      = ((long)(data.ftLastAccessTimeHigh) << 32) | ((long)data.ftLastAccessTimeLow);
            DateTime dtLocal = DateTime.FromFileTimeUtc(dt).ToLocalTime();

            return(new DateTimeOffset(dtLocal).ToLocalTime());
        }
Exemplo n.º 10
0
        private static void InternalDelete(String fullPath, String userPath, bool recursive)
        {
            String demandPath;

            // If not recursive, do permission check only on this directory
            // else check for the whole directory structure rooted below
            demandPath = GetDemandDir(fullPath, !recursive);

            // Make sure we have write permission to this directory
            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { demandPath }, false, false).Demand();

            String longPath = Path.AddLongPathPrefix(fullPath);

            // 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.
            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = File.FillAttributeInfo(longPath, ref data, false, true);

            if (dataInitialised != 0)
            {
                // Ensure we throw a DirectoryNotFoundException.
                if (dataInitialised == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    dataInitialised = Win32Native.ERROR_PATH_NOT_FOUND;
                }
                __Error.WinIOError(dataInitialised, fullPath);
            }

            if (((FileAttributes)data.fileAttributes & FileAttributes.ReparsePoint) != 0)
            {
                recursive = false;
            }

            DeleteHelper(longPath, userPath, recursive, true);
        }
Exemplo n.º 11
0
        internal static long GetLength(String path)
        {
            Contract.Requires(path != null);

            String fullPath = LongPath.NormalizePath(path);

            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false).Demand();

            String tempPath = Path.AddLongPathPrefix(fullPath);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = File.FillAttributeInfo(tempPath, ref data, false, true); // return error

            if (dataInitialised != 0)
            {
                __Error.WinIOError(dataInitialised, path); // from FileInfo.
            }
            if ((data.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) != 0)
            {
                __Error.WinIOError(Win32Native.ERROR_FILE_NOT_FOUND, path);
            }

            return(((long)data.fileSizeHigh) << 32 | ((long)data.fileSizeLow & 0xFFFFFFFFL));
        }
Exemplo n.º 12
0
        public static FileAttributes GetAttributes(String path) 
        {
            String fullPath = Path.GetFullPathInternal(path);
            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false).Demand();

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, true);
            if (dataInitialised != 0)
                __Error.WinIOError(dataInitialised, fullPath);

            return (FileAttributes) data.fileAttributes;
        }
Exemplo n.º 13
0
        public static DateTime GetLastWriteTimeUtc(String path)
        {
            String fullPath = Path.GetFullPathInternal(path);
            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false ).Demand();

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);
            if (dataInitialised != 0)
                __Error.WinIOError(dataInitialised, fullPath);

            long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow);
            return DateTime.FromFileTimeUtc(dt);
        }
Exemplo n.º 14
0
        internal static bool InternalExists(String path) {
            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(path, ref data, false, true);

            return (dataInitialised == 0) && (data.fileAttributes != -1) 
                    && ((data.fileAttributes  & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0);
        }
Exemplo n.º 15
0
        public static FileAttributes GetAttributes(String path) 
        {
            String fullPath = Path.GetFullPath(path);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, true);
            if (dataInitialised != 0)
                __Error.WinIOError(dataInitialised, fullPath);

            return (FileAttributes) data.fileAttributes;
        }
Exemplo n.º 16
0
        // Returns 0 on success, otherwise a Win32 error code.  Note that
        // classes should use -1 as the uninitialized state for dataInitialized.
        internal static int FillAttributeInfo(String path, ref Win32Native.WIN32_FILE_ATTRIBUTE_DATA data, bool tryagain, bool returnErrorOnNotFound)
        {
            int dataInitialised = 0;

            if (tryagain) // someone has a handle to the file open, or other error
            {
                Win32Native.WIN32_FIND_DATA findData;
                findData = new Win32Native.WIN32_FIND_DATA();

                // Remove trialing slash since this can cause grief to FindFirstFile. You will get an invalid argument error
                String tempPath = path.TrimEnd(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });

#if !PLATFORM_UNIX
                // For floppy drives, normally the OS will pop up a dialog saying
                // there is no disk in drive A:, please insert one.  We don't want that.
                // SetThreadErrorMode will let us disable this, but we should set the error
                // mode back, since this may have wide-ranging effects.
                uint oldMode;
                bool errorModeSuccess = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
                try
                {
#endif
                bool error            = false;
                SafeFindHandle handle = Win32Native.FindFirstFile(tempPath, findData);
                try
                {
                    if (handle.IsInvalid)
                    {
                        error           = true;
                        dataInitialised = Marshal.GetLastWin32Error();

                        if (dataInitialised == Win32Native.ERROR_FILE_NOT_FOUND ||
                            dataInitialised == Win32Native.ERROR_PATH_NOT_FOUND ||
                            dataInitialised == Win32Native.ERROR_NOT_READY)      // floppy device not ready
                        {
                            if (!returnErrorOnNotFound)
                            {
                                // Return default value for backward compatibility
                                dataInitialised     = 0;
                                data.fileAttributes = -1;
                            }
                        }
                        return(dataInitialised);
                    }
                }
                finally
                {
                    // Close the Win32 handle
                    try
                    {
                        handle.Close();
                    }
                    catch
                    {
                        // if we're already returning an error, don't throw another one.
                        if (!error)
                        {
                            Debug.Assert(false, "File::FillAttributeInfo - FindClose failed!");
                            throw Win32Marshal.GetExceptionForLastWin32Error();
                        }
                    }
                }
#if !PLATFORM_UNIX
            }
            finally
            {
                if (errorModeSuccess)
                {
                    Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
                }
            }
#endif

                // Copy the information to data
                data.PopulateFrom(findData);
            }
            else
            {
                bool success = false;

#if !PLATFORM_UNIX
                // For floppy drives, normally the OS will pop up a dialog saying
                // there is no disk in drive A:, please insert one.  We don't want that.
                // SetThreadErrorMode will let us disable this, but we should set the error
                // mode back, since this may have wide-ranging effects.
                uint oldMode;
                bool errorModeSuccess = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode);
                try
                {
#endif
                success = Win32Native.GetFileAttributesEx(path, GetFileExInfoStandard, ref data);
#if !PLATFORM_UNIX
            }
            finally
            {
                if (errorModeSuccess)
                {
                    Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode);
                }
            }
#endif

                if (!success)
                {
                    dataInitialised = Marshal.GetLastWin32Error();
                    if (dataInitialised != Win32Native.ERROR_FILE_NOT_FOUND &&
                        dataInitialised != Win32Native.ERROR_PATH_NOT_FOUND &&
                        dataInitialised != Win32Native.ERROR_NOT_READY)  // floppy device not ready
                    {
                        // In case someone latched onto the file. Take the perf hit only for failure
                        return(FillAttributeInfo(path, ref data, true, returnErrorOnNotFound));
                    }
                    else
                    {
                        if (!returnErrorOnNotFound)
                        {
                            // Return default value for backward compbatibility
                            dataInitialised     = 0;
                            data.fileAttributes = -1;
                        }
                    }
                }
            }

            return(dataInitialised);
        }
Exemplo n.º 17
0
        internal static long GetLength(String path)
        { 
            Contract.Requires(path != null);
 
            String fullPath = LongPath.NormalizePath(path); 
            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false ).Demand();
 
            String tempPath = Path.AddLongPathPrefix(fullPath);
            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = File.FillAttributeInfo(tempPath, ref data, false, true); // return error
            if (dataInitialised != 0) 
                __Error.WinIOError(dataInitialised, path); // from FileInfo.
 
            if ((data.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) != 0) 
                __Error.WinIOError(Win32Native.ERROR_FILE_NOT_FOUND, path);
 
            return ((long)data.fileSizeHigh) << 32 | ((long)data.fileSizeLow & 0xFFFFFFFFL);

        }
Exemplo n.º 18
0
        private static DateTime InternalGetCreationTimeUtc(String path, bool checkHost)
        {
            String fullPath = Path.GetFullPathInternal(path);
#if FEATURE_CORECLR && !FEATURE_LEGACYNETCFIOSECURITY
            if (checkHost) 
            {
                FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, path, fullPath);
                state.EnsureState();
            }
#elif !FEATURE_CORECLR      
            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false ).Demand();
#endif

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);
            if (dataInitialised != 0)
                __Error.WinIOError(dataInitialised, fullPath);

            long dt = ((long)(data.ftCreationTimeHigh) << 32) | ((long)data.ftCreationTimeLow);
            return DateTime.FromFileTimeUtc(dt);
        }
Exemplo n.º 19
0
        /// <include file='doc\File.uex' path='docs/doc[@for="File.GetLastWriteTime"]/*' />
        public static DateTime GetLastWriteTime(String path)
        {
            String fullPath = Path.GetFullPathInternal(path);
            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false ).Demand();

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath,ref data,false);
            if (dataInitialised != 0)
                __Error.WinIOError(dataInitialised, path);

            if (data.fileAttributes == -1)
                throw new IOException(String.Format(Environment.GetResourceString("IO.PathNotFound_Path"), path));

            long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow);
            return DateTime.FromFileTime(dt);
        }
Exemplo n.º 20
0
        internal static int FillAttributeInfo(String path, ref Win32Native.WIN32_FILE_ATTRIBUTE_DATA data, bool tryagain)
        {
            int dataInitialised = 0;

            if (Environment.OSInfo == Environment.OSName.Win95 || tryagain)
            // We are running under Windows 95 and we don't have GetFileAttributesEx API or someone has a handle to the file open
            {
                Win32Native.WIN32_FIND_DATA win95data; // We do this only on Win95 machines
                win95data = new Win32Native.WIN32_FIND_DATA();

                // Remove trialing slash since this can cause grief to FindFirstFile. You will get an invalid argument error
                String tempPath = path.TrimEnd(new char [] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });

                // For floppy drives, normally the OS will pop up a dialog saying
                // there is no disk in drive A:, please insert one.  We don't want that.
                // SetErrorMode will let us disable this, but we should set the error
                // mode back, since this may have wide-ranging effects.
                int    oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS);
                IntPtr handle  = Win32Native.FindFirstFile(tempPath, win95data);
                Win32Native.SetErrorMode(oldMode);
                if (handle == Win32Native.INVALID_HANDLE_VALUE)
                {
                    dataInitialised = Marshal.GetLastWin32Error();
                    if (dataInitialised == Win32Native.ERROR_FILE_NOT_FOUND ||
                        dataInitialised == Win32Native.ERROR_PATH_NOT_FOUND ||
                        dataInitialised == Win32Native.ERROR_NOT_READY)  // floppy device not ready
                    {
                        data.fileAttributes = -1;
                        dataInitialised     = 0;
                    }

                    return(dataInitialised);
                }
                // Close the Win32 handle
                bool r = Win32Native.FindClose(handle);
                if (!r)
                {
                    BCLDebug.Assert(false, "File::FillAttributeInfo - FindClose failed!");
                    __Error.WinIOError();
                }

                // Copy the information to data
                data.fileAttributes       = win95data.dwFileAttributes;
                data.ftCreationTimeLow    = (uint)win95data.ftCreationTime_dwLowDateTime;
                data.ftCreationTimeHigh   = (uint)win95data.ftCreationTime_dwHighDateTime;
                data.ftLastAccessTimeLow  = (uint)win95data.ftLastAccessTime_dwLowDateTime;
                data.ftLastAccessTimeHigh = (uint)win95data.ftLastAccessTime_dwHighDateTime;
                data.ftLastWriteTimeLow   = (uint)win95data.ftLastWriteTime_dwLowDateTime;
                data.ftLastWriteTimeHigh  = (uint)win95data.ftLastWriteTime_dwHighDateTime;
                data.fileSizeHigh         = win95data.nFileSizeHigh;
                data.fileSizeLow          = win95data.nFileSizeLow;
            }
            else
            {
                // For floppy drives, normally the OS will pop up a dialog saying
                // there is no disk in drive A:, please insert one.  We don't want that.
                // SetErrorMode will let us disable this, but we should set the error
                // mode back, since this may have wide-ranging effects.
                int  oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS);
                bool success = Win32Native.GetFileAttributesEx(path, GetFileExInfoStandard, ref data);

                Win32Native.SetErrorMode(oldMode);
                if (!success)
                {
                    dataInitialised = Marshal.GetLastWin32Error();
                    if (dataInitialised == Win32Native.ERROR_FILE_NOT_FOUND ||
                        dataInitialised == Win32Native.ERROR_PATH_NOT_FOUND ||
                        dataInitialised == Win32Native.ERROR_NOT_READY)  // floppy device not ready
                    {
                        data.fileAttributes = -1;
                        dataInitialised     = 0;
                    }
                    else
                    {
                        // In case someone latched onto the file. Take the perf hit only for failure
                        return(FillAttributeInfo(path, ref data, true));
                    }
                }
            }

            return(dataInitialised);
        }
Exemplo n.º 21
0
        internal static DateTimeOffset GetLastWriteTime(String path)
        { 
            Contract.Requires(path != null);
 
            String fullPath = LongPath.NormalizePath(path); 
            new FileIOPermission(FileIOPermissionAccess.Read, new String[] { fullPath }, false, false ).Demand();
 
            String tempPath = Path.AddLongPathPrefix(fullPath);
            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = File.FillAttributeInfo(tempPath, ref data, false, false);
            if (dataInitialised != 0) 
                __Error.WinIOError(dataInitialised, fullPath);
 
            long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow); 
            DateTime dtLocal = DateTime.FromFileTimeUtc(dt).ToLocalTime();
            return new DateTimeOffset(dtLocal).ToLocalTime(); 
        }
Exemplo n.º 22
0
        internal static int FillAttributeInfo(String path, ref Win32Native.WIN32_FILE_ATTRIBUTE_DATA data, bool tryagain, bool returnErrorOnNotFound)
        {
            int dataInitialised = 0;

            if (tryagain)                              // someone has a handle to the file open, or other error
            {
                Win32Native.WIN32_FIND_DATA win95data; // We do this only on Win95 machines
                win95data = new Win32Native.WIN32_FIND_DATA();

                // Remove trialing slash since this can cause grief to FindFirstFile. You will get an invalid argument error
                String tempPath = path.TrimEnd(new char [] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });

                // For floppy drives, normally the OS will pop up a dialog saying
                // there is no disk in drive A:, please insert one.  We don't want that.
                // SetErrorMode will let us disable this, but we should set the error
                // mode back, since this may have wide-ranging effects.
                int oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS);
                try {
                    bool           error  = false;
                    SafeFindHandle handle = Win32Native.FindFirstFile(tempPath, win95data);
                    try {
                        if (handle.IsInvalid)
                        {
                            error           = true;
                            dataInitialised = Marshal.GetLastWin32Error();

                            if (dataInitialised == Win32Native.ERROR_FILE_NOT_FOUND ||
                                dataInitialised == Win32Native.ERROR_PATH_NOT_FOUND ||
                                dataInitialised == Win32Native.ERROR_NOT_READY)  // floppy device not ready
                            {
                                if (!returnErrorOnNotFound)
                                {
                                    // Return default value for backward compbatibility
                                    dataInitialised     = 0;
                                    data.fileAttributes = -1;
                                }
                            }
                            return(dataInitialised);
                        }
                    }
                    finally {
                        // Close the Win32 handle
                        try {
                            handle.Close();
                        }
                        catch {
                            // if we're already returning an error, don't throw another one.
                            if (!error)
                            {
                                BCLDebug.Assert(false, "File::FillAttributeInfo - FindClose failed!");
                                __Error.WinIOError();
                            }
                        }
                    }
                }
                finally {
                    Win32Native.SetErrorMode(oldMode);
                }

                // Copy the information to data
                data.fileAttributes       = win95data.dwFileAttributes;
                data.ftCreationTimeLow    = (uint)win95data.ftCreationTime_dwLowDateTime;
                data.ftCreationTimeHigh   = (uint)win95data.ftCreationTime_dwHighDateTime;
                data.ftLastAccessTimeLow  = (uint)win95data.ftLastAccessTime_dwLowDateTime;
                data.ftLastAccessTimeHigh = (uint)win95data.ftLastAccessTime_dwHighDateTime;
                data.ftLastWriteTimeLow   = (uint)win95data.ftLastWriteTime_dwLowDateTime;
                data.ftLastWriteTimeHigh  = (uint)win95data.ftLastWriteTime_dwHighDateTime;
                data.fileSizeHigh         = win95data.nFileSizeHigh;
                data.fileSizeLow          = win95data.nFileSizeLow;
            }
            else
            {
                // For floppy drives, normally the OS will pop up a dialog saying
                // there is no disk in drive A:, please insert one.  We don't want that.
                // SetErrorMode will let us disable this, but we should set the error
                // mode back, since this may have wide-ranging effects.
                bool success = false;
                int  oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS);
                try {
                    success = Win32Native.GetFileAttributesEx(path, GetFileExInfoStandard, ref data);
                }
                finally {
                    Win32Native.SetErrorMode(oldMode);
                }

                if (!success)
                {
                    dataInitialised = Marshal.GetLastWin32Error();
                    if (dataInitialised != Win32Native.ERROR_FILE_NOT_FOUND &&
                        dataInitialised != Win32Native.ERROR_PATH_NOT_FOUND &&
                        dataInitialised != Win32Native.ERROR_NOT_READY)  // floppy device not ready
                    {
                        // In case someone latched onto the file. Take the perf hit only for failure
                        return(FillAttributeInfo(path, ref data, true, returnErrorOnNotFound));
                    }
                    else
                    {
                        if (!returnErrorOnNotFound)
                        {
                            // Return default value for backward compbatibility
                            dataInitialised     = 0;
                            data.fileAttributes = -1;
                        }
                    }
                }
            }

            return(dataInitialised);
        }
Exemplo n.º 23
0
        private static void InternalDelete(String fullPath, String userPath, bool recursive) 
        {
            String demandPath;

            // If not recursive, do permission check only on this directory 
            // else check for the whole directory structure rooted below
            demandPath = GetDemandDir(fullPath, !recursive); 
 
            // Make sure we have write permission to this directory
            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { demandPath }, false, false).Demand(); 

            String longPath = Path.AddLongPathPrefix(fullPath);
            // 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. 
            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA(); 
            int dataInitialised = File.FillAttributeInfo(longPath, ref data, false, true);
            if (dataInitialised != 0) 
            {
                // Ensure we throw a DirectoryNotFoundException.
                if (dataInitialised == Win32Native.ERROR_FILE_NOT_FOUND)
                    dataInitialised = Win32Native.ERROR_PATH_NOT_FOUND; 
                __Error.WinIOError(dataInitialised, fullPath);
            } 
 
            if (((FileAttributes)data.fileAttributes & FileAttributes.ReparsePoint) != 0)
                recursive = false; 

            DeleteHelper(longPath, userPath, recursive);
        }
Exemplo n.º 24
0
        private static DateTime InternalGetLastWriteTimeUtc(String path, bool checkHost)
        {
            String fullPath = Path.GetFullPathInternal(path);
#if FEATURE_CORECLR
            if (checkHost)
            {
                FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, path, fullPath);
                state.EnsureState();
            }
#else
            FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPath, false, false);
#endif

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);
            if (dataInitialised != 0)
                __Error.WinIOError(dataInitialised, fullPath);

            long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow);
            return DateTime.FromFileTimeUtc(dt);
        }
Exemplo n.º 25
0
        public static FileAttributes GetAttributes(String path) 
        {
            String fullPath = Path.GetFullPathInternal(path);
#if FEATURE_CORECLR
            FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, path, fullPath);
            state.EnsureState();
#else
            FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPath, false, false);
#endif

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, true);
            if (dataInitialised != 0)
                __Error.WinIOError(dataInitialised, fullPath);

            return (FileAttributes) data.fileAttributes;
        }
Exemplo n.º 26
0
        private static DateTime InternalGetLastWriteTimeUtc(String path)
        {
            String fullPath = Path.GetFullPath(path);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);
            if (dataInitialised != 0)
                __Error.WinIOError(dataInitialised, fullPath);

            long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow);
            return DateTime.FromFileTimeUtc(dt);
        }