using Container.Common; using Container.Services; using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Container.ChildWindows { /// /// GetUrlForLocal.xaml 的交互逻辑 /// public partial class GetUrlForLocal : Window { App app = ((App)System.Windows.Application.Current); Class_Config config = new Class_Config(); Class_Log log = new Class_Log(); public GetUrlForLocal() { InitializeComponent(); this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; } private void Button_Click(object sender, RoutedEventArgs e) { try { lab_msg.Visibility = System.Windows.Visibility.Hidden; if (string.IsNullOrEmpty(this.Text_HttpUrl.Text)) { lab_msg.Content = "请填写Http地址。"; lab_msg.Visibility = System.Windows.Visibility.Visible; return; } if (string.IsNullOrEmpty(this.Text_WsUrl.Text)) { lab_msg.Content = "请填写Websocket地址。"; lab_msg.Visibility = System.Windows.Visibility.Visible; return; } if (string.IsNullOrEmpty(this.Text_ExePath.Text)) { //if (MessageBox.Show("确认不填写应用程序路径?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No) //{ // lab_msg.Content = "请填写应用程序路径。"; // lab_msg.Visibility = System.Windows.Visibility.Visible; // return; //} } else if (!File.Exists(this.Text_ExePath.Text.ToString()) && !this.Text_ExePath.Text.ToString().Contains("http")) { lab_msg.Content = "未找到应用程序,请确认路径是否正确。"; lab_msg.Visibility = System.Windows.Visibility.Visible; return; } app.configurl = this.Text_HttpUrl.Text.ToString(); app.HttpUrl = this.Text_HttpUrl.Text.ToString(); app.configwebsocket = this.Text_WsUrl.Text.ToString(); HttpMessage httpclient = new HttpMessage(); string Errorlog = string.Empty; string mallCode = string.Empty; //var flag = httpclient.CheckMallRegKey(app.mallRegKey, ref mallCode, ref Errorlog); app.mallCode = mallCode; app.configstartexe = this.Text_ExePath.Text.ToString(); config.WriteToInitialConfig(); ServiceInfo info = new ServiceInfo(); this.ButtonOK.Visibility = System.Windows.Visibility.Hidden; info.ShowDialog(); if (info.DialogResult == true) { this.DialogResult = info.DialogResult; } else { this.ButtonOK.Visibility = System.Windows.Visibility.Visible; } } catch (Exception ex) { log.WriteLogFile(ex.ToString(), "GetUrlErrorLog"); } } private void Services_Loaded(object sender, RoutedEventArgs e) { this.Text_HttpUrl.Text = app.HttpUrl; this.Text_WsUrl.Text = app.configwebsocket; this.Text_ExePath.Text = app.configstartexe; } private void Win_Closed(object sender, EventArgs e) { try { string startup = log.Writestart(); if (string.IsNullOrEmpty(startup)) { MessageBox.Show("创建启动文件失败"); return; } //string startup = System.Windows.Forms.Application.ExecutablePath; //取得程序路径 RegistryKey rKey = Registry.LocalMachine; //开机自动运行 RegistryKey autoRun = rKey.CreateSubKey(@"SOFTWARE\wow6432node\microsoft\windows\currentversion\run"); log.WriteLogFile(@"打开注册表SOFTWARE\wow6432node\microsoft\windows\currentversion\run成功", "RegistryKey"); try { autoRun.SetValue("Container", startup); rKey.Close(); log.WriteLogFile(@"创建注册表启动项成功", "RegistryKey"); } catch (Exception exp) { MessageBox.Show("创建开机自启动失败:" + exp.Message.ToString() + "请手动设置开机自启动"); log.WriteLogFile(exp.Message.ToString(), "RegistryKey"); } int pp = startup.LastIndexOf("\\"); startup = startup.Substring(0, pp); } catch (Exception ex) { MessageBox.Show("创建开机自启动失败:" + ex.Message.ToString() + "请手动设置开机自启动"); //if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["MessageShow"])) //{ // MessageBox.Show(ex.Message.ToString()); //} //else if (ConfigurationManager.AppSettings["MessageShow"].ToLower().Trim().Equals("true")) //{ // MessageBox.Show(ex.Message.ToString()); //} log.WriteLogFile(ex.Message.ToString(), "RegistryKey"); } } private void ExeFile_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFile = new OpenFileDialog();//实例化打开对话框对象 openFile.Filter = "应用程序|*.bat;*.exe";//设置打开文件筛选器 // openExcel.FileName = ""; openFile.Multiselect = false;//设置打开对话框中不能多选 openFile.CheckFileExists = true; //验证路径有效性 openFile.CheckPathExists = true; //验证文件有效性 string strName = string.Empty; if (openFile.ShowDialog() == true) { strName = openFile.FileName; } if (strName == "") { return; } Text_ExePath.Text = openFile.FileName;//显示选择的Exe文件 } } }