Example #1
0
 public static void Copy(string sourceFileName, string destFileName, bool bOverwrite = false)
 {
     if (!Win32FileSystem.CopyFileW(GetWin32LongPath(sourceFileName), GetWin32LongPath(destFileName), !bOverwrite))
     {
         ThrowWin32Exception();
     }
 }
Example #2
0
 public static void Move(string sourceFileName, string destFileName)
 {
     if (!Win32FileSystem.MoveFileW(GetWin32LongPath(sourceFileName), GetWin32LongPath(destFileName)))
     {
         ThrowWin32Exception();
     }
 }
Example #3
0
        public System.Drawing.Icon ExtractIconFromPath(string path)
        {
            Win32FileSystem.SHFILEINFO shinfo = new Win32FileSystem.SHFILEINFO();
            Win32FileSystem.SHGetFileInfo(path, 0, ref shinfo,
                                          (uint)System.Runtime.InteropServices.Marshal.SizeOf(shinfo),
                                          SHGFI_ICON | SHGFI_LARGEICON);

            return(System.Drawing.Icon.FromHandle(shinfo.hIcon));
        }
Example #4
0
        public static DateTime GetLastWriteTime(string path)
        {
            long cTime = 0;
            long aTime = 0;
            long wTime = 0;

            using (var handle = GetFileHandleWithWrite(path))
            {
                Win32FileSystem.GetFileTime(handle, ref cTime, ref aTime, ref wTime);

                return(DateTime.FromFileTimeUtc(wTime));
            }
        }
Example #5
0
        private static SafeFileHandle CreateFileForWrite(string filename)
        {
            if (IsLongPath(filename))
            {
                filename = GetWin32LongPath(filename);
            }
            SafeFileHandle hfile = Win32FileSystem.CreateFile(filename, (int)Win32FileSystem.FILE_GENERIC_WRITE, Win32FileSystem.FILE_SHARE_NONE, IntPtr.Zero, Win32FileSystem.CREATE_ALWAYS, 0, IntPtr.Zero);

            if (hfile.IsInvalid)
            {
                ThrowWin32Exception();
            }
            return(hfile);
        }
Example #6
0
        internal static SafeFileHandle GetFileHandle(string filename)
        {
            if (IsLongPath(filename))
            {
                filename = GetWin32LongPath(filename);
            }
            SafeFileHandle hfile = Win32FileSystem.CreateFile(filename, (int)Win32FileSystem.FILE_GENERIC_READ, Win32FileSystem.FILE_SHARE_READ, IntPtr.Zero, Win32FileSystem.OPEN_EXISTING, 0, IntPtr.Zero);

            if (hfile.IsInvalid)
            {
                ThrowWin32Exception();
            }
            return(hfile);
        }
Example #7
0
        internal static SafeFileHandle GetFileHandleWithWrite(string filename)
        {
            if (IsLongPath(filename))
            {
                filename = GetWin32LongPath(filename);
            }
//            SafeFileHandle hfile = Win32FileSystem.CreateFile(filename, (int)(Win32FileSystem.FILE_GENERIC_READ | Win32FileSystem.FILE_GENERIC_WRITE | Win32FileSystem.FILE_WRITE_ATTRIBUTES), Win32FileSystem.FILE_SHARE_NONE, IntPtr.Zero, Win32FileSystem.OPEN_EXISTING, 0, IntPtr.Zero);
            SafeFileHandle hfile = Win32FileSystem.CreateFile(filename, (int)(Win32FileSystem.FILE_GENERIC_READ), Win32FileSystem.FILE_SHARE_READ, IntPtr.Zero, Win32FileSystem.OPEN_EXISTING, 0, IntPtr.Zero);

            if (hfile.IsInvalid)
            {
                ThrowWin32Exception();
            }
            return(hfile);
        }
Example #8
0
 public static void Delete(string path)
 {
     if (!IsLongPath(path))
     {
         System.IO.File.Delete(path);
     }
     else
     {
         bool ok = Win32FileSystem.DeleteFileW(GetWin32LongPath(path));
         if (!ok)
         {
             ThrowWin32Exception();
         }
     }
 }
        //public const int MAX_PATH = 245;//260;

        public static void CreateDirectory(string path)
        {
            var paths = GetAllPathsFromPath(GetWin32LongPath(path));

            foreach (var item in paths)
            {
                if (!Exists(item))
                {
                    var ok = Win32FileSystem.CreateDirectory(item, null);// IntPtr.Zero);
                    if (!ok)
                    {
                        ThrowWin32Exception();
                    }
                }
            }
        }
