Example #1
0
 internal FileInfoEx(DirectoryInfoEx parentDir, PIDL relPIDL)
 {
     Parent = parentDir;
     //0.15: Fixed ShellFolder not freed.
     using (ShellFolder2 parentShellFolder = parentDir.ShellFolder)
         init(parentShellFolder, parentDir.PIDLRel, relPIDL);
 }
 public static DirectoryModel FromExEntry(DirectoryInfoEx entry)
 {
     if (entry.FullName.EndsWith(":\\"))
         return new DriveModel(entry);
     else
         return new DirectoryModel(entry as DirectoryInfoEx);
 }
        public override bool Equals(FileSystemInfoEx other)
        {
            if (other is DirectoryInfoEx)
            {
                if (this.FullName.Equals(other.FullName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(true);
                }

                DirectoryInfoEx dirObj = other as DirectoryInfoEx;
                if (dirObj.FullName.Equals(IOTools.IID_UserFiles))
                {
                    if (this.FullName.Equals(DirectoryInfoEx.CurrentUserDirectory.FullName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(true);
                    }
                }
                if (dirObj.FullName.Equals(IOTools.IID_Public))
                {
                    if (this.FullName.Equals(DirectoryInfoEx.SharedDirectory.FullName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(true);
                    }
                }
            }

            return(base.Equals(other));
        }
 public void BroadcastCurrentDirectoryChanging(DirectoryInfoEx newDirectory)
 {
     foreach (DirectoryTreeItemViewModel rootmodel in _rootDirectoryModelList)
     {
         rootmodel.BroascastCurrentDirectoryChanging(newDirectory);
     }
 }
        static DirectoryInfoEx()
        {
#if DEBUG
            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
            {
                throw new Exception("This should not be executed when design time.");
            }
#endif
            DesktopDirectory     = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.DESKTOP));
            MyComputerDirectory  = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.DRIVES));
            CurrentUserDirectory = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.PROFILE));
            //0.17: Fixed some system cannot create shared directories. (by cwharmon)
            try { SharedDirectory = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.COMMON_DOCUMENTS)); }
            catch { }
            NetworkDirectory    = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.NETWORK));
            RecycleBinDirectory = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.BITBUCKET));

            //foreach (DirectoryInfoEx dir in DesktopDirectory.GetDirectories())
            //    if (dir.FullName.Equals(Helper.GetCurrentUserPath()))
            //    { CurrentUserDirectory = dir; break; }

            //foreach (DirectoryInfoEx dir in DesktopDirectory.GetDirectories())
            //    if (dir.FullName.Equals(Helper.GetSharedPath()))
            //    { SharedDirectory = dir; break; }
        }
Example #6
0
 internal FileInfoEx(DirectoryInfoEx parentDir, PIDL relPIDL)
 {
     Parent = parentDir;
     //0.15: Fixed ShellFolder not freed.
     using (ShellFolder2 parentShellFolder = parentDir.ShellFolder)
         init(parentShellFolder, parentDir.PIDLRel, relPIDL);
 }
 public static IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption)
 {
     DirectoryInfoEx rootDir = new DirectoryInfoEx(path);
     IEnumerator<FileInfoEx> fileEnumerator = rootDir.EnumerateFiles(searchPattern, searchOption).GetEnumerator();
     while (fileEnumerator.MoveNext())
         yield return fileEnumerator.Current.FullName;
 }
Example #8
0
        /// <summary>
        /// Copy directory or file, take full path of source and dest as parameter.
        /// </summary>
        public static void Copy(string source, string dest)
        {
            DirectoryInfoEx fromDir, toDir;

            fromDir = new DirectoryInfoEx(Path.GetDirectoryName(source));
            toDir   = new DirectoryInfoEx(Path.GetDirectoryName(dest));


            if (fromDir.Storage == null)
            {
                throw new IOException("Source directory does not support IStorage");
            }
            if (toDir.Storage == null)
            {
                throw new IOException("Destination directory does not support IStorage");
            }


            int hr = fromDir.Storage.MoveElementTo(Path.GetFileName(source), toDir.Storage,
                                                   Path.GetFileName(dest), ShellAPI.STGMOVE.COPY);

            if (hr != ShellAPI.S_OK)
            {
                Marshal.ThrowExceptionForHR(hr);
            }
        }
Example #9
0
        protected void initParent()
        {
            if (!_parentInited)
            {
                if (Exists)
                {
                    if (FullName.Equals(IOTools.IID_Desktop))
                    {
                        return;
                    }

                    this.RequestPIDL((pidl) =>
                    {
                        //PIDL relPIDL;
                        _parent       = new DirectoryInfoEx(getParentPIDL(pidl));
                        _parentInited = true;
                    });
                }
                else
                {
                    _parent       = new DirectoryInfoEx(PathEx.GetDirectoryName(FullName));
                    _parentInited = true;
                }
            }
        }
        public DirectoryTreeItemViewModel LookupChild(DirectoryInfoEx directory, Func<bool> cancelCheck)
        {
            if (cancelCheck != null && cancelCheck())
                return null;

            if (Parent != null)
                Parent.IsExpanded = true;
            if (directory == null)
                return null;

            if (!IsLoaded)
            {
                IsLoading = true;
                SubDirectories = getDirectories();
                HasSubDirectories = SubDirectories.Count > 0;
                IsLoading = false;
            }

            foreach (DirectoryTreeItemViewModel subDirModel in SubDirectories)
            {
                if (!subDirModel.Equals(dummyNode))
                {
                    DirectoryInfoEx subDir = subDirModel.EmbeddedDirectoryModel.EmbeddedDirectoryEntry;
                    if (directory.Equals(subDir))
                        return subDirModel;
                    else if (IOTools.HasParent(directory, subDir))
                        return subDirModel.LookupChild(directory, cancelCheck);
                }
            }
            return null;
        }
