コード例 #1
0
        private static async Task Main()
        {
            Logger = new Logger(logWriter);
            await logWriter.InitializeAsync("debug_fulltrust.log");

            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

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

            try
            {
                // Create message handlers
                messageHandlers = new List <IMessageHandler>
                {
                    new RecycleBinHandler(),
                    new LibrariesHandler(),
                    new FileTagsHandler(),
                    new ApplicationLaunchHandler(),
                    new NetworkDrivesHandler(),
                    new FileOperationsHandler(),
                    new ContextMenuHandler(),
                    new QuickLookHandler(),
                    new Win32MessageHandler(),
                    new InstallOperationsHandler()
                };

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

                // Initialize message handlers
                messageHandlers.ForEach(mh => mh.Initialize(connection));

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

                // Update tags db
                messageHandlers.OfType <FileTagsHandler>().Single().UpdateTagsDb();

                // Wait until the connection gets closed
                appServiceExit.WaitOne();

                // Wait for ongoing file operations
                messageHandlers.OfType <FileOperationsHandler>().Single().WaitForCompletion();
            }
            finally
            {
                messageHandlers.ForEach(mh => mh.Dispose());
                deviceWatcher?.Dispose();
                connection?.Dispose();
                appServiceExit?.Dispose();
                appServiceExit = null;
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: boscorelly/Files
        private static void Main(string[] args)
        {
            Logger = new Logger(logWriter);
            logWriter.InitializeAsync("debug_fulltrust.log").Wait();
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

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

            try
            {
                // Create message handlers
                messageHandlers = new List <IMessageHandler>();
                messageHandlers.Add(new RecycleBinHandler());
                messageHandlers.Add(new LibrariesHandler());
                messageHandlers.Add(new ApplicationLaunchHandler());
                messageHandlers.Add(new NetworkDrivesHandler());
                messageHandlers.Add(new FileOperationsHandler());
                messageHandlers.Add(new ContextMenuHandler());
                messageHandlers.Add(new QuickLookHandler());
                messageHandlers.Add(new Win32MessageHandler());

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

                // Initialize message handlers
                messageHandlers.ForEach(mh => mh.Initialize(connection));

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

                // Wait until the connection gets closed
                appServiceExit.WaitOne();
            }
            finally
            {
                messageHandlers.ForEach(mh => mh.Dispose());
                deviceWatcher?.Dispose();
                connection?.Dispose();
                appServiceExit?.Dispose();
            }
        }
コード例 #3
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();
            }
        }