public void LoadTemplate() { if (this.Changed == true) { DialogResult result = MessageBox.Show(this, "装载模板前,发现当前窗口中已有内容修改后未来得及保存。是否要继续装载模板到窗口中(这样将丢失先前修改的内容)?\r\n\r\n(是)继续装载模板 (否)不装载模板", "dp2rms", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result != DialogResult.Yes) { MessageBox.Show(this, "装载模板操作被放弃..."); return; } } OpenResDlg dlg = new OpenResDlg(); dlg.Font = GuiUtil.GetDefaultFont(); dlg.Text = "请选择目标数据库"; dlg.EnabledIndices = new int[] { ResTree.RESTYPE_DB }; dlg.ap = this.MainForm.AppInfo; dlg.ApCfgTitle = "detailform_openresdlg"; dlg.Path = textBox_recPath.Text; dlg.Initial( MainForm.Servers, this.Channels); // dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; textBox_recPath.Text = dlg.Path + "/?"; // 为了追加保存 // 下载配置文件 ResPath respath = new ResPath(dlg.Path); // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { string strContent; string strError; string strCfgFilePath = respath.Path + "/cfgs/template"; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载文件" + strCfgFilePath); stop.BeginLoop(); byte[] baTimeStamp = null; string strMetaData; string strOutputPath; long lRet = channel.GetRes( MainForm.cfgCache, strCfgFilePath, // this.eventClose, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { this.TimeStamp = null; MessageBox.Show(this, strError); return; } else { // MessageBox.Show(this, strContent); SelectRecordTemplateDlg tempdlg = new SelectRecordTemplateDlg(); tempdlg.Font = GuiUtil.GetDefaultFont(); int nRet = tempdlg.Initial(strContent, out strError); if (nRet == -1) { MessageBox.Show(this, "装载配置文件 '" + strCfgFilePath + "' 发生错误: " + strError); return; } tempdlg.ap = this.MainForm.AppInfo; tempdlg.ApCfgTitle = "detailform_selecttemplatedlg"; tempdlg.ShowDialog(this); if (tempdlg.DialogResult != DialogResult.OK) return; this.TimeStamp = null; this.m_strMetaData = ""; // 记忆XML记录的元数据 this.strDatabaseOriginPath = ""; // 保存从数据库中来的原始path nRet = this.SetRecordToControls(tempdlg.SelectedRecordXml, out strError); if (nRet == -1) { MessageBox.Show(this, strError); return; } this.TimeStamp = baTimeStamp; this.Text = respath.ReverseFullPath; } } finally { channel = channelSave; } }
// 保存记录到模板配置文件 // parameters: public void SaveToTemplate() { // 选择目标数据库 OpenResDlg dlg = new OpenResDlg(); dlg.Font = GuiUtil.GetDefaultFont(); dlg.Text = "请选择目标数据库"; dlg.EnabledIndices = new int[] { ResTree.RESTYPE_DB }; dlg.ap = this.MainForm.AppInfo; dlg.ApCfgTitle = "detailform_openresdlg"; dlg.Path = textBox_recPath.Text; dlg.Initial( MainForm.Servers, this.Channels); // dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; // 下载模板配置文件 ResPath respath = new ResPath(dlg.Path); string strError; string strContent; byte[] baTimeStamp = null; string strMetaData; string strOutputPath; string strCfgFilePath = respath.Path + "/cfgs/template"; long lRet = 0; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载文件" + strCfgFilePath); stop.BeginLoop(); lRet = channel.GetRes( MainForm.cfgCache, strCfgFilePath, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { this.TimeStamp = null; MessageBox.Show(this, strError); return; } } finally { channel = channelSave; } SelectRecordTemplateDlg tempdlg = new SelectRecordTemplateDlg(); tempdlg.Font = GuiUtil.GetDefaultFont(); int nRet = tempdlg.Initial(strContent, out strError); if (nRet == -1) goto ERROR1; tempdlg.Text = "请选择要修改的模板记录"; tempdlg.CheckNameExist = false; // 按OK按钮时不警告"名字不存在",这样允许新建一个模板 tempdlg.ap = this.MainForm.AppInfo; tempdlg.ApCfgTitle = "detailform_selecttemplatedlg"; tempdlg.ShowDialog(this); if (tempdlg.DialogResult != DialogResult.OK) return; string strXmlBody = ""; bool bHasUploadedFile = false; nRet = GetXmlRecord(out strXmlBody, out bHasUploadedFile, out strError); if (nRet == -1) goto ERROR1; // 修改配置文件内容 if (tempdlg.textBox_name.Text != "") { // 替换或者追加一个记录 nRet = tempdlg.ReplaceRecord(tempdlg.textBox_name.Text, strXmlBody, out strError); if (nRet == -1) { goto ERROR1; } } if (tempdlg.Changed == false) // 没有必要保存回去 return; string strOutputXml = tempdlg.OutputXml; // 使用Channel channelSave = channel; // 重新获得一次channel, 是因为前面GetXmlRecord()函数有可能摧毁这个变量 channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { // 存回配置文件 stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在保存配置文件 " + strCfgFilePath); stop.BeginLoop(); byte [] baOutputTimeStamp = null; // string strOutputPath = ""; EnableControlsInLoading(true); lRet = channel.DoSaveTextRes(strCfgFilePath, strOutputXml, true, // bInlucdePreamble "", // style baTimeStamp, out baOutputTimeStamp, out strOutputPath, out strError); EnableControlsInLoading(false); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { strError = "保存配置文件"+ strCfgFilePath +"失败,原因: "+strError; goto ERROR1; } } finally { channel = channelSave; } MessageBox.Show(this, "修改模板配置文件成功。"); return; ERROR1: MessageBox.Show(this, strError); }