Example #11
0
 internal FileInfoEx(IShellFolder2 parentShellFolder, DirectoryInfoEx parentDir, PIDL relPIDL)
 {
     Parent = parentDir;
     parentDir.RequestPIDL(parentPIDL =>
     {
         init(parentShellFolder, parentPIDL, relPIDL);
     });
 }
Example #12
0
 internal FileInfoEx(DirectoryInfoEx parentDir, PIDL relPIDL)
 {
     Parent = parentDir;
     //0.15: Fixed ShellFolder not freed.
     using (ShellFolder2 parentShellFolder = parentDir.ShellFolder)
         parentDir.RequestRelativePIDL(parentRelPidl =>
                                       init(parentShellFolder, parentRelPidl, relPIDL));
 }
Example #13
0
 public static async void AddFolderQuery(DirectoryInfoEx di)
 {
     var dr = MessageBoxResult.No;
     if (di.HasSubFolder)
         dr = MessageBox.Show("Import subfolders?", "Add Folder", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.No);
     var r = dr == MessageBoxResult.Yes;
     if (r || dr == MessageBoxResult.No)
         await FileUtils.AddFolder(di, r, DefaultLoadErrorCallback);
 }
Example #14
0
 internal DirectoryInfoEx(DirectoryInfoEx parentDir, PIDL relPIDL)
 {
     Parent = parentDir;
     parentDir.RequestPIDL(parentPIDL =>
     {
         //0.15: Fixed ShellFolder not freed.
         using (ShellFolder2 parentShellFolder = parentDir.ShellFolder)
             init(parentShellFolder, parentPIDL, relPIDL);
     });
 }
Example #15
0
        public static IEnumerable <string> EnumerateFileSystemEntries(string path, string searchPattern, SearchOption searchOption)
        {
            DirectoryInfoEx rootDir = new DirectoryInfoEx(path);
            IEnumerator <FileSystemInfoEx> fsiEnumerator = rootDir.EnumerateFileSystemInfos(searchPattern, searchOption).GetEnumerator();

            while (fsiEnumerator.MoveNext())
            {
                yield return(fsiEnumerator.Current.FullName);
            }
        }
Example #16
0
        /// <summary>
        /// Take a directory and return the IStorage PTr interface.
        /// </summary>
        internal static bool getIStorage(DirectoryInfoEx dir, out IntPtr storagePtr, out IStorage storage)
        {
            bool retVal = getIStorage(dir, out storagePtr);

            storage = null;
            if (retVal)
            {
                storage = (IStorage)Marshal.GetTypedObjectForIUnknown(storagePtr, typeof(IStorage));
            }
            return(retVal);
        }
Example #17
0
        /// <summary>
        /// Take a directory and return the IStorage PTr interface.
        /// </summary>
        internal static bool getIStorage(DirectoryInfoEx dir, out IntPtr storagePtr)
        {
            //0.19 Fixed ArgumentException when getting storage of C:\{User}\Desktop.
            if (dir.FullName.Equals(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)))
            {
                dir = DirectoryInfoEx.DesktopDirectory;
            }

            IntPtr storePtr = IntPtr.Zero;

            //0.15 : Fixed PIDL not freed correctly.
            try
            {
                return(dir.RequestRelativePIDL(dirPIDLRel =>
                {
                    int hr;
                    if (dirPIDLRel.Size == 0)
                    {
                        IntPtr pidlLast = IntPtr.Zero;
                        hr = ShellAPI.SHBindToParent(dirPIDLRel.Ptr, ShellAPI.IID_IStorage,
                                                     out storePtr, ref pidlLast);
                    }
                    else
                    {
                        //0.15: Fixed ShellFolder not freed correctly.
                        using (ShellFolder2 dirParentShellFolder = dir.Parent.ShellFolder)
                            if (dirParentShellFolder != null)
                            {
                                hr = dirParentShellFolder.BindToStorage(
                                    dirPIDLRel.Ptr, IntPtr.Zero, ref ShellAPI.IID_IStorage,
                                    out storePtr);
                            }
                            else
                            {
                                storePtr = IntPtr.Zero;
                                return false;
                            }
                    }

                    if ((hr != ShellAPI.S_OK))
                    {
                        storePtr = IntPtr.Zero;
                        Marshal.ThrowExceptionForHR(hr);
                        return false;
                    }

                    return true;
                }));
            }
            finally
            {
                storagePtr = storePtr;
            }
        }
Example #18
0
        internal static bool getIStorage(DirectoryInfoEx dir, out Storage storage)
        {
            IntPtr storagePtr;
            bool   retVal = getIStorage(dir, out storagePtr);

            storage = null;
            if (getIStorage(dir, out storagePtr))
            {
                storage = new Storage(storagePtr);
            }
            return(retVal);
        }
