现在做了一个程序包括winform程序(跑界面部分),windows服务,winpcap类库
把他们3个做成一个安装包,我现在想安装执行的时候在安装事件里面直接把windows服务和winpcap装上
问题:安装事件在什么地方写
调用安装windows服务和winpcap的代码怎么实现直接在代码中调用CMD。参考这个 C# codeProcess p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true; //不显示命令行窗口
p.Start();
p.StandardInput.WriteLine("InstallUtil -i 服务程序集文件名"); //确保InstallUtil在当前目录下 不知道上面这段代码能否实现对windows和winpcap的安装,如果能,写在什么地方呢?都说写在安装事件中,但安装事件在哪??
把他们3个做成一个安装包,我现在想安装执行的时候在安装事件里面直接把windows服务和winpcap装上
问题:安装事件在什么地方写
调用安装windows服务和winpcap的代码怎么实现直接在代码中调用CMD。参考这个 C# codeProcess p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true; //不显示命令行窗口
p.Start();
p.StandardInput.WriteLine("InstallUtil -i 服务程序集文件名"); //确保InstallUtil在当前目录下 不知道上面这段代码能否实现对windows和winpcap的安装,如果能,写在什么地方呢?都说写在安装事件中,但安装事件在哪??
解决方案 »
- c# 执行dos命令问题
- asp.net连接本地或者远程oracle数据库
- 满分求解一个数据转换的题 急啊,在线等
- 构造函数 和 load的区别
- 上传文件限制文件类型的方法讨论.
- C#写入带格式的EXCEL文件??
- 我在PictureBox上放了很多label,设置背景为Transparent,label.Parent=pictureBox,但位置变了,变成以PictureBox为基础坐标,该如何处理
- 请问Convert.ToInt32() 和 (int) 有何区别
- WinCE下,C#程序如何与C++写的DLL中的线程进行同步??
- 网站
- 观察者模式的实现
- C#怎么限制自己指定的单元格的输入格式。
private static string GetScript(string name)
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream str = asm.GetManifestResourceStream(
asm.GetName().Name+ "." + name);
StreamReader reader = new StreamReader(str);
return reader.ReadToEnd();
}
private static string GetLogin(string databaseServer,
string userName,string userPass,string database)
{
return "server=" + databaseServer +
";database="+database+";User ID=" + userName +
";Password=" + userPass;
}private static void ExecuteSql(SqlConnection sqlCon)
{
string[] SqlLine;
Regex regex = new Regex("^GO",RegexOptions.IgnoreCase | RegexOptions.Multiline);
string txtSQL = GetScript("install.txt");
SqlLine = regex.Split(txtSQL); SqlCommand cmd = sqlCon.CreateCommand();
cmd.Connection = sqlCon; foreach(string line in SqlLine)
{
if(line.Length>0)
{
cmd.CommandText = line;
cmd.CommandType = CommandType.Text;
try
{
cmd.ExecuteNonQuery();
}
catch(SqlException)
{
//rollback
ExecuteDrop(sqlCon);
break;
}
}
}
}
private static void ExecuteDrop(SqlConnection sqlCon)
{
if(sqlCon.State!=ConnectionState.Closed)sqlCon.Close();
sqlCon.Open();
SqlCommand cmd = sqlCon.CreateCommand();
cmd.Connection = sqlCon;
cmd.CommandText = GetScript("uninstall.txt");
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
sqlCon.Close();
}
public override void Install(IDictionary stateSaver)
{
base.Install (stateSaver); if(Context.Parameters["databaseServer"].Length>0 &&
Context.Parameters["userName"].Length>0 &&
Context.Parameters["userPass"].Length>0)
{
conStr = GetLogin(
Context.Parameters["databaseServer"],
Context.Parameters["userName"],
Context.Parameters["userPass"],
"master"); RijndaelCryptography rijndael = new RijndaelCryptography();
rijndael.GenKey();
rijndael.Encrypt(conStr);
//save information in the state-saver IDictionary
//to be used in the Uninstall method
stateSaver.Add("key",rijndael.Key);
stateSaver.Add("IV",rijndael.IV);
stateSaver.Add("conStr",rijndael.Encrypted);
} SqlConnection sqlCon = new SqlConnection(conStr); sqlCon.Open();
ExecuteSql(sqlCon);
if(sqlCon.State!=ConnectionState.Closed)sqlCon.Close();
}public override void Uninstall(IDictionary savedState)
{
base.Uninstall (savedState); if(savedState.Contains("conStr"))
{
RijndaelCryptography rijndael = new RijndaelCryptography();
rijndael.Key = (byte[])savedState["key"];
rijndael.IV = (byte[])savedState["IV"];
conStr = rijndael.Decrypt((byte[])savedState["conStr"]);
} SqlConnection sqlCon = new SqlConnection(conStr); ExecuteDrop(sqlCon);
}
试试吧,我没试
二楼的也谢谢你,呵呵不过数据库部分用不到,重写的那install和uninstall方法可以参照
谁还有具体这方面的实现更好,谢谢
还可使用installshoield安装脚本实现安装
installutil myservice1.exe
参考http://www.cnblogs.com/downmoon/archive/2007/12/29/1019924.html
http://blog.csdn.net/downmoon/archive/2007/04/24/1581113.aspx
有试过同时安装2个windows服务,安装一个主程序和一个windows服务都可以成功