コード例 #1
0
        public static void Move(String sourceFileName, String destFileName)
        {
            if (sourceFileName == null || destFileName == null)
            {
                throw new ArgumentNullException((sourceFileName == null ? "sourceFileName" : "destFileName"), "ArgumentNull_FileName");
            }
            if (sourceFileName.Length == 0 || destFileName.Length == 0)
            {
                throw new ArgumentException("Argument_EmptyFileName", (sourceFileName.Length == 0 ? "sourceFileName" : "destFileName"));
            }

            String fullSourceFileName = NameFix.AddLongPathPrefix(sourceFileName);
            String fullDestFileName   = NameFix.AddLongPathPrefix(destFileName);

            if (!Exists(fullSourceFileName))
            {
                throw new Exception("ERROR_FILE_NOT_FOUND" + fullSourceFileName);
            }

            if (!Win32Native.MoveFile(fullSourceFileName, fullDestFileName))
            {
                int hr = Marshal.GetLastWin32Error();
                throw new Exception("ERROR_MOVING_FILE. Error Code (" + hr + ")");
            }
        }
コード例 #2
0
        public static bool Exists(string path)
        {
            string fixPath = NameFix.AddLongPathPrefix(path);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA wIn32FileAttributeData = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();

            bool b = Win32Native.GetFileAttributesEx(fixPath, 0, ref wIn32FileAttributeData);

            return(b && (wIn32FileAttributeData.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0);
        }
コード例 #3
0
        public static void Delete(String path)
        {
            string fixPath = NameFix.AddLongPathPrefix(path);

            if (!Win32Native.DeleteFile(fixPath))
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr != Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    throw new Exception("Error while deleting file :" + path);
                }
            }
        }
コード例 #4
0
        public static void CreateDirectory(String path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (path.Length == 0)
            {
                throw new ArgumentException("Argument_PathEmpty");
            }

            String fullPath = NameFix.AddLongPathPrefix(path);

            Win32Native.CreateDirectory(fullPath, IntPtr.Zero);
        }
コード例 #5
0
        public static void Copy(String sourceFileName, String destFileName, bool overwrite)
        {
            if (sourceFileName == null || destFileName == null)
            {
                throw new ArgumentNullException((sourceFileName == null ? "sourceFileName" : "destFileName"), "ArgumentNull_FileName");
            }
            if (sourceFileName.Length == 0 || destFileName.Length == 0)
            {
                throw new ArgumentException("Argument_EmptyFileName", (sourceFileName.Length == 0 ? "sourceFileName" : "destFileName"));
            }

            String fullSourceFileName = NameFix.AddLongPathPrefix(sourceFileName);
            String fullDestFileName   = NameFix.AddLongPathPrefix(destFileName);

            bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite);

            if (!r)
            {
                // Save Win32 error because subsequent checks will overwrite this HRESULT.
                int    errorCode = Marshal.GetLastWin32Error();
                String fileName  = destFileName;

                /*
                 * if (errorCode != Win32Native.ERROR_FILE_EXISTS)
                 * {
                 *  // For a number of error codes (sharing violation, path
                 *  // not found, etc) we don't know if the problem was with
                 *  // the source or dest file.  Try reading the source file.
                 *  using (SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullSourceFileName, FileStream.GENERIC_READ, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero))
                 *  {
                 *      if (handle.IsInvalid)
                 *          fileName = sourceFileName;
                 *  }
                 *
                 *  if (errorCode == Win32Native.ERROR_ACCESS_DENIED)
                 *  {
                 *      if (Directory.InternalExists(fullDestFileName))
                 *          throw new IOException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Arg_FileIsDirectory_Name"), destFileName), Win32Native.ERROR_ACCESS_DENIED, fullDestFileName);
                 *  }
                 * }
                 *
                 * __Error.WinIOError(errorCode, fileName);
                 *
                 */
            }
        }
