示例#1
0
 public ModifyRecord Analyze(UserInfo info)
 {
     if (!Directory.Exists(info.SourcePath) || !Directory.Exists(info.TargetPath))
     {
         MainWindow.Log.Add("请检查同步文件夹目录是否存在!!");
         return null;
     }
     ModifyRecord modifyRecord = Sync(info.SourcePath, info.TargetPath,info.SyncType);
     modifyRecord.SyncType = info.SyncType;
     return modifyRecord;
 }
 /// <summary>
 /// 测试用同步入口
 /// </summary>
 private void Startup(UserInfo userinfo)
 {
     userinfo.SourcePath = left_textbox.Text;
     userinfo.TargetPath = right_textbox.Text;
     if (userinfo.SourcePath == string.Empty
         ||userinfo.TargetPath == string.Empty
         )
     {
         Log.Add("同步文件为空!!");
     }
     else if (userinfo.SourcePath == userinfo.TargetPath)
     {
         Log.Add("同步文件夹路径相同!!");
     }
     else if (userinfo.SyncType<(int)SYNCType.Sync
             ||userinfo.SyncType>(int)SYNCType.Combine)
     {
         Log.Add("未选择同步方式!!");
     }
     else
     {
         // 文件备份的问题可以在别的文件夹保存
         DealBackupFile(userinfo);
         Start_Button.IsEnabled = false;
         Log.Add("正在开始同步...");
         try
         {
             new Thread(() =>
             {
                 AnalyzerFactory analyzerFactory = new AnalyzerFactory();
                 IAnalysis iAnalysis = analyzerFactory.CreatAnalyser(userinfo.SyncType);
                 ModifyRecord modifyRecord = iAnalysis.Analyze(userinfo);
                 Execute execute = new Execute();
                 execute.Fix(modifyRecord);
                 this.Dispatcher.Invoke(new Action(() =>
                 {
                     MessageBox.Show("同步完成!");
                     Start_Button.IsEnabled = true;
                 }));
             }).Start();
         }
         catch (ArgumentNullException e)
         {
             Log.Add(e.Message);
             Start_Button.IsEnabled = true;
         }
     }
 }
        /// <summary>
        /// 处理记录文件
        /// </summary>
        /// <param name="userinfo"></param>
        private void DealBackupFile(UserInfo userinfo)
        {
            string soutarspecificText = CreateBackupFileName(FormatPath(userinfo.SourcePath), userinfo.SourcePath, userinfo.TargetPath,
                                                            userinfo.SyncType.ToString());
            string tarsouspecificText = CreateBackupFileName(FormatPath(userinfo.SourcePath), userinfo.TargetPath, userinfo.SourcePath,
                                                            userinfo.SyncType.ToString());

            if (!File.Exists(soutarspecificText) && !File.Exists(tarsouspecificText))
            {
                string[] soudelete = Directory.GetFiles(userinfo.SourcePath, "MrDebugger-*",
                    SearchOption.TopDirectoryOnly);
                for (int i = 0; i < soudelete.Length; i++)
                {
                    File.Delete(soudelete[i]);
                }

                string[] tardelete = Directory.GetFiles(userinfo.TargetPath, "MrDebugger-*",
                    SearchOption.TopDirectoryOnly);
                for (int i = 0; i < tardelete.Length; i++)
                {
                    File.Delete(tardelete[i]);
                }
                //加入xml
                helpSDHxml.Add(new HelpSyncDirectoryHistory
                {
                    Id = 1,
                    PairName = userinfo.SourcePath,
                    LeftDirectoryName = userinfo.SourcePath,
                    RightDirectoryName = userinfo.TargetPath,
                    SyncType = GetMethodName(userinfo.SyncType),
                    SyncTypeId = userinfo.SyncType
                });
            }
        }
 private void Button_Click_Start(object sender, RoutedEventArgs e)
 {
     UserInfo Static_userinfo = new UserInfo();
     Static_userinfo.SourcePath = left_textbox.Text;
     Static_userinfo.TargetPath = right_textbox.Text;
     Static_userinfo.SyncType = SyncComboBox.SelectedIndex;
     if (MessageBoxResult.OK ==
         MessageBox.Show(
             "源目录:" + Static_userinfo.SourcePath + "\r\n目标目录:" + Static_userinfo.TargetPath + "\r\n同步方式:" +
             GetMethodName(Static_userinfo.SyncType),
             "请确定您的操作", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel))
     {
         Startup(Static_userinfo);
     }
 }
 private void Button_Click_ReBoot(object sender, RoutedEventArgs e)
 {
     UserInfo Static_userinfo = new UserInfo();
     Static_userinfo.SourcePath = FormatPath(left_textbox.Text);
     Static_userinfo.TargetPath = FormatPath(right_textbox.Text);
     if (MessageBoxResult.OK ==
         MessageBox.Show(
             Static_userinfo.SourcePath + "-" + Static_userinfo.TargetPath + "-" + Static_userinfo.SyncType,
             "显示userinfo", MessageBoxButton.OKCancel))
         AutoReboot(Static_userinfo);
 }
        /// <summary>
        /// 以管理员权限重启程序
        /// </summary>
        private static void AutoReboot(UserInfo staticUserinfo)
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(identity);

            if (principal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                MessageBox.Show("现在我是管理员");
            }
            else
            {
                MessageBoxResult dr = MessageBox.Show("你好,是否允许获取管理员权限,并重启应用?", "获取管理员权限", MessageBoxButton.OKCancel,
                    MessageBoxImage.Question, MessageBoxResult.OK);

                if (dr == MessageBoxResult.OK)
                {
                    Assembly.GetEntryAssembly();
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.UseShellExecute = true;
                    startInfo.WorkingDirectory = Environment.CurrentDirectory;
                    startInfo.FileName = Application.Current.MainWindow.GetType().Assembly.Location;
                    startInfo.Verb = "runas";
                    startInfo.Arguments = "\"" + staticUserinfo.SourcePath + "\" \"" + staticUserinfo.TargetPath +
                                          "\" " + staticUserinfo.SyncType;
                    try
                    {
                        Process.Start(startInfo);
                    }
                    catch (InvalidOperationException e)
                    {
                        Debug.WriteLine(e.Message);
                    }
                    Application.Current.Shutdown();
                }
            }
        }