Example #10
0
        public static void SetAttributes(string path, System.IO.FileAttributes attributes)
        {
            var longFilename = GetWin32LongPath(path);

            Win32FileSystem.SetFileAttributesW(longFilename, (int)attributes);

            //if (path.Length < MAX_PATH)
            //{
            //    System.IO.File.SetAttributes(path, attributes);
            //}
            //else
            //{
            //    var longFilename = GetWin32LongPath(path);
            //    Win32FileSystem.SetFileAttributesW(longFilename, (int)attributes);
            //}
        }
Example #11
0
        public static void SetCreationTime(string path, DateTime creationTime)
        {
            long cTime = 0;
            long aTime = 0;
            long wTime = 0;

            using (var handle = GetFileHandleWithWrite(path))
            {
                Win32FileSystem.GetFileTime(handle, ref cTime, ref aTime, ref wTime);
                var fileTime = creationTime.ToFileTimeUtc();
                if (!Win32FileSystem.SetFileTime(handle, ref fileTime, ref aTime, ref wTime))
                {
                    ThrowWin32Exception();
                }
            }
        }
Example #12
0
        public static bool Exists(string path)
        {
            var attr = Win32FileSystem.GetFileAttributesW(GetWin32LongPath(path));

            return(attr != Win32FileSystem.INVALID_FILE_ATTRIBUTES && ((attr & Win32FileSystem.FILE_ATTRIBUTE_ARCHIVE) == Win32FileSystem.FILE_ATTRIBUTE_ARCHIVE));

            //if (path.Length < MAX_PATH)
            //{
            //    return System.IO.File.Exists(path);
            //}
            //else
            //{
            //    var attr = Win32FileSystem.GetFileAttributesW(GetWin32LongPath(path));

            //    return (attr != Win32FileSystem.INVALID_FILE_ATTRIBUTES && ((attr & Win32FileSystem.FILE_ATTRIBUTE_ARCHIVE) == Win32FileSystem.FILE_ATTRIBUTE_ARCHIVE));
            //}
        }
Example #13
0
 private static void DeleteDirectoriesRecrusive(string[] directories)
 {
     foreach (string directory in directories)
     {
         var files = Win32LongPathDirectory.GetFiles(GetWin32LongPath(directory), null, System.IO.SearchOption.TopDirectoryOnly);
         foreach (string file in files)
         {
             Win32LongPathFile.Delete(GetWin32LongPath(file));
         }
         directories = Win32LongPathDirectory.GetDirectories(directory, null, System.IO.SearchOption.TopDirectoryOnly);
         DeleteDirectoriesRecrusive(directories);
         bool ok = Win32FileSystem.RemoveDirectory(GetWin32LongPath(directory));
         if (!ok)
         {
             ThrowWin32Exception();
         }
     }
 }
Example #14
0
        public static List <string> GetFiles(string path, string searchPattern = "*", System.IO.SearchOption searchOption = System.IO.SearchOption.TopDirectoryOnly)
        {
            searchPattern = searchPattern ?? "*";

            var files = new List <string>();
            var dirs  = new List <string> {
                GetWin32LongPath(path)
            };

            if (searchOption == System.IO.SearchOption.AllDirectories)
            {
                //Add all the subpaths
                dirs.AddRange(Win32LongPathDirectory.GetDirectories(path, null, System.IO.SearchOption.AllDirectories));
            }

            foreach (var dir in dirs)
            {
                Win32FileSystem.WIN32_FIND_DATA findData;
                IntPtr findHandle = Win32FileSystem.FindFirstFile(System.IO.Path.Combine(GetWin32LongPath(dir), searchPattern), out findData);

                try
                {
                    if (findHandle != new IntPtr(-1))
                    {
                        do
                        {
                            if ((findData.dwFileAttributes & System.IO.FileAttributes.Directory) == 0)
                            {
                                string filename = System.IO.Path.Combine(dir, findData.cFileName);
                                files.Add(GetCleanPath(filename));
                            }
                        } while (Win32FileSystem.FindNextFile(findHandle, out findData));
                        Win32FileSystem.FindClose(findHandle);
                    }
                }
                catch (Exception)
                {
                    Win32FileSystem.FindClose(findHandle);
                    throw;
                }
            }

            return(files);
        }
