Beispiel #1
0
        /// <summary>
        /// 写入所有程序配置
        /// </summary>
        /// <param name="configKeyAndValue"></param>
        public void WriteAllAppConfigs(Dictionary <string, string> configKeyAndValue)
        {
            //文件不存在,则返回
            if (FileAndDirectoryHelper.FileIsNotExists(filePath))
            {
                MessageBoxHelper.ShowErrorMessageBox(string.Format("配置文件不存在:{0}", filePath));
                return;
            }
            var         xmlDoc           = XmlFileClass.LoadXmlFile(filePath);
            XmlNodeList appSettingsNodes = xmlDoc.GetElementsByTagName(AppSettings);

            if (appSettingsNodes.Count < 1)
            {
                return;
            }

            foreach (var configs in configKeyAndValue)
            {
                string      nodeName    = configs.Key;
                XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName(nodeName);
                string      innerText   = configs.Value;
                if (xmlNodeList.Count > 0)
                {
                    xmlNodeList[0].InnerText = innerText;
                }
                else
                {
                    //创建新的配置
                    XmlElement newNode = xmlDoc.CreateElement(nodeName);
                    newNode.InnerText = innerText;
                    appSettingsNodes[0].AppendChild(newNode);
                }
            }
            xmlDoc.Save(filePath);
        }
        /// <summary>
        /// 获取所有命令配置
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Dictionary <string, string> ReadCommandConfigFile(string path)
        {
            Dictionary <string, string> configKeyAndValue = new Dictionary <string, string>();

            if (FileAndDirectoryHelper.FileIsNotExists(path))
            {
                return(configKeyAndValue);
            }

            var         xmlDoc      = XmlFileLoader.LoadXmlFile(path);
            XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName(CommandInfo);

            for (int i = 0; i < xmlNodeList.Count; i++)
            {
                XmlAttributeCollection attributes = xmlNodeList[i].Attributes;
                if (attributes == null)
                {
                    continue;
                }

                XmlAttribute currentKey   = attributes[ShortName];
                XmlAttribute currentValue = attributes[Command];
                if (currentKey != null && currentValue != null)
                {
                    configKeyAndValue[currentKey.InnerText] = currentValue.InnerText;
                }
            }

            return(configKeyAndValue);
        }
Beispiel #3
0
        /// <summary>
        /// 获取所有程序配置
        /// </summary>
        /// <returns></returns>
        public Dictionary <string, string> ReadAllAppConfigs()
        {
            Dictionary <string, string> configKeyAndValue = new Dictionary <string, string>();

            //文件不存在,或节点为空,则返回空字典
            if (FileAndDirectoryHelper.FileIsNotExists(filePath))
            {
                return(configKeyAndValue);
            }

            var         xmlDoc           = XmlFileClass.LoadXmlFile(filePath);
            XmlNodeList appSettingsNodes = xmlDoc.GetElementsByTagName(AppSettings);

            if (appSettingsNodes.Count < 1)
            {
                return(configKeyAndValue);
            }

            foreach (XmlNode childNode in appSettingsNodes[0].ChildNodes)
            {
                if (childNode == null)
                {
                    continue;
                }
                configKeyAndValue[childNode.Name] = childNode.InnerText;
            }

            return(configKeyAndValue);
        }
