128 lines
5.4 KiB
C#
128 lines
5.4 KiB
C#
// 备份目标选择对话框(备份/恢复共用):
|
|
// · 两个大按钮:保存到云端 / 保存到本地文件(恢复语境=从云端恢复 / 从本地备份文件恢复)
|
|
// · "记住我的选择,以后不再提示"复选框:勾选后写 shezhi\备份目标.config.xml,
|
|
// 之后点备份/恢复不再弹框,直接走记住的选项
|
|
// · 云端项在未登录会员时禁用(灰字提示需先登录)
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace 成套报价软件
|
|
{
|
|
public enum 备份目标 { 未定, 云端, 本地文件 }
|
|
|
|
internal static class 备份目标选择
|
|
{
|
|
private const string 配置文件 = @"shezhi\备份目标.config.xml";
|
|
|
|
/// <summary>读记住的选择(0=未记住,1=云端,2=本地)。</summary>
|
|
public static int 读记住的选择()
|
|
{
|
|
try
|
|
{
|
|
string p = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 配置文件);
|
|
if (!File.Exists(p)) return 0;
|
|
var doc = new System.Xml.XmlDocument();
|
|
doc.Load(p);
|
|
var node = doc.SelectSingleNode("//备份目标");
|
|
string v = node != null && node.Attributes["值"] != null ? node.Attributes["值"].Value : "";
|
|
if (v == "云端") return 1;
|
|
if (v == "本地") return 2;
|
|
}
|
|
catch { }
|
|
return 0;
|
|
}
|
|
|
|
public static void 写记住的选择(备份目标 t)
|
|
{
|
|
try
|
|
{
|
|
string p = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 配置文件);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(p));
|
|
string v = t == 备份目标.云端 ? "云端" : "本地";
|
|
File.WriteAllText(p,
|
|
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<备份目标 值=\"" + v + "\" />\r\n",
|
|
new UTF8Encoding(true));
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 弹目标选择框。isRestore=false 备份语境(保存到云端/保存到本地文件);
|
|
/// true 恢复语境(从云端恢复/从本地备份文件恢复)。返回 未定=用户取消。
|
|
/// 用户勾"记住"后写配置,之后调用方不再弹框直接走记住的值。
|
|
/// </summary>
|
|
public static 备份目标 询问(bool isRestore)
|
|
{
|
|
int 记住 = 读记住的选择();
|
|
if (记住 == 1) return 备份目标.云端;
|
|
if (记住 == 2) return 备份目标.本地文件;
|
|
|
|
bool 云端可 = !string.IsNullOrWhiteSpace(MainForm.当前登录手机号);
|
|
备份目标 结果 = 备份目标.未定;
|
|
bool 记住勾 = false;
|
|
|
|
using (var dlg = new Form
|
|
{
|
|
Text = isRestore ? "从哪里恢复?" : "备份到哪里?",
|
|
StartPosition = FormStartPosition.CenterParent,
|
|
FormBorderStyle = FormBorderStyle.FixedDialog,
|
|
MaximizeBox = false,
|
|
MinimizeBox = false,
|
|
ClientSize = new System.Drawing.Size(400, 云端可 ? 200 : 224),
|
|
Font = new System.Drawing.Font("宋体", 9F),
|
|
})
|
|
{
|
|
string 动词 = isRestore ? "从云端恢复" : "保存到云端";
|
|
string 动词2 = isRestore ? "从本地备份文件恢复" : "保存到本地文件";
|
|
|
|
var btn云 = new Button
|
|
{
|
|
Text = 云端可 ? 动词 : 动词 + "(需先登录会员)",
|
|
Location = new System.Drawing.Point(40, 24),
|
|
Size = new System.Drawing.Size(320, 44),
|
|
Enabled = 云端可,
|
|
};
|
|
btn云.Click += (s, e) => { 结果 = 备份目标.云端; dlg.DialogResult = DialogResult.OK; dlg.Close(); };
|
|
dlg.Controls.Add(btn云);
|
|
|
|
var btn本地 = new Button
|
|
{
|
|
Text = 动词2,
|
|
Location = new System.Drawing.Point(40, 78),
|
|
Size = new System.Drawing.Size(320, 44),
|
|
};
|
|
btn本地.Click += (s, e) => { 结果 = 备份目标.本地文件; dlg.DialogResult = DialogResult.OK; dlg.Close(); };
|
|
dlg.Controls.Add(btn本地);
|
|
|
|
if (!云端可)
|
|
{
|
|
dlg.Controls.Add(new Label
|
|
{
|
|
Text = "提示:登录会员后可使用云端备份/恢复",
|
|
Location = new System.Drawing.Point(40, 128),
|
|
Size = new System.Drawing.Size(300, 18),
|
|
ForeColor = System.Drawing.Color.Firebrick,
|
|
});
|
|
}
|
|
|
|
var chk = new CheckBox
|
|
{
|
|
Text = "记住我的选择,以后不再提示",
|
|
Location = new System.Drawing.Point(40, 云端可 ? 132 : 158),
|
|
Size = new System.Drawing.Size(280, 24),
|
|
};
|
|
chk.CheckedChanged += (s, e) => { 记住勾 = chk.Checked; };
|
|
dlg.Controls.Add(chk);
|
|
|
|
dlg.ShowDialog(Form.ActiveForm);
|
|
|
|
if (dlg.DialogResult == DialogResult.OK && 记住勾 && 结果 != 备份目标.未定)
|
|
写记住的选择(结果);
|
|
return 结果;
|
|
}
|
|
}
|
|
}
|
|
}
|