private async static void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) { AppServiceDeferral Deferral = args.GetDeferral(); try { switch (args.Request.Message["ExcuteType"]) { case "Excute_GetHyperlinkInfo": { string ExcutePath = Convert.ToString(args.Request.Message["ExcutePath"]); ValueSet Value = new ValueSet(); if (File.Exists(ExcutePath)) { try { using (ShellLink Link = new ShellLink(ExcutePath)) { Value.Add("Success", string.Empty); Value.Add("TargetPath", Link.TargetPath); Value.Add("Argument", Link.Arguments); Value.Add("RunAs", Link.RunAsAdministrator); } } catch (Exception e) { Value.Add("Error", e.Message); } } else { Value.Add("Error", "File is not exist"); } await args.Request.SendResponseAsync(Value); break; } case "Excute_Intercept_Win_E": { ValueSet Value = new ValueSet(); try { StorageFile InterceptFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Intercept_WIN_E.reg")); StorageFile TempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Intercept_WIN_E_Temp.reg", CreationCollisionOption.ReplaceExisting); using (Stream FileStream = await InterceptFile.OpenStreamForReadAsync().ConfigureAwait(false)) using (StreamReader Reader = new StreamReader(FileStream)) { string Content = await Reader.ReadToEndAsync().ConfigureAwait(false); string LocalApplicationDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); using (Stream TempStream = await TempFile.OpenStreamForWriteAsync()) using (StreamWriter Writer = new StreamWriter(TempStream)) { await Writer.WriteAsync(Content.Replace("%LOCALAPPDATA%", LocalApplicationDataPath.Replace(@"\", @"\\"))); } } using (Process Process = Process.Start(TempFile.Path)) { User32.SetWindowPos(Process.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, User32.SetWindowPosFlags.SWP_NOSIZE | User32.SetWindowPosFlags.SWP_NOMOVE); Process.WaitForExit(); } Value.Add("Success", string.Empty); } catch (Exception e) { Value.Add("Error", e.Message); } await args.Request.SendResponseAsync(Value); break; } case "Excute_Restore_Win_E": { ValueSet Value = new ValueSet(); try { StorageFile RestoreFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Restore_WIN_E.reg")); using (Process Process = Process.Start(RestoreFile.Path)) { User32.SetWindowPos(Process.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, User32.SetWindowPosFlags.SWP_NOSIZE | User32.SetWindowPosFlags.SWP_NOMOVE); Process.WaitForExit(); } Value.Add("Success", string.Empty); } catch (Exception e) { Value.Add("Error", e.Message); } await args.Request.SendResponseAsync(Value); break; } case "Excute_RemoveHiddenAttribute": { ValueSet Value = new ValueSet(); string ExcutePath = Convert.ToString(args.Request.Message["ExcutePath"]); try { if (File.Exists(ExcutePath)) { File.SetAttributes(ExcutePath, File.GetAttributes(ExcutePath) & ~FileAttributes.Hidden); } else if (Directory.Exists(ExcutePath)) { DirectoryInfo Info = new DirectoryInfo(ExcutePath); Info.Attributes &= ~FileAttributes.Hidden; } Value.Add("Success", string.Empty); } catch (Exception e) { Value.Add("Error_RemoveAttributeFailure", e.Message); } await args.Request.SendResponseAsync(Value); break; } case "Excute_RequestClosePipe": { string Guid = Convert.ToString(args.Request.Message["Guid"]); if (PipeServers.ContainsKey(Guid)) { PipeServers[Guid].Disconnect(); PipeServers[Guid].Dispose(); PipeServers.Remove(Guid); } break; } case "Excute_RequestCreateNewPipe": { string Guid = Convert.ToString(args.Request.Message["Guid"]); if (!PipeServers.ContainsKey(Guid)) { InitializeNewNamedPipe(Guid); } break; } case "Identity": { await args.Request.SendResponseAsync(new ValueSet { { "Identity", "FullTrustProcess" } }); break; } case "Excute_Quicklook": { string ExcutePath = Convert.ToString(args.Request.Message["ExcutePath"]); if (!string.IsNullOrEmpty(ExcutePath)) { QuicklookConnector.SendMessageToQuicklook(ExcutePath); } break; } case "Excute_Check_QuicklookIsAvaliable": { bool IsSuccess = QuicklookConnector.CheckQuicklookIsAvaliable(); ValueSet Result = new ValueSet { { "Check_QuicklookIsAvaliable_Result", IsSuccess } }; await args.Request.SendResponseAsync(Result); break; } case "Excute_Get_Associate": { string Path = Convert.ToString(args.Request.Message["ExcutePath"]); string Associate = ExtensionAssociate.GetAssociate(Path); ValueSet Result = new ValueSet { { "Associate_Result", Associate } }; await args.Request.SendResponseAsync(Result); break; } case "Excute_Get_RecycleBinItems": { ValueSet Result = new ValueSet(); string RecycleItemResult = RecycleBinController.GenerateRecycleItemsByJson(); if (string.IsNullOrEmpty(RecycleItemResult)) { Result.Add("Error", "Unknown reason"); } else { Result.Add("RecycleBinItems_Json_Result", RecycleItemResult); } await args.Request.SendResponseAsync(Result); break; } case "Excute_Empty_RecycleBin": { ValueSet Result = new ValueSet(); try { Result.Add("RecycleBinItems_Clear_Result", RecycleBinController.EmptyRecycleBin()); } catch (Exception e) { Result.Add("Error", e.Message); } await args.Request.SendResponseAsync(Result); break; } case "Excute_Restore_RecycleItem": { string Path = Convert.ToString(args.Request.Message["ExcutePath"]); ValueSet Result = new ValueSet { { "Restore_Result", RecycleBinController.Restore(Path) } }; await args.Request.SendResponseAsync(Result); break; } case "Excute_Delete_RecycleItem": { string Path = Convert.ToString(args.Request.Message["ExcutePath"]); ValueSet Result = new ValueSet { { "Delete_Result", RecycleBinController.Delete(Path) } }; await args.Request.SendResponseAsync(Result); break; } case "Excute_EjectUSB": { ValueSet Value = new ValueSet(); string Path = Convert.ToString(args.Request.Message["ExcutePath"]); if (string.IsNullOrEmpty(Path)) { Value.Add("EjectResult", false); } else { Value.Add("EjectResult", USBController.EjectDevice(Path)); } await args.Request.SendResponseAsync(Value); break; } case "Excute_Unlock_Occupy": { ValueSet Value = new ValueSet(); string Path = Convert.ToString(args.Request.Message["ExcutePath"]); if (File.Exists(Path)) { if (StorageItemController.CheckOccupied(Path)) { if (StorageItemController.TryUnoccupied(Path)) { Value.Add("Success", string.Empty); } else { Value.Add("Error_Failure", "Unoccupied failed"); } } else { Value.Add("Error_NotOccupy", "The file is not occupied"); } } else { Value.Add("Error_NotFoundOrNotFile", "Path is not a file"); } await args.Request.SendResponseAsync(Value); break; } case "Excute_Copy": { ValueSet Value = new ValueSet(); string SourcePathJson = Convert.ToString(args.Request.Message["SourcePath"]); string DestinationPath = Convert.ToString(args.Request.Message["DestinationPath"]); string Guid = Convert.ToString(args.Request.Message["Guid"]); bool IsUndo = Convert.ToBoolean(args.Request.Message["Undo"]); List <KeyValuePair <string, string> > SourcePathList = JsonConvert.DeserializeObject <List <KeyValuePair <string, string> > >(SourcePathJson); List <string> OperationRecordList = new List <string>(); if (SourcePathList.All((Item) => Directory.Exists(Item.Key) || File.Exists(Item.Key))) { if (StorageItemController.Copy(SourcePathList, DestinationPath, (s, e) => { lock (Locker) { try { NamedPipeServerStream Server = PipeServers[Guid]; if (!Server.IsConnected) { Server.WaitForConnection(); } using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine(e.ProgressPercentage); } } catch { Debug.WriteLine("无法传输进度数据"); } } }, (se, arg) => { if (arg.Result == HRESULT.S_OK && !IsUndo) { if (arg.DestItem == null || string.IsNullOrEmpty(arg.Name)) { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Copy||{(Directory.Exists(arg.SourceItem.FileSystemPath) ? "Folder" : "File")}||{Path.Combine(arg.DestFolder.FileSystemPath, arg.SourceItem.Name)}"); } else { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Copy||{(Directory.Exists(arg.SourceItem.FileSystemPath) ? "Folder" : "File")}||{Path.Combine(arg.DestFolder.FileSystemPath, arg.Name)}"); } } })) { Value.Add("Success", string.Empty); if (OperationRecordList.Count > 0) { Value.Add("OperationRecord", JsonConvert.SerializeObject(OperationRecordList)); } } else { Value.Add("Error_Failure", "An error occurred while copying the folder"); } } else { Value.Add("Error_NotFound", "SourcePath is not a file or directory"); } if (!Value.ContainsKey("Success")) { lock (Locker) { try { NamedPipeServerStream Server = PipeServers[Guid]; if (!Server.IsConnected) { Server.WaitForConnection(); } using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine("Error_Stop_Signal"); } } catch { Debug.WriteLine("无法传输进度数据"); } } } await args.Request.SendResponseAsync(Value); break; } case "Excute_Move": { ValueSet Value = new ValueSet(); string SourcePathJson = Convert.ToString(args.Request.Message["SourcePath"]); string DestinationPath = Convert.ToString(args.Request.Message["DestinationPath"]); string Guid = Convert.ToString(args.Request.Message["Guid"]); bool IsUndo = Convert.ToBoolean(args.Request.Message["Undo"]); List <KeyValuePair <string, string> > SourcePathList = JsonConvert.DeserializeObject <List <KeyValuePair <string, string> > >(SourcePathJson); List <string> OperationRecordList = new List <string>(); if (SourcePathList.All((Item) => Directory.Exists(Item.Key) || File.Exists(Item.Key))) { if (SourcePathList.Where((Path) => File.Exists(Path.Key)).Any((Item) => StorageItemController.CheckOccupied(Item.Key))) { Value.Add("Error_Capture", "An error occurred while moving the folder"); } else { if (StorageItemController.Move(SourcePathList, DestinationPath, (s, e) => { lock (Locker) { try { NamedPipeServerStream Server = PipeServers[Guid]; if (!Server.IsConnected) { Server.WaitForConnection(); } using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine(e.ProgressPercentage); } } catch { Debug.WriteLine("无法传输进度数据"); } } }, (se, arg) => { if (arg.Result == HRESULT.COPYENGINE_S_DONT_PROCESS_CHILDREN && !IsUndo) { if (arg.DestItem == null || string.IsNullOrEmpty(arg.Name)) { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Move||{(Directory.Exists(arg.SourceItem.FileSystemPath) ? "Folder" : "File")}||{Path.Combine(arg.DestFolder.FileSystemPath, arg.SourceItem.Name)}"); } else { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Move||{(Directory.Exists(arg.SourceItem.FileSystemPath) ? "Folder" : "File")}||{Path.Combine(arg.DestFolder.FileSystemPath, arg.Name)}"); } } })) { Value.Add("Success", string.Empty); if (OperationRecordList.Count > 0) { Value.Add("OperationRecord", JsonConvert.SerializeObject(OperationRecordList)); } } else { Value.Add("Error_Failure", "An error occurred while moving the folder"); } } } else { Value.Add("Error_NotFound", "SourcePath is not a file or directory"); } if (!Value.ContainsKey("Success")) { lock (Locker) { try { NamedPipeServerStream Server = PipeServers[Guid]; if (!Server.IsConnected) { Server.WaitForConnection(); } using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine("Error_Stop_Signal"); } } catch { Debug.WriteLine("无法传输进度数据"); } } } await args.Request.SendResponseAsync(Value); break; } case "Excute_Delete": { ValueSet Value = new ValueSet(); string ExcutePathJson = Convert.ToString(args.Request.Message["ExcutePath"]); string Guid = Convert.ToString(args.Request.Message["Guid"]); bool PermanentDelete = Convert.ToBoolean(args.Request.Message["PermanentDelete"]); bool IsUndo = Convert.ToBoolean(args.Request.Message["Undo"]); List <string> ExcutePathList = JsonConvert.DeserializeObject <List <string> >(ExcutePathJson); List <string> OperationRecordList = new List <string>(); try { if (ExcutePathList.All((Item) => Directory.Exists(Item) || File.Exists(Item))) { if (ExcutePathList.Where((Path) => File.Exists(Path)).Any((Item) => StorageItemController.CheckOccupied(Item))) { Value.Add("Error_Capture", "An error occurred while deleting the folder"); } else { ExcutePathList.Where((Path) => File.Exists(Path)).ToList().ForEach((Item) => { File.SetAttributes(Item, FileAttributes.Normal); }); ExcutePathList.Where((Path) => Directory.Exists(Path)).ToList().ForEach((Item) => { DirectoryInfo Info = new DirectoryInfo(Item); Info.Attributes &= ~FileAttributes.ReadOnly; }); if (StorageItemController.Delete(ExcutePathList, PermanentDelete, (s, e) => { lock (Locker) { try { NamedPipeServerStream Server = PipeServers[Guid]; if (!Server.IsConnected) { Server.WaitForConnection(); } using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine(e.ProgressPercentage); } } catch { Debug.WriteLine("无法传输进度数据"); } } }, (se, arg) => { if (!PermanentDelete && !IsUndo) { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Delete"); } })) { Value.Add("Success", string.Empty); if (OperationRecordList.Count > 0) { Value.Add("OperationRecord", JsonConvert.SerializeObject(OperationRecordList)); } } else { Value.Add("Error_Failure", "The specified file could not be deleted"); } } } else { Value.Add("Error_NotFound", "ExcutePath is not a file or directory"); } } catch { Value.Add("Error_Failure", "The specified file or folder could not be deleted"); } if (!Value.ContainsKey("Success")) { lock (Locker) { try { NamedPipeServerStream Server = PipeServers[Guid]; if (!Server.IsConnected) { Server.WaitForConnection(); } using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine("Error_Stop_Signal"); } } catch { Debug.WriteLine("无法传输进度终止数据"); } } } await args.Request.SendResponseAsync(Value); break; } case "Excute_RunExe": { string ExcutePath = Convert.ToString(args.Request.Message["ExcutePath"]); string ExcuteParameter = Convert.ToString(args.Request.Message["ExcuteParameter"]); string ExcuteAuthority = Convert.ToString(args.Request.Message["ExcuteAuthority"]); if (!string.IsNullOrEmpty(ExcutePath)) { if (string.IsNullOrEmpty(ExcuteParameter)) { using (Process Process = new Process()) { Process.StartInfo.FileName = ExcutePath; if (ExcuteAuthority == "Administrator") { Process.StartInfo.Verb = "runAs"; } Process.Start(); User32.SetWindowPos(Process.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, User32.SetWindowPosFlags.SWP_NOSIZE | User32.SetWindowPosFlags.SWP_NOMOVE); } } else { if (SpecialStringMap.Contains(ExcutePath)) { using (Process Process = new Process()) { Process.StartInfo.FileName = ExcutePath; Process.StartInfo.Arguments = ExcuteParameter; if (ExcuteAuthority == "Administrator") { Process.StartInfo.Verb = "runAs"; } Process.Start(); User32.SetWindowPos(Process.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, User32.SetWindowPosFlags.SWP_NOSIZE | User32.SetWindowPosFlags.SWP_NOMOVE); } } else { using (Process Process = new Process()) { Process.StartInfo.FileName = ExcutePath; Process.StartInfo.Arguments = $"\"{ExcuteParameter}\""; if (ExcuteAuthority == "Administrator") { Process.StartInfo.Verb = "runAs"; } Process.Start(); User32.SetWindowPos(Process.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, User32.SetWindowPosFlags.SWP_NOSIZE | User32.SetWindowPosFlags.SWP_NOMOVE); } } } } break; } case "Excute_Test_Connection": { await args.Request.SendResponseAsync(new ValueSet { { "Excute_Test_Connection", string.Empty } }); break; } case "Excute_Exit": { ExitLocker.Set(); break; } } } catch { ValueSet Value = new ValueSet { { "Error", "An exception occurred while processing the instruction" } }; await args.Request.SendResponseAsync(Value); } finally { Deferral.Complete(); } }
private async static void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) { AppServiceDeferral Deferral = args.GetDeferral(); try { switch (args.Request.Message["ExecuteType"]) { case "Execute_GetContextMenuItems": { IEnumerable <string> ExecutePath = JsonConvert.DeserializeObject <string[]>(Convert.ToString(args.Request.Message["ExecutePath"])); bool IncludeExtensionItem = Convert.ToBoolean(args.Request.Message["IncludeExtensionItem"]); List <(string, string, string)> ContextMenuItems = ContextMenu.FetchContextMenuItems(ExecutePath, IncludeExtensionItem); ValueSet Value = new ValueSet { { "Success", JsonConvert.SerializeObject(ContextMenuItems) } }; await args.Request.SendResponseAsync(Value); break; } case "Execute_InvokeContextMenuItem": { string Verb = Convert.ToString(args.Request.Message["InvokeVerb"]); string[] Paths = JsonConvert.DeserializeObject <string[]>(Convert.ToString(args.Request.Message["ExecutePath"])); ValueSet Value = new ValueSet(); if (!string.IsNullOrEmpty(Verb) && Paths.Length > 0) { if (ContextMenu.InvokeVerb(Paths, Verb)) { Value.Add("Success", string.Empty); } else { Value.Add("Error", $"Execute Verb: {Verb} failed"); } } else { Value.Add("Error", "Verb is empty or Paths is empty"); } await args.Request.SendResponseAsync(Value); break; } case "Execute_ElevateAsAdmin": { Connection?.Dispose(); Connection = null; using (Process AdminProcess = new Process()) { AdminProcess.StartInfo.Verb = "runas"; AdminProcess.StartInfo.UseShellExecute = true; AdminProcess.StartInfo.Arguments = $"Elevation_Restart {ExplorerProcess?.Id}"; AdminProcess.StartInfo.FileName = Process.GetCurrentProcess().MainModule.FileName; AdminProcess.Start(); } ExitLocker.Set(); break; } case "Execute_CreateLink": { string LinkPath = Convert.ToString(args.Request.Message["LinkPath"]); string LinkTarget = Convert.ToString(args.Request.Message["LinkTarget"]); string LinkDesc = Convert.ToString(args.Request.Message["LinkDesc"]); string LinkArgument = Convert.ToString(args.Request.Message["LinkArgument"]); ShellLink.Create(LinkPath, LinkTarget, description: LinkDesc, arguments: LinkArgument).Dispose(); ValueSet Value = new ValueSet { { "Success", string.Empty } }; await args.Request.SendResponseAsync(Value); break; } case "Execute_GetVariable_Path": { ValueSet Value = new ValueSet(); string Variable = Convert.ToString(args.Request.Message["Variable"]); string Env = Environment.GetEnvironmentVariable(Variable); if (string.IsNullOrEmpty(Env)) { Value.Add("Error", "Could not found EnvironmentVariable"); } else { Value.Add("Success", Env); } await args.Request.SendResponseAsync(Value); break; } case "Execute_Rename": { string ExecutePath = Convert.ToString(args.Request.Message["ExecutePath"]); string DesireName = Convert.ToString(args.Request.Message["DesireName"]); ValueSet Value = new ValueSet(); if (File.Exists(ExecutePath) || Directory.Exists(ExecutePath)) { if (StorageItemController.CheckOccupied(ExecutePath)) { Value.Add("Error_Occupied", "FileLoadException"); } else { if (StorageItemController.CheckPermission(FileSystemRights.Modify, Path.GetDirectoryName(ExecutePath))) { if (StorageItemController.Rename(ExecutePath, DesireName)) { Value.Add("Success", string.Empty); } else { Value.Add("Error_Failure", "Error happened when rename"); } } else { Value.Add("Error_Failure", "No Modify Permission"); } } } else { Value.Add("Error_NotFound", "FileNotFoundException"); } await args.Request.SendResponseAsync(Value); break; } case "Execute_GetHyperlinkInfo": { string ExecutePath = Convert.ToString(args.Request.Message["ExecutePath"]); ValueSet Value = new ValueSet(); if (File.Exists(ExecutePath)) { using (ShellLink Link = new ShellLink(ExecutePath)) { Value.Add("Success", string.Empty); Value.Add("TargetPath", Link.TargetPath); Value.Add("Argument", Link.Arguments); Value.Add("RunAs", Link.RunAsAdministrator); Value.Add("IsFile", File.Exists(Link.TargetPath)); } } else { Value.Add("Error", "File is not exist"); } await args.Request.SendResponseAsync(Value); break; } case "Execute_Intercept_Win_E": { ValueSet Value = new ValueSet(); string[] EnvironmentVariables = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.User).Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); if (EnvironmentVariables.Where((Var) => Var.Contains("WindowsApps")).Select((Var) => Path.Combine(Var, "RX-Explorer.exe")).FirstOrDefault((Path) => File.Exists(Path)) is string AliasLocation) { StorageFile InterceptFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Intercept_WIN_E.reg")); StorageFile TempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Intercept_WIN_E_Temp.reg", CreationCollisionOption.ReplaceExisting); using (Stream FileStream = await InterceptFile.OpenStreamForReadAsync().ConfigureAwait(true)) using (StreamReader Reader = new StreamReader(FileStream)) { string Content = await Reader.ReadToEndAsync().ConfigureAwait(true); using (Stream TempStream = await TempFile.OpenStreamForWriteAsync()) using (StreamWriter Writer = new StreamWriter(TempStream, Encoding.Unicode)) { await Writer.WriteAsync(Content.Replace("<FillActualAliasPathInHere>", $"{AliasLocation.Replace(@"\", @"\\")} %1")); } } using (Process Process = Process.Start(TempFile.Path)) { SetWindowsZPosition(Process); Process.WaitForExit(); } Value.Add("Success", string.Empty); } else { Value.Add("Error", "Alias file is not exists"); } await args.Request.SendResponseAsync(Value); break; } case "Execute_Restore_Win_E": { StorageFile RestoreFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Restore_WIN_E.reg")); using (Process Process = Process.Start(RestoreFile.Path)) { SetWindowsZPosition(Process); Process.WaitForExit(); } ValueSet Value = new ValueSet { { "Success", string.Empty } }; await args.Request.SendResponseAsync(Value); break; } case "Execute_RemoveHiddenAttribute": { string ExecutePath = Convert.ToString(args.Request.Message["ExecutePath"]); if (File.Exists(ExecutePath)) { File.SetAttributes(ExecutePath, File.GetAttributes(ExecutePath) & ~FileAttributes.Hidden); } else if (Directory.Exists(ExecutePath)) { DirectoryInfo Info = new DirectoryInfo(ExecutePath); Info.Attributes &= ~FileAttributes.Hidden; } ValueSet Value = new ValueSet { { "Success", string.Empty } }; await args.Request.SendResponseAsync(Value); break; } case "Execute_RequestCreateNewPipe": { string Guid = Convert.ToString(args.Request.Message["Guid"]); if (!PipeServers.ContainsKey(Guid)) { NamedPipeServerStream NewPipeServer = new NamedPipeServerStream($@"Explorer_And_FullTrustProcess_NamedPipe-{Guid}", PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 2048, 2048, null, HandleInheritability.None, PipeAccessRights.ChangePermissions); PipeSecurity Security = NewPipeServer.GetAccessControl(); PipeAccessRule ClientRule = new PipeAccessRule(new SecurityIdentifier("S-1-15-2-1"), PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance, AccessControlType.Allow); PipeAccessRule OwnerRule = new PipeAccessRule(WindowsIdentity.GetCurrent().Owner, PipeAccessRights.FullControl, AccessControlType.Allow); Security.AddAccessRule(ClientRule); Security.AddAccessRule(OwnerRule); NewPipeServer.SetAccessControl(Security); PipeServers.Add(Guid, NewPipeServer); _ = NewPipeServer.WaitForConnectionAsync(new CancellationTokenSource(3000).Token).ContinueWith((task) => { if (PipeServers.TryGetValue(Guid, out NamedPipeServerStream Pipe)) { Pipe.Dispose(); PipeServers.Remove(Guid); } }, TaskContinuationOptions.OnlyOnCanceled); } break; } case "Identity": { ValueSet Value = new ValueSet { { "Identity", "FullTrustProcess" } }; if (ExplorerProcess != null) { Value.Add("PreviousExplorerId", ExplorerProcess.Id); } await args.Request.SendResponseAsync(Value); break; } case "Execute_Quicklook": { string ExecutePath = Convert.ToString(args.Request.Message["ExecutePath"]); if (!string.IsNullOrEmpty(ExecutePath)) { QuicklookConnector.SendMessageToQuicklook(ExecutePath); } break; } case "Execute_Check_QuicklookIsAvaliable": { bool IsSuccess = QuicklookConnector.CheckQuicklookIsAvaliable(); ValueSet Result = new ValueSet { { "Check_QuicklookIsAvaliable_Result", IsSuccess } }; await args.Request.SendResponseAsync(Result); break; } case "Execute_Get_Associate": { string Path = Convert.ToString(args.Request.Message["ExecutePath"]); string Associate = ExtensionAssociate.GetAssociate(Path); ValueSet Result = new ValueSet { { "Associate_Result", Associate } }; await args.Request.SendResponseAsync(Result); break; } case "Execute_Get_RecycleBinItems": { ValueSet Result = new ValueSet(); string RecycleItemResult = RecycleBinController.GenerateRecycleItemsByJson(); if (string.IsNullOrEmpty(RecycleItemResult)) { Result.Add("Error", "Could not get recycle items"); } else { Result.Add("RecycleBinItems_Json_Result", RecycleItemResult); } await args.Request.SendResponseAsync(Result); break; } case "Execute_Empty_RecycleBin": { ValueSet Result = new ValueSet { { "RecycleBinItems_Clear_Result", RecycleBinController.EmptyRecycleBin() } }; await args.Request.SendResponseAsync(Result); break; } case "Execute_Restore_RecycleItem": { string Path = Convert.ToString(args.Request.Message["ExecutePath"]); ValueSet Result = new ValueSet { { "Restore_Result", RecycleBinController.Restore(Path) } }; await args.Request.SendResponseAsync(Result); break; } case "Execute_Delete_RecycleItem": { string Path = Convert.ToString(args.Request.Message["ExecutePath"]); ValueSet Result = new ValueSet { { "Delete_Result", RecycleBinController.Delete(Path) } }; await args.Request.SendResponseAsync(Result); break; } case "Execute_EjectUSB": { ValueSet Value = new ValueSet(); string Path = Convert.ToString(args.Request.Message["ExecutePath"]); if (string.IsNullOrEmpty(Path)) { Value.Add("EjectResult", false); } else { Value.Add("EjectResult", USBController.EjectDevice(Path)); } await args.Request.SendResponseAsync(Value); break; } case "Execute_Unlock_Occupy": { ValueSet Value = new ValueSet(); string Path = Convert.ToString(args.Request.Message["ExecutePath"]); if (File.Exists(Path)) { if (StorageItemController.CheckOccupied(Path)) { if (StorageItemController.TryUnoccupied(Path)) { Value.Add("Success", string.Empty); } else { Value.Add("Error_Failure", "Unoccupied failed"); } } else { Value.Add("Error_NotOccupy", "The file is not occupied"); } } else { Value.Add("Error_NotFoundOrNotFile", "Path is not a file"); } await args.Request.SendResponseAsync(Value); break; } case "Execute_Copy": { ValueSet Value = new ValueSet(); string SourcePathJson = Convert.ToString(args.Request.Message["SourcePath"]); string DestinationPath = Convert.ToString(args.Request.Message["DestinationPath"]); string Guid = Convert.ToString(args.Request.Message["Guid"]); bool IsUndo = Convert.ToBoolean(args.Request.Message["Undo"]); List <KeyValuePair <string, string> > SourcePathList = JsonConvert.DeserializeObject <List <KeyValuePair <string, string> > >(SourcePathJson); List <string> OperationRecordList = new List <string>(); int Progress = 0; if (SourcePathList.All((Item) => Directory.Exists(Item.Key) || File.Exists(Item.Key))) { if (StorageItemController.CheckPermission(FileSystemRights.Modify, DestinationPath)) { if (StorageItemController.Copy(SourcePathList, DestinationPath, (s, e) => { lock (Locker) { try { Progress = e.ProgressPercentage; if (PipeServers.TryGetValue(Guid, out NamedPipeServerStream Pipeline)) { using (StreamWriter Writer = new StreamWriter(Pipeline, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine(e.ProgressPercentage); } } } catch { Debug.WriteLine("Could not send progress data"); } } }, (se, arg) => { if (arg.Result == HRESULT.S_OK && !IsUndo) { if (arg.DestItem == null || string.IsNullOrEmpty(arg.Name)) { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Copy||{(Directory.Exists(arg.SourceItem.FileSystemPath) ? "Folder" : "File")}||{Path.Combine(arg.DestFolder.FileSystemPath, arg.SourceItem.Name)}"); } else { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Copy||{(Directory.Exists(arg.SourceItem.FileSystemPath) ? "Folder" : "File")}||{Path.Combine(arg.DestFolder.FileSystemPath, arg.Name)}"); } } })) { Value.Add("Success", string.Empty); if (OperationRecordList.Count > 0) { Value.Add("OperationRecord", JsonConvert.SerializeObject(OperationRecordList)); } } else { Value.Add("Error_Failure", "An error occurred while copying the folder"); } } else { Value.Add("Error_Failure", "An error occurred while copying the folder"); } } else { Value.Add("Error_NotFound", "SourcePath is not a file or directory"); } if (Progress < 100) { try { if (PipeServers.TryGetValue(Guid, out NamedPipeServerStream Pipeline)) { using (StreamWriter Writer = new StreamWriter(Pipeline, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine("Error_Stop_Signal"); } } } catch { Debug.WriteLine("Could not send stop signal"); } } await args.Request.SendResponseAsync(Value); break; } case "Execute_Move": { ValueSet Value = new ValueSet(); string SourcePathJson = Convert.ToString(args.Request.Message["SourcePath"]); string DestinationPath = Convert.ToString(args.Request.Message["DestinationPath"]); string Guid = Convert.ToString(args.Request.Message["Guid"]); bool IsUndo = Convert.ToBoolean(args.Request.Message["Undo"]); List <KeyValuePair <string, string> > SourcePathList = JsonConvert.DeserializeObject <List <KeyValuePair <string, string> > >(SourcePathJson); List <string> OperationRecordList = new List <string>(); int Progress = 0; if (SourcePathList.All((Item) => Directory.Exists(Item.Key) || File.Exists(Item.Key))) { if (SourcePathList.Any((Item) => StorageItemController.CheckOccupied(Item.Key))) { Value.Add("Error_Capture", "An error occurred while moving the folder"); } else { if (StorageItemController.CheckPermission(FileSystemRights.Modify, DestinationPath)) { if (StorageItemController.Move(SourcePathList, DestinationPath, (s, e) => { lock (Locker) { try { Progress = e.ProgressPercentage; if (PipeServers.TryGetValue(Guid, out NamedPipeServerStream Pipeline)) { using (StreamWriter Writer = new StreamWriter(Pipeline, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine(e.ProgressPercentage); } } } catch { Debug.WriteLine("Could not send progress data"); } } }, (se, arg) => { if (arg.Result == HRESULT.COPYENGINE_S_DONT_PROCESS_CHILDREN && !IsUndo) { if (arg.DestItem == null || string.IsNullOrEmpty(arg.Name)) { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Move||{(Directory.Exists(arg.SourceItem.FileSystemPath) ? "Folder" : "File")}||{Path.Combine(arg.DestFolder.FileSystemPath, arg.SourceItem.Name)}"); } else { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Move||{(Directory.Exists(arg.SourceItem.FileSystemPath) ? "Folder" : "File")}||{Path.Combine(arg.DestFolder.FileSystemPath, arg.Name)}"); } } })) { Value.Add("Success", string.Empty); if (OperationRecordList.Count > 0) { Value.Add("OperationRecord", JsonConvert.SerializeObject(OperationRecordList)); } } else { Value.Add("Error_Failure", "An error occurred while moving the folder"); } } else { Value.Add("Error_Failure", "An error occurred while moving the folder"); } } } else { Value.Add("Error_NotFound", "SourcePath is not a file or directory"); } if (Progress < 100) { try { if (PipeServers.TryGetValue(Guid, out NamedPipeServerStream Pipeline)) { using (StreamWriter Writer = new StreamWriter(Pipeline, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine("Error_Stop_Signal"); } } } catch { Debug.WriteLine("Could not send progress data"); } } await args.Request.SendResponseAsync(Value); break; } case "Execute_Delete": { ValueSet Value = new ValueSet(); string ExecutePathJson = Convert.ToString(args.Request.Message["ExecutePath"]); string Guid = Convert.ToString(args.Request.Message["Guid"]); bool PermanentDelete = Convert.ToBoolean(args.Request.Message["PermanentDelete"]); bool IsUndo = Convert.ToBoolean(args.Request.Message["Undo"]); List <string> ExecutePathList = JsonConvert.DeserializeObject <List <string> >(ExecutePathJson); List <string> OperationRecordList = new List <string>(); int Progress = 0; try { if (ExecutePathList.All((Item) => Directory.Exists(Item) || File.Exists(Item))) { if (ExecutePathList.Any((Item) => StorageItemController.CheckOccupied(Item))) { Value.Add("Error_Capture", "An error occurred while deleting the folder"); } else { if (ExecutePathList.All((Path) => (Directory.Exists(Path) || File.Exists(Path)) && StorageItemController.CheckPermission(FileSystemRights.Modify, System.IO.Path.GetDirectoryName(Path)))) { if (StorageItemController.Delete(ExecutePathList, PermanentDelete, (s, e) => { lock (Locker) { try { Progress = e.ProgressPercentage; if (PipeServers.TryGetValue(Guid, out NamedPipeServerStream Pipeline)) { using (StreamWriter Writer = new StreamWriter(Pipeline, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine(e.ProgressPercentage); } } } catch { Debug.WriteLine("Could not send progress data"); } } }, (se, arg) => { if (!PermanentDelete && !IsUndo) { OperationRecordList.Add($"{arg.SourceItem.FileSystemPath}||Delete"); } })) { Value.Add("Success", string.Empty); if (OperationRecordList.Count > 0) { Value.Add("OperationRecord", JsonConvert.SerializeObject(OperationRecordList)); } } else { Value.Add("Error_Failure", "The specified file could not be deleted"); } } else { Value.Add("Error_Failure", "The specified file could not be deleted"); } } } else { Value.Add("Error_NotFound", "ExecutePath is not a file or directory"); } } catch { Value.Add("Error_Failure", "The specified file or folder could not be deleted"); } if (Progress < 100) { try { if (PipeServers.TryGetValue(Guid, out NamedPipeServerStream Pipeline)) { using (StreamWriter Writer = new StreamWriter(Pipeline, new UTF8Encoding(false), 1024, true)) { Writer.WriteLine("Error_Stop_Signal"); } } } catch { Debug.WriteLine("Could not send stop signal"); } } await args.Request.SendResponseAsync(Value); break; } case "Execute_RunExe": { string ExecutePath = Convert.ToString(args.Request.Message["ExecutePath"]); string ExecuteParameter = Convert.ToString(args.Request.Message["ExecuteParameter"]); string ExecuteAuthority = Convert.ToString(args.Request.Message["ExecuteAuthority"]); bool ExecuteCreateNoWindow = Convert.ToBoolean(args.Request.Message["ExecuteCreateNoWindow"]); ValueSet Value = new ValueSet(); if (!string.IsNullOrEmpty(ExecutePath)) { if (StorageItemController.CheckPermission(FileSystemRights.ReadAndExecute, ExecutePath)) { if (string.IsNullOrEmpty(ExecuteParameter)) { using (Process Process = new Process()) { Process.StartInfo.FileName = ExecutePath; Process.StartInfo.UseShellExecute = true; Process.StartInfo.CreateNoWindow = ExecuteCreateNoWindow; Process.StartInfo.WorkingDirectory = Path.GetDirectoryName(ExecutePath); if (ExecuteAuthority == "Administrator") { Process.StartInfo.Verb = "runAs"; } Process.Start(); SetWindowsZPosition(Process); } } else { using (Process Process = new Process()) { Process.StartInfo.FileName = ExecutePath; Process.StartInfo.Arguments = ExecuteParameter; Process.StartInfo.UseShellExecute = true; Process.StartInfo.CreateNoWindow = ExecuteCreateNoWindow; Process.StartInfo.WorkingDirectory = Path.GetDirectoryName(ExecutePath); if (ExecuteAuthority == "Administrator") { Process.StartInfo.Verb = "runAs"; } Process.Start(); SetWindowsZPosition(Process); } } Value.Add("Success", string.Empty); } else { Value.Add("Error_Failure", "The specified file could not be executed"); } } else { Value.Add("Success", string.Empty); } await args.Request.SendResponseAsync(Value); break; } case "Execute_Test_Connection": { try { if (args.Request.Message.TryGetValue("ProcessId", out object Obj) && Obj is int Id && ExplorerProcess?.Id != Id) { ExplorerProcess?.Dispose(); ExplorerProcess = Process.GetProcessById(Id); } } catch { Debug.WriteLine("GetProcess from id and register Exit event failed"); } await args.Request.SendResponseAsync(new ValueSet { { "Execute_Test_Connection", string.Empty } }); break; } case "Execute_Exit": { ExitLocker.Set(); break; } } } catch (Exception ex) { ValueSet Value = new ValueSet { { "Error", ex.Message } }; await args.Request.SendResponseAsync(Value); } finally { Deferral.Complete(); } }
private async static void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) { var Deferral = args.GetDeferral(); try { switch (args.Request.Message["ExcuteType"]) { case "Excute_Quicklook": { string ExcutePath = Convert.ToString(args.Request.Message["ExcutePath"]); if (!string.IsNullOrEmpty(ExcutePath)) { await QuicklookConnector.SendMessageToQuicklook(ExcutePath); } break; } case "Excute_Check_QuicklookIsAvaliable": { bool IsSuccess = await QuicklookConnector.CheckQuicklookIsAvaliable(); ValueSet Result = new ValueSet { { "Check_QuicklookIsAvaliable_Result", IsSuccess } }; await args.Request.SendResponseAsync(Result); break; } case "Excute_Get_Associate": { string Path = Convert.ToString(args.Request.Message["AssociatePath"]); string Associate = ExtensionAssociate.GetAssociate(Path); ValueSet Result = new ValueSet { { "Associate_Result", string.IsNullOrEmpty(Associate)?"<Empty>":Associate } }; await args.Request.SendResponseAsync(Result); break; } case "Excute_Get_RecycleBinItems": { ValueSet Result = new ValueSet { { "RecycleBinItems_Json_Result", RecycleBinController.GenerateRecycleItemsByJson() } }; await args.Request.SendResponseAsync(Result); break; } case "Excute_Empty_RecycleBin": { RecycleBinController.EmptyRecycleBin(); break; } case "Excute_Copy": { ValueSet Value = new ValueSet(); string SourcePath = Convert.ToString(args.Request.Message["SourcePath"]); string DestinationPath = Convert.ToString(args.Request.Message["DestinationPath"]); if (Directory.Exists(SourcePath)) { if (StorageItemOperator.CopyFolder(SourcePath, DestinationPath)) { Value.Add("Success", string.Empty); } else { Value.Add("Error_Failure", "An error occurred while copying the folder"); } } else if (File.Exists(SourcePath)) { if (StorageItemOperator.CopyFile(SourcePath, DestinationPath)) { Value.Add("Success", string.Empty); } else { Value.Add("Error_Failure", "An error occurred while copying the file"); } } else { Value.Add("Error_NoExist", "SourcePath is not a file or directory"); } await args.Request.SendResponseAsync(Value); break; } case "Excute_Move": { ValueSet Value = new ValueSet(); string SourcePath = Convert.ToString(args.Request.Message["SourcePath"]); string DestinationPath = Convert.ToString(args.Request.Message["DestinationPath"]); if (Directory.Exists(SourcePath)) { if (StorageItemOperator.MoveFolder(SourcePath, DestinationPath)) { Value.Add("Success", string.Empty); } else { Value.Add("Error_Failure", "An error occurred while moving the folder"); } } else if (File.Exists(SourcePath)) { if (StorageItemOperator.MoveFile(SourcePath, DestinationPath)) { Value.Add("Success", string.Empty); } else { Value.Add("Error_Failure", "An error occurred while moving the file"); } } else { Value.Add("Error_NoExist", "SourcePath is not a file or directory"); } await args.Request.SendResponseAsync(Value); break; } case "Excute_Delete": { ValueSet Value = new ValueSet(); string ExcutePath = Convert.ToString(args.Request.Message["ExcutePath"]); try { if (File.Exists(ExcutePath)) { File.SetAttributes(ExcutePath, FileAttributes.Normal); if (StorageItemOperator.TryUnoccupied(ExcutePath)) { File.Delete(ExcutePath); Value.Add("Success", string.Empty); } else { Value.Add("Error_Failure", "The specified file or folder could not be deleted"); } } else if (Directory.Exists(ExcutePath)) { DirectoryInfo Info = new DirectoryInfo(ExcutePath) { Attributes = FileAttributes.Normal & FileAttributes.Directory }; Info.Delete(true); Value.Add("Success", string.Empty); } else { Value.Add("Error_NoExist", "ExcutePath is not a file or directory"); } } catch { Value.Add("Error_Failure", "The specified file or folder could not be deleted"); } await args.Request.SendResponseAsync(Value); break; } case "Excute_RunExe": { string ExcutePath = Convert.ToString(args.Request.Message["ExcutePath"]); string ExcuteParameter = Convert.ToString(args.Request.Message["ExcuteParameter"]); string ExcuteAuthority = Convert.ToString(args.Request.Message["ExcuteAuthority"]); if (!string.IsNullOrEmpty(ExcutePath)) { if (string.IsNullOrEmpty(ExcuteParameter)) { if (ExcuteAuthority == "Administrator") { ProcessStartInfo Info = new ProcessStartInfo(ExcutePath) { Verb = "runAs" }; Process.Start(Info).Dispose(); } else { Process.Start(ExcutePath).Dispose(); } } else { if (SpecialStringMap.Contains(ExcutePath)) { if (ExcuteAuthority == "Administrator") { ProcessStartInfo Info = new ProcessStartInfo(ExcutePath, ExcuteParameter) { Verb = "runAs" }; Process.Start(Info).Dispose(); } else { Process.Start(ExcutePath, ExcuteParameter).Dispose(); } } else { if (ExcuteAuthority == "Administrator") { ProcessStartInfo Info = new ProcessStartInfo(ExcutePath, $"\"{ExcuteParameter}\"") { Verb = "runAs" }; Process.Start(Info).Dispose(); } else { Process.Start(ExcutePath, $"\"{ExcuteParameter}\"").Dispose(); } } } } break; } case "Excute_Exit": { ExitLocker.Set(); break; } } } catch { ValueSet Value = new ValueSet { { "Error", "An exception occurred while processing the instruction" } }; await args.Request.SendResponseAsync(Value); } finally { Deferral.Complete(); } }