Esempio n. 1
0
 private void MemoryTest()
 {
     for (int i = 0; i < 10000; i++)
     {
         ShellFolder folder = GetFolder("C:\\Windows");
         int         p      = folder.Files.Count;
         folder.Dispose();
     }
 }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NLog.config"));
            NLog.LogManager.Configuration.Variables["LogPath"] = storageFolder.Path;

            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

            if (HandleCommandLineArgs())
            {
                // Handles OpenShellCommandInExplorer
                return;
            }

            // Only one instance of the fulltrust process allowed
            // This happens if multiple instances of the UWP app are launched
            using var mutex = new Mutex(true, "FilesUwpFullTrust", out bool isNew);
            if (!isNew)
            {
                return;
            }

            try
            {
                // Create shell COM object and get recycle bin folder
                recycler = new ShellFolder(Vanara.PInvoke.Shell32.KNOWNFOLDERID.FOLDERID_RecycleBinFolder);
                Windows.Storage.ApplicationData.Current.LocalSettings.Values["RecycleBin_Title"] = recycler.Name;

                // Create shell watcher to monitor recycle bin folder
                watcher = new ShellItemChangeWatcher(recycler, false);
                watcher.NotifyFilter = ChangeFilters.AllDiskEvents;
                watcher.Changed     += Watcher_Changed;
                //watcher.EnableRaisingEvents = true; // TODO: uncomment this when updated library is released

                // Connect to app service and wait until the connection gets closed
                appServiceExit = new AutoResetEvent(false);
                InitializeAppServiceConnection();
                appServiceExit.WaitOne();
            }
            finally
            {
                connection?.Dispose();
                watcher?.Dispose();
                recycler?.Dispose();
                appServiceExit?.Dispose();
                mutex?.ReleaseMutex();
            }
        }
Esempio n. 3
0
        public static ContextMenu GetContextMenuForFolder(string folderPath, Shell32.CMF flags, Func <string, bool> itemFilter = null)
        {
            ShellFolder fsi = null;

            try
            {
                fsi = new ShellFolder(folderPath);
                return(GetContextMenuForFolder(fsi, flags, itemFilter));
            }
            catch (Exception ex) when(ex is ArgumentException || ex is FileNotFoundException)
            {
                // Return empty context menu
                return(null);
            }
            finally
            {
                fsi?.Dispose();
            }
        }
Esempio n. 4
0
        private void ShowGoToFolderDialog()
        {
            Common.MessageControls.Input inputControl = new Common.MessageControls.Input();
            inputControl.InputField.Text = NavigationManager.CurrentItem.Path;
            inputControl.InputField.SelectAll();
            inputControl.InputField.Focus();

            CairoMessage.ShowControl(Localization.DisplayString.sDesktop_GoToFolderMessage,
                                     Localization.DisplayString.sDesktop_GoToFolderTitle,
                                     CairoMessageImage.Default,
                                     inputControl,
                                     Localization.DisplayString.sInterface_Go,
                                     Localization.DisplayString.sInterface_Cancel,
                                     (bool?result) =>
            {
                string path = inputControl.InputField.Text;
                if (result != true || string.IsNullOrEmpty(path))
                {
                    return;
                }

                // Check if this is a valid folder
                ShellFolder shellFolder = new ShellFolder(path, IntPtr.Zero);
                bool valid = shellFolder.Loaded;
                shellFolder.Dispose();

                if (valid)
                {
                    NavigationManager.NavigateTo(path);
                }
                else
                {
                    CairoMessage.Show(Localization.DisplayString.sError_FileNotFoundInfo,
                                      Localization.DisplayString.sError_OhNo,
                                      MessageBoxButton.OK,
                                      CairoMessageImage.Error, (bool?errResult) =>
                    {
                        ShowGoToFolderDialog();
                    });
                }
            });
        }