Example #19
0
        internal PIDL getPIDL()
        {
            if (_pidl != null)
            {
                return(new PIDL(_pidl, true));
            }

            if (FullName == "::{00021400-0000-0000-C000-000000000046}") //Desktop
            {
                return(DirectoryInfoEx.CSIDLtoPIDL(ShellAPI.CSIDL.CSIDL_DESKTOP));
            }
            return(PathToPIDL(FullName));
        }
 internal void BroascastCurrentDirectoryChanging(DirectoryInfoEx newDirectory)
 {
     if (IsExpanded)
         if (this.EmbeddedDirectoryModel.EmbeddedDirectoryEntry.Equals(newDirectory) ||
             IOTools.HasParent(newDirectory, this.EmbeddedDirectoryModel.EmbeddedDirectoryEntry) ||
             IOTools.HasParent(this.EmbeddedDirectoryModel.EmbeddedDirectoryEntry, newDirectory))
         {
             if (IsLoaded)
                 foreach (DirectoryTreeItemViewModel subItem in SubDirectories)
                     subItem.BroascastCurrentDirectoryChanging(newDirectory);
         }
         else IsExpanded = false;
 }
Example #21
0
        protected void init(DirectoryInfoEx dir, bool includeSubdir)
        {
            if (_sww == null || !_sww.MonitorDir.Equals(dir) || includeSubdir != _sww.IncludeSubDirectories)
            {
                if (_sww != null)
                {
                    _sww.OnEvent -= new ShellChangeEventHandler(HandleEvent);
                }

                _sww          = new SystemWatcherWrapper(dir, includeSubdir);
                _sww.OnEvent += new ShellChangeEventHandler(HandleEvent);
            }
        }
Example #22
0
 internal FileInfoEx(IShellFolder2 parentShellFolder, DirectoryInfoEx parentDir, PIDL relPIDL)
 {
     Parent = parentDir;
     PIDL parentPIDL = parentDir.PIDL;
     try
     {
         init(parentShellFolder, parentPIDL, relPIDL);
     }
     finally
     {
         if (parentPIDL != null) parentPIDL.Free();
         parentPIDL = null;
     }
 }
Example #23
0
 /// <summary>
 /// Get relative path of a entry based on baseDirectory.
 /// e.g. C:\Temp\AbC\1.txt (entry), C:\Temp\ (baseDirectory) will return ABC\1.txt
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="baseDirectory"></param>
 /// <returns></returns>
 public static string GetRelativePath(string name, DirectoryInfoEx baseDirectory)
 {
     if (name.IndexOf(baseDirectory.FullName, StringComparison.InvariantCultureIgnoreCase) == -1)
     {
         if (Debugger.IsAttached)
         {
             Debugger.Break();
         }
         return(PathEx.GetFileName(name));
     }
     else
     {
         return(name.Substring(baseDirectory.FullName.Length + 1));
     }
 }
        internal static PIDL getParentPIDL(PIDL pidl, out PIDL relPIDL)
        {
            relPIDL = new PIDL(pidl, true); //0.21
            if (pidl.Size == 0)
            {
                return(pidl);
            }
            IntPtr pParent = PIDL.ILClone(pidl.Ptr);

            relPIDL = getRelativePIDL(pidl);
            if (pParent == IntPtr.Zero || !PIDL.ILRemoveLastID2(ref pParent))
            {
                return(DirectoryInfoEx.CSIDLtoPIDL(ShellAPI.CSIDL.DESKTOP));
            }

            return(new PIDL(pParent, false)); //pParent will be freed by the PIDL.
        }
Example #25
0
        internal DirectoryInfoEx(DirectoryInfoEx parentDir, PIDL relPIDL)
        {
            Parent = parentDir;
            PIDL parentPIDL = parentDir.PIDL;

            try
            {
                //0.15: Fixed ShellFolder not freed.
                using (ShellFolder2 parentShellFolder = parentDir.ShellFolder)
                    init(parentShellFolder, parentPIDL, relPIDL);
            }
            finally { if (parentPIDL != null)
                      {
                          parentPIDL.Free();
                      }
                      parentPIDL = null; }
        }
Example #26
0
        /// <summary>
        /// Takes a directoryInfoEx and return the first parent with directory type = desktop or drive.
        /// </summary>
        internal static DirectoryInfoEx getDirectoryRoot(DirectoryInfoEx lookup)
        {
            DirectoryInfoEx dir = lookup.Parent;

            while (dir.DirectoryType != DirectoryTypeEnum.dtDesktop &&
                   dir.DirectoryType != DirectoryTypeEnum.dtDrive &&
                   dir.DirectoryType != DirectoryTypeEnum.dtRoot &&
                   dir != null)
            {
                dir = dir.Parent;
            }
            if (dir == null)
            {
                throw new IOException("Internal exception in GetDirectoryRoot.");
            }
            return(dir);
        }
Example #27
0
 public static async Task<bool> AddFolder(DirectoryInfoEx dir, bool subfolders, Action<Exception, string> errorCallback)
 {
     var arr = Task.Run(() =>
     {
         IEnumerable<IMusicInfo> stuff;
         if (subfolders)
             stuff = from f in dir.EnumerateFilesEx()
                     where PlaybackManager.Instance.HasSupportingPlayer(f.Name.GetExt())
                     select MusicInfo.Create(f, errorCallback);
         else
             stuff = from f in dir.GetFiles()
                     where PlaybackManager.Instance.HasSupportingPlayer(f.Name.GetExt())
                     select MusicInfo.Create(f, errorCallback);
         return stuff.ToArray();
     });
     PlaybackManager.Instance.Playlist.AddRange(await arr);
     return true;
 }
