Example #1
0
        private void OnAppRenamed(object sender, RenamedEventArgs e)
        {
            string oldPath = e.OldFullPath;
            string newPath = e.FullPath;

            string extension = Path.GetExtension(newPath);

            Win32Program.ApplicationType appType = Win32Program.GetAppTypeFromPath(newPath);
            Programs.Win32Program        newApp  = Win32Program.GetAppFromPath(newPath);
            Programs.Win32Program        oldApp  = null;

            // Once the shortcut application is renamed, the old app does not exist and therefore when we try to get the FullPath we get the lnk path instead of the exe path
            // This changes the hashCode() of the old application.
            // Therefore, instead of retrieving the old app using the GetAppFromPath(), we construct the application ourself
            // This situation is not encountered for other application types because the fullPath is the path itself, instead of being computed by using the path to the app.
            try
            {
                if (appType == Win32Program.ApplicationType.ShortcutApplication)
                {
                    oldApp = new Win32Program()
                    {
                        Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = Path.GetFileName(e.OldName), FullPath = newApp.FullPath
                    };
                }
                else if (appType == Win32Program.ApplicationType.InternetShortcutApplication)
                {
                    oldApp = new Win32Program()
                    {
                        Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = Path.GetFileName(e.OldName), FullPath = newApp.FullPath
                    };
                }
                else
                {
                    oldApp = Win32Program.GetAppFromPath(oldPath);
                }
            }
            catch (Exception ex)
            {
                Log.Exception($"OnAppRenamed-{extension} Program|{e.OldName}|Unable to create program from {oldPath}", ex, GetType());
            }

            // To remove the old app which has been renamed and to add the new application.
            if (oldApp != null)
            {
                if (string.IsNullOrWhiteSpace(oldApp.Name) || string.IsNullOrWhiteSpace(oldApp.ExecutableName) || string.IsNullOrWhiteSpace(oldApp.FullPath))
                {
                    Log.Error($"Old app was not initialized properly. OldFullPath: {e.OldFullPath}; OldName: {e.OldName}; FullPath: {e.FullPath}", GetType());
                }
                else
                {
                    Remove(oldApp);
                }
            }

            if (newApp != null)
            {
                Add(newApp);
            }
        }
        public void GetAppTypeFromPathShouldReturnCorrectAppTypeWhenAppPathIsPassedAsArgument(string path, Win32Program.ApplicationType result)
        {
            // Directory.Exists must be mocked
            Win32Program.DirectoryWrapper = GetMockedDirectoryWrapper();

            // Act
            Assert.AreEqual(Win32Program.GetAppTypeFromPath(path), result);
        }