//#act public static string Act(string funcName, string[] funcParamArray) { var returnStr = ""; try { if (funcName.ToLower() == "CreateDirectory".ToLower()) { if (!Directory.Exists(funcParamArray[0])) { Directory.CreateDirectory(funcParamArray[0]); } if (funcParamArray[1] == "true") { var di = new DirectoryInfo(funcParamArray[0]); if (!((di.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)) { di.Attributes = FileAttributes.Hidden; } } } else if (funcName.ToLower() == "OpenDirectory".ToLower()) { if (!Directory.Exists(funcParamArray[0])) { throw new ArgumentException("Directory does not exist! ");; } else { System.Diagnostics.Process.Start(funcParamArray[0]); } } else if (funcName.ToLower() == "SelectAndRenameFile".ToLower()) { var dlgOpenFile = new OpenFileDialog(); dlgOpenFile.Title = funcParamArray[0]; dlgOpenFile.Filter = funcParamArray[1]; dlgOpenFile.InitialDirectory = funcParamArray[2]; dlgOpenFile.Multiselect = false; dlgOpenFile.RestoreDirectory = true; if (dlgOpenFile.ShowDialog() == DialogResult.OK) { if (dlgOpenFile.FileNames.Length > 0) { var firstFilePath = dlgOpenFile.FileNames[0]; var folder = FileHelper.GetFileDetailByOption(firstFilePath, FilePathComposition.Directory); if (File.Exists(firstFilePath)) { File.Move(firstFilePath, folder + "\\" + funcParamArray[3]); } else { throw new ArgumentException("File does not exist! ");; } } } else { //throw new ArgumentException("Operation Cancelled! "); } } else if (funcName.ToLower() == "Run".ToLower()) { var actArgsStr = ""; if (funcParamArray.Length > 1) { actArgsStr = funcParamArray[1]; } ProcessHelper.Run(funcParamArray[0], actArgsStr); } else if (funcName.ToLower() == "OpenFile".ToLower()) { var actArgsStr = ""; ProcessHelper.OpenFile(funcParamArray[0], actArgsStr); } else if (funcName.ToLower() == "OpenFolder".ToLower()) { ProcessHelper.OpenFolder(funcParamArray[0]); } else if (funcName.ToLower() == ("Redirect").ToLower()) { ProcessHelper.Redirect(funcParamArray[0]); } else if (funcName.ToLower() == "SendLocalEmail".ToLower()) { var mailTo = funcParamArray[0]; var subject = funcParamArray[1]; var body = ""; if (funcParamArray.Length > 2) { body = funcParamArray[2]; } if (body.Contains("\r\n")) { body = body.Replace("\r\n", "%0D%0A"); } if (body.Contains("\\r\\n")) { body = body.Replace("\\r\\n", "%0D%0A"); } LocalEmailHelper.Send(mailTo, subject, body); } else if (funcName.ToLower() == ("RunCmd".ToLower()) | funcName.ToLower() == ("ExecCmd".ToLower())) { var actArgsStr = ""; var inputStr = ""; var execCmdModeStr = "0"; if (funcName.ToLower() == ("RunCmd".ToLower())) { if (!File.Exists(funcParamArray[0])) { throw new ArgumentException("File:'" + funcParamArray[0] + "' does not exist!"); } if (funcParamArray.Length > 1) { actArgsStr = funcParamArray[1]; } if (funcParamArray.Length > 2) { if (funcParamArray[2].IsPlusInteger()) { execCmdModeStr = funcParamArray[2]; } } inputStr = actArgsStr.IsNullOrEmpty() ? funcParamArray[0] : funcParamArray[0] + " " + actArgsStr; } else if (funcName.ToLower() == "ExecCmd".ToLower()) { if (funcParamArray.Length > 1) { if (funcParamArray[1].IsPlusInteger()) { execCmdModeStr = funcParamArray[1]; } } inputStr = funcParamArray[0]; } var execCmdModeInt = (ExecCmdMode)Convert.ToInt16(execCmdModeStr); var execCmdMode = (ExecCmdMode)(execCmdModeInt); returnStr = ProcessHelper.Cmd(inputStr, execCmdMode); } else if (funcName.ToLower() == "ExportToExcel".ToLower()) { var content = funcParamArray[0]; content = "UTF格式"; var title = "ExpertToExcel".ToUniqueStringByNow(); var folder = DirectoryHelper.GetSpecialDir("personal"); var path = folder + "\\" + title + ".xls"; File.WriteAllText(path, content, Encoding.Default); ProcessHelper.OpenFile(path, ""); } else if (funcName.ToLower() == "EncryptTextFile".ToLower()) { var path = funcParamArray[0]; path = FileHelper.GetFilePath(path, Path.GetDirectoryName(Application.ExecutablePath)); var folder = FileHelper.GetFileDetailByOption(path, FilePathComposition.Directory); var fileTitle = FileHelper.GetFileDetailByOption(path, FilePathComposition.FileTitle); var postfix = FileHelper.GetFileDetailByOption(path, FilePathComposition.Postfix); var txt = EncryptionHelper.SmEncrypt(File.ReadAllText(path)); var path1 = folder + "\\" + fileTitle + ".E" + postfix.Substring(1, postfix.Length - 1); File.WriteAllText(path1, txt, Encoding.Default); if (funcParamArray.Length > 1 & funcParamArray[1].ToLower() == "true") { File.Delete(path); } } else { return("OutOfScope"); } } catch (Exception ex) { throw new ArgumentException("\n>> " + TypeName + ".Act error: " + ex.Message); } return(returnStr); }
//##RunAsAdmin internal string RunAsAdmin(string funcName, string[] funcParamArray) { var returnStr = ""; try { var isRunAsAdminAccountStatus = RunningParams.RunAsAdminAccountStatus == UniversalStatus.Ok ? true : false; if (!isRunAsAdminAccountStatus) { throw new ArgumentException(" RunAsAdmin Account Status is not OK" + "! "); } var actArgsStr = ""; var inputStr = ""; var runAsAdimAccount = RunningParams.CurrentRunAsAdminAccountName; var runAsAdimAccountPassword = RunningParams.CurrentRunAsAdminAccountPassword; var runAsAdimAccountDomain = RunningParams.CurrentRunAsAdminAccountPassword; bool isCurWinIdIsAdmin = RunningParams.CurrentWinIdAsRunAsAdminAccountStatus == UniversalStatus.Ok ? true : false; if (funcName.ToLower() == "RunAsAdmin".ToLower()) { if (funcParamArray.Length > 1) { actArgsStr = funcParamArray[1]; } if (isCurWinIdIsAdmin) { ProcessHelper.Run(funcParamArray[0], actArgsStr); } else { ProcessHelper.Run(funcParamArray[0], actArgsStr, runAsAdimAccountDomain, runAsAdimAccount, runAsAdimAccountPassword); } } else if (funcName.ToLower() == ("RunCmdAsAdmin".ToLower()) | funcName.ToLower() == ("ExecCmdAsAdmin".ToLower())) { var execCmdModeStr = "0"; if (funcName.ToLower() == ("RunCmdAsAdmin".ToLower())) { if (!File.Exists(funcParamArray[0])) { throw new ArgumentException("File:'" + funcParamArray[0] + "' does not exist!"); } if (funcParamArray.Length > 1) { actArgsStr = funcParamArray[1]; } if (funcParamArray.Length > 2) { if (funcParamArray[2].IsPlusInteger()) { execCmdModeStr = funcParamArray[2]; } } inputStr = actArgsStr.IsNullOrEmpty() ? funcParamArray[0] : funcParamArray[0] + " " + actArgsStr; } else if (funcName.ToLower() == "ExecCmdAsAdmin".ToLower()) { if (funcParamArray.Length > 1) { if (funcParamArray[1].IsPlusInteger()) { execCmdModeStr = funcParamArray[1]; } } inputStr = funcParamArray[0]; } var execCmdModeInt = (ExecCmdMode)Convert.ToInt16(execCmdModeStr); var execCmdMode = (ExecCmdMode)(execCmdModeInt); if (RunningParams.CurrentRunAsAdminAccountName.StartsWith("Current Windows Id:")) { returnStr = ProcessHelper.Cmd(inputStr, execCmdMode); } else { returnStr = ProcessHelper.Cmd(inputStr, execCmdMode, runAsAdimAccountDomain, runAsAdimAccount, runAsAdimAccountPassword); } } else if (funcName.ToLower() == "OpenFileAsAdmin".ToLower()) { if (RunningParams.CurrentRunAsAdminAccountName.StartsWith("Current Windows Id:")) { ProcessHelper.OpenFile(funcParamArray[0], actArgsStr); } else { ProcessHelper.OpenFile(funcParamArray[0], actArgsStr, runAsAdimAccountDomain, runAsAdimAccount, runAsAdimAccountPassword); } } else if (funcName.ToLower() == "OpenFolderAsAdmin".ToLower()) { if (RunningParams.CurrentRunAsAdminAccountName.StartsWith("Current Windows Id:")) { ProcessHelper.OpenFolder(funcParamArray[0]); } else { ProcessHelper.OpenFolder(funcParamArray[0], runAsAdimAccountDomain, runAsAdimAccount, runAsAdimAccountPassword); } } else if (funcName.ToLower() == ("RedirectAsAdmin").ToLower()) { if (RunningParams.CurrentRunAsAdminAccountName.StartsWith("Current Windows Id:")) { ProcessHelper.Redirect(funcParamArray[0]); } else { ProcessHelper.Redirect(funcParamArray[0], runAsAdimAccountDomain, runAsAdimAccount, runAsAdimAccountPassword); } } else { throw new ArgumentException(funcName + " has no param: " + funcParamArray[0] + "! "); } return(returnStr); } catch (Exception ex) { throw new ArgumentException("\n>> " + GetType().FullName + ".RunAsAdmin Error: " + ex.Message); } }