Beispiel #4
0
        /// <summary>
        /// 获取命令
        /// </summary>
        /// <param name="originalShortName">简称</param>
        /// <returns></returns>
        private string GetCommand(string originalShortName)
        {
            string command = GetCommandFormSetting(originalShortName);

            //对应的命令不存在
            if (string.IsNullOrEmpty(command))
            {
                return(CommandNotInConfig(originalShortName));
            }

            //是目录或文件路径
            bool isDirectoryOrFilePath = FileAndDirectoryHelper.IsDirectoryOrFilePath(command);

            if (isDirectoryOrFilePath && FileAndDirectoryHelper.PathIsNotExists(command))
            {
                MessageBoxHelper.ShowErrorMessageBox($@"对应路径不存在:{command}");
                return(string.Empty);
            }

            if (isDirectoryOrFilePath || IsWellFormedUriString(command))
            {
                return(ConvertCommandWithQuote(command));
            }

            return(SimpleCommand(command));
        }
        /// <summary>
        /// 更新命令信息
        /// </summary>
        /// <param name="configKeyAndValue"></param>
        /// <param name="xmlDoc"></param>
        private void UpdateCommandInfos(Dictionary <string, string> configKeyAndValue, XmlDocument xmlDoc)
        {
            bool isAddSuccessful = TryAddCommandAndPathInfosNode(xmlDoc);

            if (!isAddSuccessful)
            {
                return;
            }

            XmlNode commandInfosNode = xmlDoc.GetElementsByTagName(CommandInfos)[0];
            XmlNode pathInfosNode    = xmlDoc.GetElementsByTagName(PathInfos)[0];

            foreach (var configs in configKeyAndValue)
            {
                string     commandValue = configs.Value;
                XmlElement newNode      = xmlDoc.CreateElement(CommandInfo);
                newNode.SetAttribute(Command, commandValue);
                newNode.SetAttribute(ShortName, configs.Key);
                //添加到路径信息
                if (FileAndDirectoryHelper.PathIsExists(commandValue))
                {
                    pathInfosNode.AppendChild(newNode);
                }
                else
                {
                    commandInfosNode.AppendChild(newNode);
                }
            }

            xmlDoc.Save(filePath);
        }
        /// <summary>
        /// 获取所有命令配置
        /// </summary>
        /// <returns></returns>
        public Dictionary <string, string> ReadAllCommandConfigs()
        {
            Dictionary <string, string> configKeyAndValue = new Dictionary <string, string>();

            //文件不存在,或节点为空,则返回空字典
            if (FileAndDirectoryHelper.FileIsNotExists(filePath))
            {
                return(configKeyAndValue);
            }

            var         xmlDoc      = XmlFileClass.LoadXmlFile(filePath);
            XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName(CommandInfo);

            for (int i = 0; i < xmlNodeList.Count; i++)
            {
                XmlNode currentNode = xmlNodeList[i];
                if (currentNode.Attributes == null)
                {
                    continue;
                }
                XmlAttribute currentKey   = currentNode.Attributes[ShortName];
                XmlAttribute currentValue = currentNode.Attributes[Command];
                if (currentKey == null || currentValue == null)
                {
                    continue;
                }
                configKeyAndValue[currentKey.InnerText] = currentValue.InnerText;
            }
            return(configKeyAndValue);
        }
        /// <summary>
        /// 修改文件路径
        /// </summary>
        /// <param name="selectRowIndex"></param>
        public void ChangeFilePath(int selectRowIndex)
        {
            string path = FileAndDirectoryHelper.OpenFileDialog(@"*.*|*.*");

            if (!string.IsNullOrEmpty(path))
            {
                ShortNameAndCommandsTable.Rows[selectRowIndex][RealCommandName] = path;
            }
        }
        private void btnExplorerPath_Click(object sender, EventArgs e)
        {
            string filePath = FileAndDirectoryHelper.OpenExeFileDialog();

            if (!filePath.IsNullOrEmpty())
            {
                txbExplorerPath.Text = filePath;
            }
        }
Beispiel #9
0
        private static string CommandNotInConfig(string originalShortName)
        {
            //输入是URL网址或文件路径,则直接打开
            if (IsWellFormedUriString(originalShortName) || FileAndDirectoryHelper.PathIsExists(originalShortName))
            {
                return(ConvertCommandWithQuote(originalShortName));
            }

            return(GetSearchCommand(originalShortName));
        }
        /// <summary>
        /// 写入所有命令配置
        /// </summary>
        /// <param name="configKeyAndValue"></param>
        public void WriteAllCommandConfigs(Dictionary <string, string> configKeyAndValue)
        {
            //文件不存在,则返回
            if (FileAndDirectoryHelper.FileIsNotExists(filePath))
            {
                MessageBoxHelper.ShowErrorMessageBox($"配置文件不存在:{filePath}");
                return;
            }

            UpdateCommandInfos(configKeyAndValue, XmlFileLoader.LoadXmlFile(filePath));
        }
        /// <summary>
        /// 写入所有命令配置
        /// </summary>
        /// <param name="configKeyAndValue"></param>
        public void WriteAllCommandConfigs(Dictionary <string, string> configKeyAndValue)
        {
            //文件不存在,则返回
            if (FileAndDirectoryHelper.FileIsNotExists(filePath))
            {
                MessageBox.Show(string.Format("配置文件不存在:{0}", filePath), @"错误");
                return;
            }
            var xmlDoc = XmlFileClass.LoadXmlFile(filePath);

            UpdateCommandInfos(configKeyAndValue, xmlDoc);
        }
