博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# WinForm 技巧七:读取进程获取进程信息
阅读量:6935 次
发布时间:2019-06-27

本文共 1785 字,大约阅读时间需要 5 分钟。

自动配置软件

 

主软件已经在电脑上运行,我现在要启动相关的小工具,有要配置相同的软件参数,如数据库连接配置,体麻烦就想让小工具自动读取主软件上的配置信息到小工具上。

 

需求:

 

变化点:

     1:主软件没有运行,小工具照样可以运行,那就需要小工具可以手动配置

 步骤:

     1:读取进程

      2:获取进程位置

      3:找到需要的配置文件

      4:读取配置文件信息到小工具上

      5:小工具保存配置信息

实现:

 

主软件进程信息:

寻找配置文件:

界面操作:

读取源码:

//读取进程获取配置        private void button1_Click(object sender, EventArgs e)        {            try            {                #region 读取进程获取配置                Process[] myProcess = Process.GetProcessesByName("HaiLin");                foreach (Process ps in myProcess)                {                    string fileName = ps.MainModule.FileName;                    string configFile = Path.GetDirectoryName(fileName) + @"\HaiLin.exe";                    if (File.Exists(configFile))                    {                        Configuration _innerConfig = ConfigurationManager.OpenExeConfiguration(configFile);                        if (_innerConfig.AppSettings.Settings.Count > 0)                        {                            txt_server.Text = _innerConfig.AppSettings.Settings["server"].Value;                            txt_database.Text = _innerConfig.AppSettings.Settings["db"].Value;                            txt_sa.Text = _innerConfig.AppSettings.Settings["uid"].Value;                            txt_pwd.Text = _innerConfig.AppSettings.Settings["pwd"].Value;                            MessageBox.Show("读取成功!请点击设置保存。");                        }                    }                    else                    {                        MessageBox.Show("配置文件不存在!" + configFile);                    }                }                #endregion            }            catch            {                MessageBox.Show("读取失败!请手动输入配置信息。");            }        }

转载地址:http://eiljl.baihongyu.com/

你可能感兴趣的文章
nova-network
查看>>
LAMP企业架构读写分离
查看>>
PHP高效率写法
查看>>
嵌套在母版页中的repeater自动生成控件ID
查看>>
数据访问层工具类
查看>>
16、SpringBoot------整合MapStruct
查看>>
应用各种领域逻辑模式组织业务逻辑层
查看>>
【原】Iframe with SimpleModal keeps breaking down in IE 9 IE 7
查看>>
Azure Active Directory Connect密码同步问题
查看>>
LoadLibrary("*.dll")失败 - 找不到指定的模块
查看>>
4.2.4 SQL Server数据库文件
查看>>
rails3 ajax替换成js
查看>>
Fix for: Permission denied to access property 'toString'
查看>>
小学生四则运算应用软件(一)
查看>>
bzoj 1004 [HNOI2008]Cards && poj 2409 Let it Bead ——置换群
查看>>
洛谷 1312 Mayan游戏——暴搜+剪枝
查看>>
Qt5遇到的问题
查看>>
「bash」跨多行命令
查看>>
「golang」单个文件和单个函数的单元测试
查看>>
问题 F: 背包问题
查看>>