Example #28
0
        public static DirectoryInfoEx CreateDirectory(string path)
        {
            DirectoryInfoEx dir = new DirectoryInfoEx(path);
            dir.Create();
            return dir;
            //if (DirectoryEx.Exists(path))
            //    throw new IOException(path + " already exist.");

            //string name = Path.GetFileName(path);
            //DirectoryInfoEx rootDir = new DirectoryInfoEx(Path.GetDirectoryName(path));

            //IntPtr outPtr;
            //rootDir.Storage.CreateStorage(name, ShellDll.ShellAPI.STGM.FAILIFTHERE |
            //    ShellDll.ShellAPI.STGM.CREATE, 0, 0, out outPtr);
            //Storage storage = new Storage(outPtr);

            //return new DirectoryInfoEx(path);
        }
Example #29
0
        internal FileInfoEx(IShellFolder2 parentShellFolder, DirectoryInfoEx parentDir, PIDL relPIDL)
        {
            Parent = parentDir;
            PIDL parentPIDL = parentDir.PIDL;

            try
            {
                init(parentShellFolder, parentPIDL, relPIDL);
            }
            finally
            {
                if (parentPIDL != null)
                {
                    parentPIDL.Free();
                }
                parentPIDL = null;
            }
        }
Example #30
0
        /// <summary>
        /// Copy directory or file, take full path of source and dest as parameter.
        /// </summary>
        public static void Copy(string source, string dest)
        {
            DirectoryInfoEx fromDir, toDir;

            fromDir = new DirectoryInfoEx(PathEx.GetDirectoryName(source));
            toDir = new DirectoryInfoEx(PathEx.GetDirectoryName(dest));

            if (fromDir.Storage == null)
                throw new IOException("Source directory does not support IStorage");
            if (toDir.Storage == null)
                throw new IOException("Destination directory does not support IStorage");

            int hr = fromDir.Storage.MoveElementTo(PathEx.GetFileName(source), toDir.Storage,
                Path.GetFileName(dest), ShellAPI.STGMOVE.COPY);

            if (hr != ShellAPI.S_OK)
                Marshal.ThrowExceptionForHR(hr);
        }
Example #31
0
        void init(DirectoryInfoEx rootDir)
        {
            _rootDirectory = rootDir;
            _name          = rootDir.FullName;
            _label         = rootDir.Label;


            if (rootDir.FullName.Length == 3 & rootDir.FullName.Substring(1).Equals(":\\"))
            {
                DriveInfo innerDriveInfo = new DriveInfo(this.Name);
                this._isReady   = innerDriveInfo.IsReady;
                this._driveType = innerDriveInfo.DriveType;
                if (this._isReady)
                {
                    this._freeSpace   = innerDriveInfo.AvailableFreeSpace;
                    this._totalSize   = innerDriveInfo.TotalSize;
                    this._driveFormat = innerDriveInfo.DriveFormat;
                }

                //string objAlias = "Win32_LogicalDisk.DeviceID=\"" + rootDir.FullName.Substring(0, 1) +  ":\"";
                //Management.ManagementObject disk =
                //    new Management.ManagementObject(objAlias);
                //try
                //{
                //    _isReady = true;
                //    _totalSize = Convert.ToInt64(disk["Size"]);
                //    _freeSpace = Convert.ToInt64(disk["FreeSpace"]);

                //    uint driveType = Convert.ToUInt32(disk["DriveType"]);
                //    if (driveType <= 6) _driveType = (DriveType)driveType; else _driveType = DriveType.Unknown;


                //}
                //catch //Disconnected Network Drives etc. will generate an error here, just assume that it is a network drive.
                //{
                //    _driveType = DriveType.Network;
                //    _isReady = false;
                //}
            }
            else
            {
                _driveType = DriveType.Network;
            }
        }
 public void RefreshCurrentDirectory(DirectoryInfoEx dInfo, ICurrentDirectoryViewModelFactory DirViewModelFactory)
 {
     if (DirViewModelFactory != null)
     {
         _curDirViewModelFactory = DirViewModelFactory;
     }
   
     if (CurrentDirectory == null || dInfo.FullName != CurrentDirectory.FullName)
     {
         CurrentDirectory = dInfo;
     }
     else
     {
       if (dInfo != null)
       {     //if (_currentDirectoryModel == null) || _currentDirectoryModel.EmbeddedModel.FullName != dInfo.FullName
           ReInitializeCurrentDirectoryModel(dInfo);
       }
     }
 }
Example #33
0
        public SystemWatcherWrapper(DirectoryInfoEx dir, bool includeSubDir)
        {
            MonitorDir            = dir;
            _dirPIDL              = dir.getPIDL();
            IncludeSubDirectories = includeSubDir;

            this.CreateHandle(new CreateParams());

            ShellAPI.SHChangeNotifyEntry entry = new ShellAPI.SHChangeNotifyEntry();

            entry.pIdl        = _dirPIDL.Ptr;
            entry.Recursively = includeSubDir;
            _notifyID         = ShellAPI.SHChangeNotifyRegister(this.Handle,
                                                                ShellAPI.SHCNRF.InterruptLevel | ShellAPI.SHCNRF.ShellLevel,
                                                                ShellAPI.SHCNE.ALLEVENTS | ShellAPI.SHCNE.INTERRUPT,
                                                                ShellAPI.WM.SH_NOTIFY,
                                                                1,
                                                                new ShellAPI.SHChangeNotifyEntry[] { entry });
        }
