新建工程。
将上面建的类导入到该工程中。
添加了4个button 分别设置为开启,停止,配置和关闭服务器
添加一个picturebox 作为服务器开始停止标识。
添加一个notifyIcon 作为状态栏标识。
添加一个statusStrip 作为服务器的状态显示。
添加一个contextMenuStrip。作为notifyIcon控件的右键显示。
添加一个checkbox 作为是否系统启动就运行本服务器的标识
具体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Microsoft.Win32;
using System.IO;
using qqServerManger;
namespace qqServer
{
public partial class Form1 : Form
{
private string SerIp = "";
private string SerPort = "";
private Server QQserver = new Server();
private string SysInf = "QQ2008服务已经停止!";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string SerXmlUrl = "config\\ipConfig.dat";
SerIp = qSaveConfigServer.GetValueByNode(SerXmlUrl, "//root//server//ip");
SerPort = qSaveConfigServer.GetValueByNode(SerXmlUrl, "//root//server//port");
if (SerIp == "-1" || SerPort == "-1")
{
MessageBox.Show("系统配置文件错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.button1.Enabled = false;
}
else
{
this.comboBox1.Items.Add(SerIp);
this.pictureBox1.Image = global::qqServer.Properties.Resources.stop;
this.toolStripStatusLabel2.Text = "服务已停止!";
this.button2.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.Text == "")
{
MessageBox.Show("请选择服务器!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
this.toolStripStatusLabel2.Text = "服务启动中……";
if (QQserver.IniServer(SerIp, SerPort))
{
QQserver.StartServer();
XmlFileOp.DelThisFile("onlineinf\\onLine.xml");
this.pictureBox1.Image = global::qqServer.Properties.Resources.start;
this.button1.Enabled = false;
this.button2.Enabled = true;
SysInf = "QQ2008服务正在运行!";
this.notifyIcon1.Icon = ((System.Drawing.Icon)(global::qqServer.Properties.Resources.ser_start));
this.toolStripStatusLabel2.Text = "服务已经运行";
}
else
{
MessageBox.Show("无法初始化服务!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
Form2 show = new Form2();
show.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
if (QQserver.StopServer())
{
this.button1.Enabled = true;
this.button2.Enabled = false;
this.toolStripStatusLabel2.Text = "服务已停止!";
SysInf = "QQ2008服务已经停止!";
this.notifyIcon1.Icon = ((System.Drawing.Icon)(global::qqServer.Properties.Resources.ser_stop));
this.pictureBox1.Image = global::qqServer.Properties.Resources.stop;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
try
{
if (this.checkBox1.Checked)
{
RegistryKey ms_run = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
ms_run.SetValue("mistysoft", Application.ExecutablePath.ToString());
//HKEY_LOCAl_MACHINE
}
else
{
RegistryKey ms_run = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
ms_run.SetValue("mistysoft", "");
}
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("保存设置失败!请确认本程序能够访问注册表", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 fo = new Form3();
fo.ShowDialog();
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Visible = true;
}
private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
{
this.notifyIcon1.Text = SysInf;
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Registry:位于Microsoft.Win32 提供表示 Windows 注册表中的根项的 RegistryKey对象,并提供访问项/值对的 static 方法。
Registry.LocalMachine:
包含本地计算机的配置数据。该字段读取Windows 注册表基项HKEY_LOCAL_MACHINE。
RegistryKey:表示 Windows 注册表中的项级节点。此类是注册表封装。
RegistryKey.OpenSubKey :检索指定的子项。
RegistryKey.SetValue:
设置注册表项中的名称/值对的值。从所存储数据的类型或指定的 RegistryValueKind 确定注册表数据类型
注册表充当计算机上操作系统和应用程序的中央信息储存库。注册表根据存储在其中的元素的逻辑顺序,以分层形式组织(有关该层次结构中的基级项,请参见 Registry)。在注册表中存储信息时,请根据存储的信息类型选择适当的位置。一定要避免损坏由其他应用程序创建的信息,原因是这样会导致那些应用程序出现意外的行为,并且还会对您自己的应用程序带来不利影响。
注册表项是注册表中的基本组织单位,好比是 Windows 资源管理器中的文件夹。每个具体的注册表项都可以有子项,就像文件夹下可以有子文件夹一样。只要用户具有相应的权限,且注册表项不是基项或基项的下一级项,就可以删除该注册表项。每个注册表项也可带有与其相关联的多个值(一个值就好比是一个文件),它们用于存储信息,例如,有关计算机上安装的应用程序的信息。每个值存储特定的信息,可按需要对其进行检索或更新。例如,可以为您的公司创建一个 RegistryKey(在项 HKEY_LOCAL_MACHINE\Software 下),然后为您的公司创建的每个应用程序创建一个子项。每个子项保存特定于该应用程序的信息,如颜色设置、屏幕位置和大小或者可识别的文件扩展名。
单击服务器配置。
显示服务器器配置窗口
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using qqServerManger;
namespace qqServer
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (regx.isNull(this.tIpQQ.Text) || regx.isNull(this.tPortQQ.Text))
{
MessageBox.Show("请输入qq服务器ip地址或端口号", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (regx.isIpaddres(this.tIpQQ.Text))
{
if (qSaveConfigServer.SaveConfigServer(this.tIpQQ.Text, this.tPortQQ.Text))
{
MessageBox.Show("信息保存成功!", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("保存失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("你输入ip地址错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string DataIp = this.tDataIp.Text;
string DataBase = this.tDataName.Text;
string DataUid = this.tDataUid.Text;
string DataPwd = this.tDataPwd.Text;
if (regx.isNull(DataIp) || regx.isNull(DataBase) || regx.isNull(DataUid) || regx.isNull(DataPwd))
{
MessageBox.Show("请你输入服务器信息!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// if (regx.isIpaddres(DataIp))
if(true)
{
if (qSaveConfigServer.SaveConfigDate(DataIp, DataBase, DataUid, DataPwd))
{
MessageBox.Show("信息保存成功!", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("保存失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("你输入ip地址错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void button5_Click(object sender, EventArgs e)
{
string DataIp = this.tDataIp.Text;
string DataBase = this.tDataName.Text;
string DataUid = this.tDataUid.Text;
string DataPwd = this.tDataPwd.Text;
if (regx.isNull(DataIp) || regx.isNull(DataBase) || regx.isNull(DataUid) || regx.isNull(DataPwd))
{
MessageBox.Show("请你输入服务器信息!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (SqlConnectionTest.ConnectionTest(DataIp, DataBase, DataUid, DataPwd))
{
MessageBox.Show("链接测试成功!", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("链接测试失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
0 评论:
发表评论