Skip to content

Commit

Permalink
修复任务更新提示错误的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sdg32 committed Jun 24, 2016
1 parent 2f1681f commit 5f87f08
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
10 changes: 2 additions & 8 deletions AppRunas/AppForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void btnConfirm_Click(object sender, EventArgs e)
Program.MsgBox("名称不能为空", "ok", "err");
return;
}
if (txtPassword.Text == "")
if (txtPath.Text == "")
{
Program.MsgBox("路径不能为空", "ok", "err");
return;
Expand Down Expand Up @@ -85,13 +85,7 @@ private void btnConfirm_Click(object sender, EventArgs e)

if (Program.AddApp(txtName.Text, txtPath.Text, txtDir.Text, txtArgs.Text, txtUsername.Text, txtPassword.Text, txtDomain.Text))
{
if (appName == null)
{
Program.MsgBox("任务创建成功");
} else
{
Program.MsgBox("任务更改成功");
}
Program.MsgBox((appName == null) ? "任务创建成功" : "任务更改成功");
}
this.Close();
}
Expand Down
4 changes: 2 additions & 2 deletions AppRunas/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ private void MainForm_Load(object sender, EventArgs e)
form.ShowDialog();
if (Program.IsNeedRegister())
{
Application.Exit();
System.Environment.Exit(0);
}
} else
{
LoginForm form = new LoginForm();
if (form.ShowDialog() != DialogResult.OK)
{
Application.Exit();
System.Environment.Exit(0);
}
}

Expand Down
33 changes: 20 additions & 13 deletions AppRunas/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ namespace AppRunas
{
static class Program
{

// 加密密钥
private const string ENCRYPT_SALT_KEY = "UK9ei";
// 加密向量
private const string ENCRYPT_IV = "3gbkpH9MOoSeXD1u";

// 配置文件路径
private static string configPath = Environment.CurrentDirectory + "/config.xml";
// 用户Id:主机名+域名+用户名
private static string userId = Environment.MachineName + "|" + Environment.UserDomainName + "|" + Environment.UserName;

private static XmlDocument doc = LoadConfig();
Expand All @@ -41,11 +44,13 @@ static void Main(string[] args)
}
}

// 判断用户是否需要注册
public static bool IsNeedRegister()
{
return user.GetAttribute("password") == "";
}

// 依任务名称启动程序
public static void RunApp(string name)
{
App app = FindApp(name);
Expand Down Expand Up @@ -88,22 +93,21 @@ public static void RunApp(string name)
}
}

// 设置用户密码
public static bool SetPassword(string password)
{
user.SetAttribute("password", Encrypt(password));
doc.Save(configPath);
return true;
}

// 验证用户密码
public static bool VerifyPassword(string password)
{
if (user == null)
{
return false;
}
return Decrypt(user.GetAttribute("password")) == password;
return (user == null) ? false: Decrypt(user.GetAttribute("password")) == password;
}

// 新增或更新任务
public static bool AddApp(string name, string path, string dir, string args, string username, string password, string domain)
{
XmlElement appXML = FindAppXML(name);
Expand All @@ -123,6 +127,7 @@ public static bool AddApp(string name, string path, string dir, string args, str
return true;
}

// 移除任务
public static bool RemoveApp(string name)
{
XmlElement appXML = FindAppXML(name);
Expand All @@ -134,6 +139,7 @@ public static bool RemoveApp(string name)
return true;
}

// 查找任务
public static App FindApp(string name)
{
XmlElement appXML = FindAppXML(name);
Expand All @@ -152,6 +158,7 @@ public static App FindApp(string name)
return null;
}

// 获取全部任务
public static App[] GetApps()
{
XmlNodeList appsXML = user.SelectNodes("app");
Expand All @@ -174,12 +181,14 @@ public static App[] GetApps()
return apps;
}

// 查找任务XML
public static XmlElement FindAppXML(string name)
{
string query = string.Format("app[@name='{0}']", Encrypt(name));
return (XmlElement)user.SelectSingleNode(query);
}

// 加载配置文件
private static XmlDocument LoadConfig()
{
XmlDocument doc = new XmlDocument();
Expand All @@ -206,6 +215,7 @@ private static XmlDocument LoadConfig()
return doc;
}

// 字符串加密
private static string Encrypt(string text)
{
byte[] key = Encoding.UTF8.GetBytes(getEncryptKey());
Expand All @@ -228,6 +238,7 @@ private static string Encrypt(string text)
}
}

// 字符串解密
private static string Decrypt(string text)
{
byte[] key = Encoding.UTF8.GetBytes(getEncryptKey());
Expand All @@ -250,6 +261,7 @@ private static string Decrypt(string text)
}
}

// 获取加密Key
private static string getEncryptKey()
{
string key = ENCRYPT_SALT_KEY + Environment.UserName + Environment.MachineName;
Expand All @@ -260,15 +272,10 @@ private static string getEncryptKey()
return key.Substring(0, 32);
}

// 消息框函数
public static DialogResult MsgBox(string msg, string btn="ok", string icon="info")
{
MessageBoxButtons msgBtn;
if (btn == "yesno")
{
msgBtn = MessageBoxButtons.YesNo;
} else {
msgBtn = MessageBoxButtons.OK;
}
MessageBoxButtons msgBtn = (btn == "yesno") ? MessageBoxButtons.YesNo : MessageBoxButtons.OK;

MessageBoxIcon msgIcon;
if (icon == "err")
Expand Down

0 comments on commit 5f87f08

Please sign in to comment.