Beispiel #12
0
        /// <summary>
        ///   <para>Imports the config file.</para>
        /// </summary>
        /// <param name="commandTableHandler">The command table handler.</param>
        public static void ImportConfig(ShortCommandTableHandler commandTableHandler)
        {
            string filePath = FileAndDirectoryHelper.OpenXmlFileDialog();
            Dictionary <string, string> importCommandConfig = CommandConfigHandler.ReadCommandConfigFile(filePath);

            if (importCommandConfig == null || importCommandConfig.Count <= 0)
            {
                return;
            }

            List <CurrentAndImportCommand> sharedCommands     = new List <CurrentAndImportCommand>();
            Dictionary <string, string>    nameAndCommandCopy =
                new Dictionary <string, string>(commandTableHandler.UpdateNameAndCommandFromTable());

            foreach (var importPair in importCommandConfig)
            {
                string importCommand = importPair.Value;
                if (FileAndDirectoryHelper.IsInvalidPath(importCommand))
                {
                    continue;
                }

                string name = importPair.Key;
                if (nameAndCommandCopy.TryGetValue(name, out var currentCommand))
                {
                    if (currentCommand.Equals(importCommand))
                    {
                        continue;
                    }
                    sharedCommands.Add(new CurrentAndImportCommand(name, currentCommand, importCommand));
                }
                else
                {
                    nameAndCommandCopy[name] = importCommand;
                }
            }

            //有相同的快捷命令冲突
            if (sharedCommands.Count > 0)
            {
                ShowCommandCompareForm(sharedCommands, nameAndCommandCopy, commandTableHandler);
            }
            else if (!nameAndCommandCopy.SequenceEqual(commandTableHandler.ShortNameAndCommands))
            {
                commandTableHandler.UpdateShortNameAndCommands(nameAndCommandCopy);
            }
        }
        /// <summary>
        /// 清除无效路径
        /// </summary>
        public void ClearInvalidPath()
        {
            int invalidCount = 0;

            for (int i = ShortNameAndCommandsTable.Rows.Count - 1; i >= 0; i--)
            {
                string command = ShortNameAndCommandsTable.Rows[i][RealCommandName].ToString();
                //删除无效路径
                if (FileAndDirectoryHelper.IsInvalidPath(command))
                {
                    ShortNameAndCommandsTable.Rows.RemoveAt(i);
                    invalidCount++;
                }
            }

            MessageBoxHelper.ShowInfoMessageBox(string.Format("共清除{0}个无效路径。", invalidCount));
        }
        /// <summary>
        /// 获取命令
        /// </summary>
        /// <param name="originalShortName">简称</param>
        /// <returns></returns>
        private string GetCommand(string originalShortName)
        {
            string command = GetCommandFormSetting(originalShortName);

            //简称对应的命令不存在
            if (string.IsNullOrEmpty(command))
            {
                //简称是URL网址或文件路径,则直接打开
                if (IsWellFormedUriString(originalShortName) || FileAndDirectoryHelper.PathIsExists(originalShortName))
                {
                    return(ConvertCommandWithQuote(originalShortName));
                }

                //搜索该简称
                return(GetSearchCommand(originalShortName));
            }

            //满足URI格式
            if (IsWellFormedUriString(command))
            {
                return(ConvertCommandWithQuote(command));
            }

            //是目录或文件路径
            if (FileAndDirectoryHelper.IsDirectoryOrFilePath(command))
            {
                //路径不存在
                if (FileAndDirectoryHelper.PathIsNotExists(command))
                {
                    MessageBoxHelper.ShowErrorMessageBox(string.Format(@"对应路径不存在:{0}", command));
                    return(string.Empty);
                }

                return(ConvertCommandWithQuote(command));
            }

            return(string.Format("start \"\" {0}", command));
        }