Example #34
0
        public static DirectoryInfoEx CreateDirectory(string path)
        {
            DirectoryInfoEx dir = new DirectoryInfoEx(path);

            dir.Create();
            return(dir);
            //if (DirectoryEx.Exists(path))
            //    throw new IOException(path + " already exist.");

            //string name = Path.GetFileName(path);
            //DirectoryInfoEx rootDir = new DirectoryInfoEx(Path.GetDirectoryName(path));

            //IntPtr outPtr;
            //rootDir.Storage.CreateStorage(name, ShellDll.ShellAPI.STGM.FAILIFTHERE |
            //    ShellDll.ShellAPI.STGM.CREATE, 0, 0, out outPtr);
            //Storage storage = new Storage(outPtr);

            //return new DirectoryInfoEx(path);
        }
Example #35
0
 /// <summary>
 /// Check if the file / directory exists.
 /// </summary>
 public static bool Exists(string path, bool isDir)
 {
     try
     {
         DirectoryInfoEx dir = new DirectoryInfoEx(Path.GetDirectoryName(path));
         bool            temp;
         if (dir.Contains(Path.GetFileName(path), out temp))
         {
             return(temp == isDir);
         }
         else
         {
             return(false);
         }
     }
     catch
     {
         return(false);
     }
 }
Example #36
0
        //0.13: Added HasParent
        /// <summary>
        /// Return whether parent directory contain child directory.
        /// Aware Library, UserFiles and Public directory too.
        /// </summary>
        /// <param name="child"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static bool HasParent(FileSystemInfoEx child, DirectoryInfoEx parent)
        {
            if (parent == null)
            {
                //if (Debugger.IsAttached)
                //    Debugger.Break();
                return(false);
            }

            //::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Music.library-ms
            if (parent.FullName.StartsWith(IOTools.IID_Library) && parent.FullName.EndsWith(".library-ms"))
            {
                //Reverse
                foreach (DirectoryInfoEx subDir in parent.GetDirectories())
                {
                    if (subDir.Equals(child) || HasParent(child, subDir))
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else
            {
                if (child.FullName.StartsWith(parent.FullName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(true);
                }

                if (child.FullName.StartsWith(IID_UserFiles) || child.FullName.StartsWith(IID_Public))
                {
                    return(false);
                }
                FileSystemInfoEx current = child.Parent;
                while (current != null && !parent.Equals(current))
                {
                    current = current.Parent;
                }
                return(current != null);
            }
        }
        //0.3
        public DriveModel(DirectoryInfoEx dir)
            : base(dir)
        {
            try
            {
                DriveInfo driveInfo = new DriveInfo(dir.FullName.Replace(":\\", ""));
                IsReady = driveInfo.IsReady;
                if (IsReady)
                {
                    VolumeLabel = driveInfo.VolumeLabel;
                    TotalSize = driveInfo.TotalSize;
                    FreeSpace = driveInfo.AvailableFreeSpace;
                    DriveType = driveInfo.DriveType;
                    DriveFormat = driveInfo.DriveFormat;
                    PercentFull = (int)((float)(TotalSize - FreeSpace) / (float)TotalSize * 100);
                }
            }
                //0.5
            catch (ArgumentException) //Drive not found
            {

            }
        }
        protected void initParent()
        {
            if (!_parentInited)
            {
                if (Exists)
                {
                    if (FullName.Equals(IOTools.IID_Desktop))
                    {
                        return;
                    }
                    PIDL relPIDL;
                    _parent       = new DirectoryInfoEx(getParentPIDL(PIDL, out relPIDL));
                    _parentInited = true;

                    relPIDL.Free();
                }
                else
                {
                    _parent       = new DirectoryInfoEx(PathEx.GetDirectoryName(FullName));
                    _parentInited = true;
                }
            }
        }
        static DirectoryInfoEx()
        {
            #if DEBUG
            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
                throw new Exception("This should not be executed when design time.");
            #endif
            DesktopDirectory = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.DESKTOP));
            MyComputerDirectory = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.DRIVES));
            CurrentUserDirectory = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.PROFILE));
            //0.17: Fixed some system cannot create shared directories. (by cwharmon)
            try { SharedDirectory = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.COMMON_DOCUMENTS)); }
            catch { }
            NetworkDirectory = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.NETWORK));
            RecycleBinDirectory = new DirectoryInfoEx(CSIDLtoPIDL(ShellAPI.CSIDL.BITBUCKET));

            //foreach (DirectoryInfoEx dir in DesktopDirectory.GetDirectories())
            //    if (dir.FullName.Equals(Helper.GetCurrentUserPath()))
            //    { CurrentUserDirectory = dir; break; }

            //foreach (DirectoryInfoEx dir in DesktopDirectory.GetDirectories())
            //    if (dir.FullName.Equals(Helper.GetSharedPath()))
            //    { SharedDirectory = dir; break; }
        }
        public static FileSystemInfoEx ResolveSymLink(this FileSystemInfoEx info)
        {
            var    path      = info.FullName;
            string directory = Path.GetDirectoryName(path);
            string file      = Path.GetFileName(path);

            Shell32.Shell      shell      = new Shell32.Shell();
            Shell32.Folder     folder     = shell.NameSpace(directory);
            Shell32.FolderItem folderItem = folder.ParseName(file);

            Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;

            FileSystemInfoEx ret = null;

            if (Path.GetDirectoryName(link.Path) == link.Path)
            {
                ret = new DirectoryInfoEx(link.Path);
            }
            else
            {
                ret = new FileInfoEx(link.Path);
            }
            return(ret);
        }