コード例 #6
0
        public DirectoryInfo[] GetDirectories(string SearchPattern, bool includeHidden = true)
        {
            List <DirectoryInfo> dirs = new List <DirectoryInfo>();

            string dirName = NameFix.AddLongPathPrefix(FullName);

            Win32Native.WIN32_FIND_DATA findData = new Win32Native.WIN32_FIND_DATA();
            SafeFindHandle findHandle            = Win32Native.FindFirstFile(dirName + @"\" + SearchPattern, findData);

            if (!findHandle.IsInvalid)
            {
                do
                {
                    string currentFileName = findData.cFileName;

                    // if this is a directory, find its contents
                    if ((findData.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0)
                    {
                        continue;
                    }
                    if (currentFileName == "." || currentFileName == "..")
                    {
                        continue;
                    }
                    if (!includeHidden && (findData.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_HIDDEN) != 0)
                    {
                        continue;
                    }

                    DirectoryInfo di = new DirectoryInfo
                    {
                        Name           = currentFileName,
                        FullName       = Path.Combine(FullName, currentFileName),
                        LastWriteTime  = Convert.Time(findData.ftLastWriteTimeHigh, findData.ftLastWriteTimeLow),
                        fileAttributes = findData.dwFileAttributes
                    };
                    dirs.Add(di);
                }while (Win32Native.FindNextFile(findHandle, findData));
            }

            // close the find handle
            findHandle.Dispose();

            return(dirs.ToArray());
        }
コード例 #7
0
        public DirectoryInfo(string path)
        {
            FullName = path;
            Name     = Path.GetFileName(path);

            string fileName = NameFix.AddLongPathPrefix(path);

            Win32Native.WIN32_FILE_ATTRIBUTE_DATA wIn32FileAttributeData = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();

            bool b = Win32Native.GetFileAttributesEx(fileName, 0, ref wIn32FileAttributeData);

            if (!b || (wIn32FileAttributeData.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0)
            {
                return;
            }
            LastWriteTime  = Convert.Time(wIn32FileAttributeData.ftLastWriteTimeHigh, wIn32FileAttributeData.ftLastWriteTimeLow);
            fileAttributes = wIn32FileAttributeData.fileAttributes;
        }
コード例 #8
0
        // errorMessage = new Win32Exception(errorCode).Message;

        public static int OpenFileRead(string path, out System.IO.Stream stream)
        {
            string         filename = NameFix.AddLongPathPrefix(path);
            SafeFileHandle hFile    = Win32Native.CreateFile(filename,
                                                             GENERIC_READ,
                                                             System.IO.FileShare.Read,
                                                             IntPtr.Zero,
                                                             System.IO.FileMode.Open,
                                                             FILE_ATTRIBUTE_NORMAL,
                                                             IntPtr.Zero);

            if (hFile.IsInvalid)
            {
                stream = null;
                return(Marshal.GetLastWin32Error());
            }
            stream = new System.IO.FileStream(hFile, System.IO.FileAccess.Read);
            return(0);
        }
コード例 #9
0
        public static void Move(String sourceDirName, String destDirName)
        {
            if (sourceDirName == null)
            {
                throw new ArgumentNullException("sourceDirName");
            }
            if (sourceDirName.Length == 0)
            {
                throw new ArgumentException("Argument_EmptyFileName", "sourceDirName");
            }

            if (destDirName == null)
            {
                throw new ArgumentNullException("destDirName");
            }
            if (destDirName.Length == 0)
            {
                throw new ArgumentException("Argument_EmptyFileName", "destDirName");
            }

            String fullsourceDirName = NameFix.AddLongPathPrefix(sourceDirName);

            String fulldestDirName = NameFix.AddLongPathPrefix(destDirName);

            if (!Win32Native.MoveFile(fullsourceDirName, fulldestDirName))
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // Source dir not found
                {
                    throw new Exception("ERROR_PATH_NOT_FOUND " + fullsourceDirName);
                }
                if (hr == Win32Native.ERROR_ACCESS_DENIED) // WinNT throws IOException. This check is for Win9x. We can't change it for backcomp.
                {
                    throw new Exception("UnauthorizedAccess_IODenied_Path" + sourceDirName);
                }
            }
        }
コード例 #10
0
        public static bool SetAttributes(String path, FileAttributes fileAttributes)
        {
            String fullPath = NameFix.AddLongPathPrefix(path);

            return(Win32Native.SetFileAttributes(fullPath, (int)fileAttributes));
        }
コード例 #11
0
        public static void Delete(String path)
        {
            String fullPath = NameFix.AddLongPathPrefix(path);

            Win32Native.RemoveDirectory(fullPath);
        }