using ryCommon;
using ryCommonDb;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace VSoft.Prams
{
public static class Run
{
[DllImportAttribute("shell32.dll")]
private static extern int SHEmptyRecycleBin(IntPtr handle, string root, int falgs);
const int SHERB_NOCONFIRMATION = 0x000001;
const int SHERB_NOPROGRESSUI = 0x000002;
const int SHERB_NOSOUND = 0x000004;
///
/// 打开软件
///
///
///
///
public static int Open(SoftInfo info, bool RunasAdmin)
{
var path = info.TruePath;
var parm = info.RunPram;
var StartPath = info.StartPath;
var SetJson = info.SetJson;
if (info.SoftType == 1)
{
#region 重新载入数据
IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
if (db.ConnDb(info.Path) == 1)
{
RyQuickSQL mySQL = new RyQuickSQL("Softs");
string where = "";
if(parm.StartsWith("cmdid=",StringComparison.OrdinalIgnoreCase))
{
where = "CmdId=@CmdId";
mySQL.AddField("CmdId", parm.Substring(6));
}
else if (parm.StartsWith("id=", StringComparison.OrdinalIgnoreCase))
{
where = "id=@id";
mySQL.AddField("id", parm.Substring(3).ToInt());
}
if (where.Length > 0)
{
DataSet ds = db.ReadData("select * from Softs where " + where, mySQL);
if (ds.HaveData())
{
DataRow row = ds.Tables[0].Rows[0];
path = RyFiles.GetRealPath(row["Path"].ToString());
parm = row["RunPram"].ToString();
SetJson = row["SetJson"].ToString();
StartPath = row["StartPath"].ToString();
}
else
{
ds.Dispose();
db.Free();
return -1;
}
ds.Dispose();
}
db.Free();
}
#endregion
}
if (StartPath.Length == 0)
{
if (System.IO.File.Exists(path) || System.IO.Directory.Exists(path))
{
StartPath = System.IO.Path.GetDirectoryName(path.TrimEnd('\\'));
}
else
{
StartPath = null;
}
}
if (path.StartsWith("@", StringComparison.OrdinalIgnoreCase)) //如果路径是@开头,则表示这个是一个命令
{
switch(path.Substring(1).ToLower())
{
case "emptyrecyclebin"://清空回收站
switch (Msg.ShowMsg("是否确认要清空回收站?\r\n\r\n该操作不可逆!!!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2))
{
case DialogResult.Yes:
SHEmptyRecycleBin(IntPtr.Zero, "", SHERB_NOCONFIRMATION + SHERB_NOPROGRESSUI + SHERB_NOSOUND);
return 37;
}
break;
case "trans"://翻译
Config.Soft_Config.MainForm.Invoke(new Action(() =>
{
RyForm.ShowOne(new Tools.FrmTrans());
}));
return 37;
}
return 0;
}
Json json = new Json(SetJson);
var RunCount = json.GetJsonValue("RunCount", 1);
var run_result = 37;
for (int i = 0; i < RunCount; i++)
{
if (RunasAdmin)
{
if(RunProcessAsAdmin(path, parm, StartPath, json.GetJsonValue("RunMode", 0))==-1)
{
run_result = -1;break;
}
}
else
{
if (json.GetJsonValue("RunAsAdmin", false))
{
if (RunProcessAsAdmin(path, parm, StartPath, json.GetJsonValue("RunMode", 0)) == -1)
{
run_result = -1; break;
}
}
else
{
if (RyFiles.RunFile(path, parm, StartPath, json.GetJsonValue("RunMode", 0)) < 36)
{
run_result = -1; break;
}
}
}
}
return run_result;
}
///
/// 以管理员方式打开
///
///
///
///
///
public static int RunProcessAsAdmin(string exeName, string parameters, string CurrentDirectory,int RunMode)
{
try
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = CurrentDirectory;
startInfo.FileName = exeName;
startInfo.Verb = "runas";
switch(RunMode)
{
case 0:
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
break;
case 1:
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
break;
case 2:
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
break;
case 3:
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
break;
}
//MLHIDE
startInfo.Arguments = parameters;
startInfo.ErrorDialog = true;
var process = System.Diagnostics.Process.Start(startInfo);
return 37;
}
catch
{
return -1;
}
}
}
}