Example #41
0
        protected virtual void init(string file, FileMode mode, FileAccess access)
        {
            _fileName = file;
            string          dirPath = Path.GetDirectoryName(file);
            DirectoryInfoEx dir     = new DirectoryInfoEx(dirPath);

            if (dir == null)
            {
                throw new IOException(String.Format("Directory not exists: {0}", dirPath));
            }
            if (!IOTools.getIStorage(dir, out tempParentStoragePtr, out tempParentStorage))
            {
                throw new IOException(String.Format("Failed to obtaiin IStorage: {0}", dirPath));
            }
            IOTools.openStream(tempParentStorage, file, ref mode, ref access, out streamPtr, out stream);

            this._canRead  = (access == FileAccess.Read || access == FileAccess.ReadWrite);
            this._canWrite = (access == FileAccess.Write || access == FileAccess.ReadWrite);
            currentPos     = 0;

            if (_canRead)
            {
                stream.Stat(out streamInfo, ShellAPI.STATFLAG.NONAME);
            }

            switch (mode)
            {
            case FileMode.Append:
                Seek(0, SeekOrigin.End);
                break;

            case FileMode.CreateNew:

                break;
            }
        }
        public static string GetDisplayName(DirectoryInfoEx dx)
        {
            IntPtr pidl;
            uint pchEaten = 0;
            var sfgao = new ShellAPI.SFGAO();
            DirectoryInfoEx.DesktopDirectory.ShellFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, dx.FullName, ref pchEaten, out pidl, ref sfgao);

            var ptrStr = Marshal.AllocCoTaskMem(ShellAPI.MAX_PATH * 2 + 4);
            Marshal.WriteInt32(ptrStr, 0, 0);
            var buf = new StringBuilder(ShellAPI.MAX_PATH);
            try
            {
                DirectoryInfoEx.DesktopDirectory.ShellFolder.GetDisplayNameOf(pidl, ShellAPI.SHGNO.NORMAL, ptrStr);
                ShellAPI.StrRetToBuf(ptrStr, pidl, buf, ShellAPI.MAX_PATH);
            }
            finally
            {
                if (ptrStr != IntPtr.Zero)
                    Marshal.FreeCoTaskMem(ptrStr);
                ptrStr = IntPtr.Zero;
            }

            return buf.ToString();
        }
        protected virtual void init(string file, FileMode mode, FileAccess access)
        {
            _fileName = file;
            string dirPath = Path.GetDirectoryName(file);
            DirectoryInfoEx dir = new DirectoryInfoEx(dirPath);
            if (dir == null) throw new IOException(String.Format("Directory not exists: {0}", dirPath));
            if (!IOTools.getIStorage(dir, out tempParentStoragePtr, out tempParentStorage))
                throw new IOException(String.Format("Failed to obtaiin IStorage: {0}", dirPath));
            IOTools.openStream(tempParentStorage, file, ref mode, ref access, out streamPtr, out stream);

            this._canRead = (access == FileAccess.Read || access == FileAccess.ReadWrite);
            this._canWrite = (access == FileAccess.Write || access == FileAccess.ReadWrite);
            currentPos = 0;

            if (_canRead) stream.Stat(out streamInfo, ShellAPI.STATFLAG.NONAME);

            switch (mode)
            {
                case FileMode.Append :
                    Seek(0, SeekOrigin.End);
                    break;
                case FileMode.CreateNew:

                    break;
            }
        }
Example #44
0
        //0.13: Added HasParent
        /// <summary>
        /// Return whether parent directory contain child directory.
        /// Aware Library, UserFiles and Public directory too.
        /// </summary>
        /// <param name="child"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static bool HasParent(FileSystemInfoEx child, DirectoryInfoEx parent)
        {
            if (parent == null)
            {
                //if (Debugger.IsAttached)
                //    Debugger.Break();
                return false;
            }

            //::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Music.library-ms
            if (parent.FullName.StartsWith(IOTools.IID_Library) && parent.FullName.EndsWith(".library-ms"))
            {
                //Reverse
                foreach (DirectoryInfoEx subDir in parent.GetDirectories())
                    if (subDir.Equals(child) || HasParent(child, subDir))
                        return true;
                return false;
            }
            else
            {
                if (child.FullName.StartsWith(parent.FullName.TrimEnd('\\') + "\\", StringComparison.InvariantCultureIgnoreCase))
                    return true;

                if (child.FullName.StartsWith(IID_UserFiles) || child.FullName.StartsWith(IID_Public))
                    return false;
                FileSystemInfoEx current = child.Parent;
                while (current != null && !parent.Equals(current))
                    current = current.Parent;
                return (current != null);
            }
        }