Esempio n. 5
0
        private List <ApplicationInfo> generateAppList(string directory)
        {
            List <ApplicationInfo> rval   = new List <ApplicationInfo>();
            ShellFolder            folder = null;

            try
            {
                folder = new ShellFolder(directory, IntPtr.Zero, false, false);

                foreach (var file in folder.Files)
                {
                    if (file.IsFolder)
                    {
                        if (!file.Path.ToLower().EndsWith("\\startup"))
                        {
                            rval.AddRange(generateAppList(file.Path));
                        }
                    }
                    else
                    {
                        ApplicationInfo app = PathToApp(file.Path, file.DisplayName, false, false);
                        if (!ReferenceEquals(app, null))
                        {
                            rval.Add(app);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ShellLogger.Warning($"AppGrabberService: Unable to enumerate files: {e.Message}");
            }
            finally
            {
                folder?.Dispose();
            }

            return(rval);
        }
Esempio n. 6
0
        private static void Main(string[] args)
        {
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;

            LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NLog.config"));
            LogManager.Configuration.Variables["LogPath"] = storageFolder.Path;

            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

            if (HandleCommandLineArgs())
            {
                // Handles OpenShellCommandInExplorer
                return;
            }

            // Only one instance of the fulltrust process allowed
            // This happens if multiple instances of the UWP app are launched
            using var mutex = new Mutex(true, "FilesUwpFullTrust", out bool isNew);
            if (!isNew)
            {
                return;
            }

            try
            {
                // Create shell COM object and get recycle bin folder
                recycler = new ShellFolder(Shell32.KNOWNFOLDERID.FOLDERID_RecycleBinFolder);
                ApplicationData.Current.LocalSettings.Values["RecycleBin_Title"] = recycler.Name;

                // Create filesystem watcher to monitor recycle bin folder(s)
                // SHChangeNotifyRegister only works if recycle bin is open in explorer :(
                watchers = new List <FileSystemWatcher>();
                var sid = System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();
                foreach (var drive in DriveInfo.GetDrives())
                {
                    var recycle_path = Path.Combine(drive.Name, "$Recycle.Bin", sid);
                    if (!Directory.Exists(recycle_path))
                    {
                        continue;
                    }
                    var watcher = new FileSystemWatcher();
                    watcher.Path         = recycle_path;
                    watcher.Filter       = "*.*";
                    watcher.NotifyFilter = NotifyFilters.LastWrite
                                           | NotifyFilters.FileName
                                           | NotifyFilters.DirectoryName;
                    watcher.Created            += Watcher_Changed;
                    watcher.Deleted            += Watcher_Changed;
                    watcher.EnableRaisingEvents = true;
                    watchers.Add(watcher);
                }

                // Connect to app service and wait until the connection gets closed
                appServiceExit = new AutoResetEvent(false);
                InitializeAppServiceConnection();
                appServiceExit.WaitOne();
            }
            finally
            {
                connection?.Dispose();
                foreach (var watcher in watchers)
                {
                    watcher.Dispose();
                }
                recycler?.Dispose();
                appServiceExit?.Dispose();
                mutex?.ReleaseMutex();
            }
        }
Esempio n. 7
0
        private static void Main(string[] args)
        {
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;

            LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NLog.config"));
            LogManager.Configuration.Variables["LogPath"] = storageFolder.Path;

            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

            if (HandleCommandLineArgs())
            {
                // Handles OpenShellCommandInExplorer
                return;
            }

            // Only one instance of the fulltrust process allowed
            // This happens if multiple instances of the UWP app are launched
            using var mutex = new Mutex(true, "FilesUwpFullTrust", out bool isNew);
            if (!isNew)
            {
                return;
            }

            try
            {
                // Create handle table to store e.g. context menu references
                handleTable = new Win32API.DisposableDictionary();

                // Create shell COM object and get recycle bin folder
                recycler = new ShellFolder(Shell32.KNOWNFOLDERID.FOLDERID_RecycleBinFolder);
                ApplicationData.Current.LocalSettings.Values["RecycleBin_Title"] = recycler.Name;

                // Create filesystem watcher to monitor recycle bin folder(s)
                // SHChangeNotifyRegister only works if recycle bin is open in explorer :(
                binWatchers = new List <FileSystemWatcher>();
                var sid = System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();
                foreach (var drive in DriveInfo.GetDrives())
                {
                    var recycle_path = Path.Combine(drive.Name, "$Recycle.Bin", sid);
                    if (drive.DriveType == DriveType.Network || !Directory.Exists(recycle_path))
                    {
                        continue;
                    }
                    var watcher = new FileSystemWatcher();
                    watcher.Path         = recycle_path;
                    watcher.Filter       = "*.*";
                    watcher.NotifyFilter = NotifyFilters.LastWrite
                                           | NotifyFilters.FileName
                                           | NotifyFilters.DirectoryName;
                    watcher.Created            += Watcher_Changed;
                    watcher.Deleted            += Watcher_Changed;
                    watcher.EnableRaisingEvents = true;
                    binWatchers.Add(watcher);
                }

                // Preload context menu for better performace
                // We query the context menu for the app's local folder
                var preloadPath = ApplicationData.Current.LocalFolder.Path;
                using var _ = Win32API.ContextMenu.GetContextMenuForFiles(new string[] { preloadPath }, Shell32.CMF.CMF_NORMAL | Shell32.CMF.CMF_SYNCCASCADEMENU, FilterMenuItems(false));

                // Create cancellation token for drop window
                cancellation = new CancellationTokenSource();

                // Connect to app service and wait until the connection gets closed
                appServiceExit = new AutoResetEvent(false);
                InitializeAppServiceConnection();

                // Initialize device watcher
                deviceWatcher = new DeviceWatcher(connection);
                deviceWatcher.Start();

                // Wait until the connection gets closed
                appServiceExit.WaitOne();
            }
            finally
            {
                connection?.Dispose();
                foreach (var watcher in binWatchers)
                {
                    watcher.Dispose();
                }
                handleTable?.Dispose();
                recycler?.Dispose();
                deviceWatcher?.Dispose();
                cancellation?.Cancel();
                cancellation?.Dispose();
                appServiceExit?.Dispose();
                mutex?.ReleaseMutex();
            }
        }
Esempio n. 8
0
 private void MainWindow_OnClosing(object sender, CancelEventArgs e)
 {
     folder?.Dispose();
 }