Example #15
0
        public static System.IO.FileStream GetFileStream(string filename, System.IO.FileAccess access = System.IO.FileAccess.Read)
        {
            var            longFilename = GetWin32LongPath(filename);
            SafeFileHandle hfile;

            if (access == System.IO.FileAccess.Write)
            {
                hfile = Win32FileSystem.CreateFile(longFilename, (int)(Win32FileSystem.FILE_GENERIC_READ | Win32FileSystem.FILE_GENERIC_WRITE | Win32FileSystem.FILE_WRITE_ATTRIBUTES), Win32FileSystem.FILE_SHARE_NONE, IntPtr.Zero, Win32FileSystem.OPEN_EXISTING, 0, IntPtr.Zero);
            }
            else
            {
                hfile = Win32FileSystem.CreateFile(longFilename, (int)Win32FileSystem.FILE_GENERIC_READ, Win32FileSystem.FILE_SHARE_READ, IntPtr.Zero, Win32FileSystem.OPEN_EXISTING, 0, IntPtr.Zero);
            }

            if (hfile.IsInvalid)
            {
                ThrowWin32Exception();
            }

            return(new System.IO.FileStream(hfile, access));
        }
Example #16
0
        public static void Delete(string path, bool recursive = false)
        {
            if (!recursive)
            {
                //handle read only
                //Win32LongPathFile.SetAttributes(path, System.IO.FileAttributes.Normal);
                if (!Win32FileSystem.RemoveDirectory(GetWin32LongPath(path)))
                {
                    ThrowWin32Exception();
                }
            }
            else
            {
                var longPath = new string[] { GetWin32LongPath(path) };
                DeleteDirectoriesRecrusive(longPath);
            }

            //if (path.Length < MAX_PATH && !recursive)
            //{
            //    //Handle read only
            //    //            SetFileAttribute(directory, FileAttributes.Normal);
            //    System.IO.Directory.Delete(path, recursive);
            //}
            //else
            //{
            //    if (!recursive)
            //    {
            //        //handle read only
            //        //            SetFileAttribute(directory, FileAttributes.Normal);
            //        bool ok = Win32FileSystem.RemoveDirectory(GetWin32LongPath(path));
            //        if (!ok) ThrowWin32Exception();
            //    }
            //    else
            //    {
            //        DeleteDirectories(new string[] { GetWin32LongPath(path) });
            //    }
            //}
        }
Example #17
0
        private static void InternalGetDirectories(string path, string searchPattern, System.IO.SearchOption searchOption, ref List <string> dirs)
        {
            Win32FileSystem.WIN32_FIND_DATA findData;
            IntPtr findHandle = Win32FileSystem.FindFirstFile(System.IO.Path.Combine(GetWin32LongPath(path), searchPattern), out findData);

            try
            {
                if (findHandle != new IntPtr(-1))
                {
                    do
                    {
                        if ((findData.dwFileAttributes & System.IO.FileAttributes.Directory) != 0)
                        {
                            if (findData.cFileName != "." && findData.cFileName != "..")
                            {
                                string subdirectory = System.IO.Path.Combine(path, findData.cFileName);
                                dirs.Add(GetCleanPath(subdirectory));
                                if (searchOption == System.IO.SearchOption.AllDirectories)
                                {
                                    InternalGetDirectories(subdirectory, searchPattern, searchOption, ref dirs);
                                }
                            }
                        }
                    } while (Win32FileSystem.FindNextFile(findHandle, out findData));
                    Win32FileSystem.FindClose(findHandle);
                }
                else
                {
                    //ThrowWin32Exception();
                }
            }
            catch (Exception)
            {
                Win32FileSystem.FindClose(findHandle);
                throw;
            }
        }
Example #18
0
 public static System.IO.FileAttributes GetAttributes(string path)
 {
     return((System.IO.FileAttributes)Win32FileSystem.GetFileAttributesW(GetWin32LongPath(path)));
 }
Example #19
0
        public static bool Exists(string path)
        {
            var attr = Win32FileSystem.GetFileAttributesW(GetWin32LongPath(path));

            return(attr != Win32FileSystem.INVALID_FILE_ATTRIBUTES && ((attr & Win32FileSystem.FILE_ATTRIBUTE_DIRECTORY) == Win32FileSystem.FILE_ATTRIBUTE_DIRECTORY));
        }