.net制作安装包获取当前Step.exe路径
.net制作安装包获取当前Step.exe路径
我的.Net项目在制作打包安装时需要读取已经配置好的服务器数据库连接字符串,(在安装过程中严重用户身份)这样需要在安装包之外有个配置文件供安装包使用,安装包如何正确读取配置文件? Application.StartupPath取得的路径是系统msiexec.exe的运行路径C:"WINDOWS"system32, 要想得到Setup.exe运行路径,改如何是好?换种思路来处理:先取得当前运行的所有Process然后再找到那个Setup或是*.msi那不就可以了。
/// <summary>
/// 获取当前运行steup路径
/// </summary>
/// <returns></returns>
private string GetsetupDir()
{
Process[] processes = Process.GetProcesses();
string tmp = "";
int i = 0;//调试时为了查找线程的个数
foreach (Process pro in processes)
{
//MessageManagerSetup即为你的打包文件在运行时显示的标题
if (pro.ProcessName.ToLower() == "msiexec" && pro.MainWindowTitle == "MessageManagerSetup")
{
//这里在Vista下测试时因为要读写文件(由于Vista使用了UAC来增强安全性,
//要求必须以管理员身份运行,而直接运行msi文件只能以普通权限来运行),
//所以不让用户直接运行MSI文件,而要求必须从Setup.exe文件运行,
//如果你不需要在Vista下使用则不用如此处理
MessageBox.Show("Please run setup.exe", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new InstallException("run");
}
else if (pro.ProcessName.ToLower().IndexOf("setup") > 0 || pro.MainWindowTitle.ToLower() == "messagemanagersetup")
{//判断ProcessName是否是setup,并且其MainWindowTitle为你的安装文件的Title
tmp = pro.MainModule.FileName;//pro.MainModule.FileName即为Setup的完整运行路径
break;
}
}
// MessageBox.Show(tmp);
if (tmp.EndsWith(@"\"))
tmp = tmp.Substring(0, tmp.Length - 1);
tmp = tmp.Substring(0, tmp.LastIndexOf(@"\"));//得到运行所在文件夹
return tmp;
}
浙公网安备 33010602011771号