Example #45
0
 /// <summary>
 /// Get relative path of a entry based on baseDirectory.
 /// e.g. C:\Temp\AbC\1.txt (entry), C:\Temp\ (baseDirectory) will return ABC\1.txt
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="baseDirectory"></param>
 /// <returns></returns>
 public static string GetRelativePath(string name, DirectoryInfoEx baseDirectory)
 {
     if (name.IndexOf(baseDirectory.FullName, StringComparison.InvariantCultureIgnoreCase) == -1)
     {
         if (Debugger.IsAttached)
             Debugger.Break();
         return PathEx.GetFileName(name);
     }
     else return name.Substring(baseDirectory.FullName.Length + 1);
 }
Example #46
0
 public static DirectoryInfoEx GetParent(string path)
 {
     DirectoryInfoEx dir = new DirectoryInfoEx(path);
     return dir.Parent;
 }
Example #47
0
 public static string[] GetFileSystemEntries(string path)
 {
     DirectoryInfoEx rootDir = new DirectoryInfoEx(path);
     return FSListToStringList(rootDir.GetFileSystemInfos());
 }
 /// <summary>
 /// Takes a directoryInfoEx and return the first parent with directory type = desktop or drive.
 /// </summary>
 internal static DirectoryInfoEx getDirectoryRoot(DirectoryInfoEx lookup)
 {
     DirectoryInfoEx dir = lookup.Parent;
     while (dir.DirectoryType != DirectoryTypeEnum.dtDesktop &&
         dir.DirectoryType != DirectoryTypeEnum.dtDrive &&
         dir.DirectoryType != DirectoryTypeEnum.dtRoot &&
         dir != null)
         dir = dir.Parent;
     if (dir == null)
         throw new IOException("Internal exception in GetDirectoryRoot.");
     return dir;
 }
Example #49
0
 internal static bool getIStorage(DirectoryInfoEx dir, out Storage storage)
 {
     IntPtr storagePtr;
     bool retVal = getIStorage(dir, out storagePtr);
     storage = null;
     if (getIStorage(dir, out storagePtr))
         storage = new Storage(storagePtr);
     return retVal;
 }
Example #50
0
 /// <summary>
 /// Check if the file / directory exists.
 /// </summary>
 public static bool Exists(string path, bool isDir)
 {
     try
     {
         DirectoryInfoEx dir = new DirectoryInfoEx(Path.GetDirectoryName(path));
         bool temp;
         if (dir.Contains(Path.GetFileName(path), out temp))
             return temp == isDir;
         else return false;
     }
     catch
     {
         return false;
     }
 }
Example #51
0
 public DriveInfoEx(DirectoryInfoEx dir)
 {
     init(dir);
 }
 internal DirectoryInfoEx(DirectoryInfoEx parentDir, PIDL relPIDL)
 {
     Parent = parentDir;
     PIDL parentPIDL = parentDir.PIDL;
     try
     {
         //0.15: Fixed ShellFolder not freed.
         using (ShellFolder2 parentShellFolder = parentDir.ShellFolder)
             init(parentShellFolder, parentPIDL, relPIDL);
     }
     finally { if (parentPIDL != null) parentPIDL.Free(); parentPIDL = null; }
 }
        public IEnumerable<DirectoryInfoEx> EnumerateDirectories(String searchPattern, SearchOption searchOption, CancelDelegate cancel)
        {
            IntPtr ptrEnum = IntPtr.Zero;
            IEnumIDList IEnum = null;
            PIDL parentPIDL = this.PIDL;

            using (ShellFolder2 sf = this.ShellFolder)
                try
                {
                    if (sf.EnumObjects(IntPtr.Zero, flag, out ptrEnum) == ShellAPI.S_OK)
                    {
                        IEnum = (IEnumIDList)Marshal.GetTypedObjectForIUnknown(ptrEnum, typeof(IEnumIDList));
                        IntPtr pidlSubItem;
                        int celtFetched;

                        while (!IOTools.IsCancelTriggered(cancel) && IEnum.Next(1, out pidlSubItem, out celtFetched) == ShellAPI.S_OK && celtFetched == 1)
                        {

                            ShellAPI.SFGAO attribs = ShellAPI.SFGAO.FOLDER | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.STREAM |
                                ShellAPI.SFGAO.FILESYSANCESTOR | ShellAPI.SFGAO.NONENUMERATED;
                            sf.GetAttributesOf(1, new IntPtr[] { pidlSubItem }, ref attribs);
                            bool isZip = ((attribs & ShellAPI.SFGAO.FOLDER) != 0 && (attribs & ShellAPI.SFGAO.STREAM) != 0);
                            bool isDir = ((attribs & ShellAPI.SFGAO.FOLDER) != 0);
                            //0.18 Added a check for NonEnumerated items so DirectoryInfoEx.EnumerateDirectories wont return some system directories (e.g. C:\MSOCache)
                            //bool isNonEnumerated = ((attribs & ShellAPI.SFGAO.NONENUMERATED) != 0);
                            bool isFileAncestor = ((attribs & ShellAPI.SFGAO.FILESYSANCESTOR) != 0);
                            bool includedFolder = false;

                            if (!isZip && !isFileAncestor) //0.14 : Added allowed folder list so Non-FileAncestor directory (e.g. recycle-bin) is listed.
                            {
                                string[] allowedPaths = new string[]
                                {
                                    "::{645FF040-5081-101B-9F08-00AA002F954E}"
                                };
                                string path = PIDLToPath(new PIDL(pidlSubItem, false));
                                foreach (string allowedPath in allowedPaths)
                                    if (allowedPath == path)
                                        includedFolder = true;
                                if (!includedFolder)
                                    if (IOTools.HasParent(this, NetworkDirectory))
                                        includedFolder = true;

                            }
                            if (isDir && !isZip /*&& !isNonEnumerated*/ && (isFileAncestor || includedFolder))
                            {
                                PIDL subPidl = new PIDL(pidlSubItem, false);
                                //DirectoryInfoEx di = new DirectoryInfoEx(this, subPidl);
                                DirectoryInfoEx di = new DirectoryInfoEx(sf, parentPIDL, subPidl);
                                if (IOTools.MatchFileMask(di.Name, searchPattern))
                                    yield return di;
                                if (searchOption == SearchOption.AllDirectories)
                                {
                                  IEnumerator<DirectoryInfoEx> dirEnumerator = di.EnumerateDirectories(searchPattern, searchOption, cancel).GetEnumerator();

                                  while (dirEnumerator.MoveNext())
                                  {
                                      //Debug.Assert(dirEnumerator.Current.IsFolder);
                                      yield return dirEnumerator.Current;
                                  }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (parentPIDL != null)
                    {
                        parentPIDL.Free();
                        parentPIDL = null;
                    }

                    if (IEnum != null)
                    {
                        Marshal.ReleaseComObject(IEnum);
                        Marshal.Release(ptrEnum);
                    }
                }
        }
Example #54
0
 public static void SetCurrentDirectory(DependencyObject target, DirectoryInfoEx value)
 {
     target.SetValue(CurrentDirectoryProperty, value);
 }
Example #55
0
 public FileSystemWatcherEx(DirectoryInfoEx dir, bool includeSubdirectories)
 {
     init(dir, includeSubdirectories);
 }
Example #56
0
 /// <summary>
 /// Get relative path of a entry based on baseDirectory.
 /// e.g. C:\Temp\AbC\1.txt (entry), C:\Temp\ (baseDirectory) will return ABC\1.txt
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="baseDirectory"></param>
 /// <returns></returns>
 public static string GetRelativePath(FileSystemInfoEx entry, DirectoryInfoEx baseDirectory)
 {
     if (entry.FullName.IndexOf(baseDirectory.FullName, StringComparison.InvariantCultureIgnoreCase) == -1)
     {
         if (Debugger.IsAttached)
             Debugger.Break();
         return entry.Name;
     }
     else return entry.FullName.Substring(baseDirectory.FullName.Length + 1);
 }
Example #57
0
 public FileSystemWatcherEx(DirectoryInfoEx dir)
 {
     init(dir, true);
 }
Example #58
0
        public static string[] GetFileSystemEntries(string path)
        {
            DirectoryInfoEx rootDir = new DirectoryInfoEx(path);

            return(FSListToStringList(rootDir.GetFileSystemInfos()));
        }
Example #59
0
        /// <summary>
        /// Take a directory and return the IStorage PTr interface.
        /// </summary>
        internal static bool getIStorage(DirectoryInfoEx dir, out IntPtr storagePtr)
        {
            //0.19 Fixed ArgumentException when getting storage of C:\{User}\Desktop.
            if (dir.FullName.Equals(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)))
                dir = DirectoryInfoEx.DesktopDirectory;

            //0.15 : Fixed PIDL not freed correctly.
            PIDL dirPIDLRel = dir.PIDLRel;
            int hr;

            try
            {
                if (dirPIDLRel.Size == 0)
                {
                    IntPtr pidlLast = IntPtr.Zero;
                    hr = ShellAPI.SHBindToParent(dirPIDLRel.Ptr, ShellAPI.IID_IStorage,
                        out storagePtr, ref pidlLast);
                }
                else
                    //0.15: Fixed ShellFolder not freed correctly.
                    using (ShellFolder2 dirParentShellFolder = dir.Parent.ShellFolder)
                        if (dirParentShellFolder != null)
                            hr = dirParentShellFolder.BindToStorage(
                            dirPIDLRel.Ptr, IntPtr.Zero, ref ShellAPI.IID_IStorage,
                            out storagePtr);
                        else
                        {
                            storagePtr = IntPtr.Zero;
                            return false;
                        }

                if ((hr != ShellAPI.S_OK))
                {
                    storagePtr = IntPtr.Zero;
                    Marshal.ThrowExceptionForHR(hr);
                    return false;
                }
            }
            finally { if (dirPIDLRel != null) dirPIDLRel.Free(); dirPIDLRel = null; }

            return true;
        }
Example #60
0
 /// <summary>
 /// Take a directory and return the IStorage PTr interface.
 /// </summary>
 internal static bool getIStorage(DirectoryInfoEx dir, out IntPtr storagePtr, out IStorage storage)
 {
     bool retVal = getIStorage(dir, out storagePtr);
     storage = null;
     if (retVal)
         storage = (IStorage)Marshal.GetTypedObjectForIUnknown(storagePtr, typeof(IStorage));
     return retVal;
 }