private static XmlDocument LoadEnforcedConfigFile() { #if DEBUG Stopwatch sw = Stopwatch.StartNew(); #endif g_xdEnforced = null; try { if (!File.Exists(g_strEnforcedConfigFile)) { return(null); } XmlDocument xd = XmlUtilEx.CreateXmlDocument(); xd.Load(g_strEnforcedConfigFile); g_xdEnforced = xd; return(xd); } catch (Exception ex) { FileDialogsEx.ShowConfigError(g_strEnforcedConfigFile, ex.Message, false, false); } #if DEBUG finally { sw.Stop(); } #endif return(null); }
private static AppConfigEx LoadConfigFileEx(string strFilePath, XmlDocument xdEnforced) { if (string.IsNullOrEmpty(strFilePath)) { Debug.Assert(false); return(null); } AppConfigEx tConfig = null; try { if (!File.Exists(strFilePath)) { return(null); } XmlSerializerEx xs = new XmlSerializerEx(typeof(AppConfigEx)); if (xdEnforced == null) { using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { tConfig = (AppConfigEx)xs.Deserialize(fs); } } else // Enforced configuration { XmlDocument xd = XmlUtilEx.CreateXmlDocument(); xd.Load(strFilePath); XmContext ctx = new XmContext(xd, AppConfigEx.GetNodeOptions, AppConfigEx.GetNodeKey); XmlUtil.MergeElements(xd.DocumentElement, xdEnforced.DocumentElement, "/" + xd.DocumentElement.Name, null, ctx); using (MemoryStream msW = new MemoryStream()) { xd.Save(msW); using (MemoryStream msR = new MemoryStream(msW.ToArray(), false)) { tConfig = (AppConfigEx)xs.Deserialize(msR); } } } } catch (Exception ex) { FileDialogsEx.ShowConfigError(strFilePath, ex.Message, false, true); } if (tConfig != null) { tConfig.OnLoad(); } return(tConfig); }
private void OnClickKeyFileBrowse(object sender, EventArgs e) { string strFile = FileDialogsEx.ShowKeyFileDialog(false, KPRes.KeyFileSelect, null, true, m_bSecureDesktop); if (string.IsNullOrEmpty(strFile)) { return; } AddKeyFileItem(strFile, true); }
private string GetKeyFilePath() { string strName = UrlUtil.StripExtension(UrlUtil.GetFileName(m_ioInfo.Path)); if (string.IsNullOrEmpty(strName)) { strName = KPRes.KeyFileSafe; } strName += "." + AppDefs.FileExtension.KeyFile; return(FileDialogsEx.ShowKeyFileDialog(true, KPRes.KeyFileCreateTitle, strName, false, false)); }
private static bool SaveConfigFileEx(AppConfigEx tConfig, string strFilePath) { if (string.IsNullOrEmpty(strFilePath)) { Debug.Assert(false); return(false); } tConfig.OnSavePre(); // Temporarily remove user file preference (restore after saving) bool bPref = tConfig.Meta.PreferUserConfiguration; tConfig.Meta.PreferUserConfiguration = false; bool bResult = true; try { IOConnectionInfo iocPath = IOConnectionInfo.FromPath(strFilePath); using (FileTransactionEx ft = new FileTransactionEx(iocPath, Program.Config.Application.UseTransactedConfigWrites)) { using (Stream s = ft.OpenWrite()) { using (XmlWriter xw = XmlUtilEx.CreateXmlWriter(s)) { XmlSerializerEx xs = new XmlSerializerEx(typeof(AppConfigEx)); xs.Serialize(xw, tConfig); } } ft.CommitWrite(); } } catch (Exception ex) { FileDialogsEx.ShowConfigError(strFilePath, ex.Message, true, false); bResult = false; } tConfig.Meta.PreferUserConfiguration = bPref; tConfig.OnSavePost(); return(bResult); }
private void OnClickKeyFileBrowse(object sender, EventArgs e) { string strFile = FileDialogsEx.ShowKeyFileDialog(false, KPRes.KeyFileUseExisting, null, false, m_bSecureDesktop); if (string.IsNullOrEmpty(strFile)) { return; } try { IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile); if (!IOConnection.FileExists(ioc, true)) { throw new FileNotFoundException(); } // Check the file size? } catch (Exception ex) { MessageService.ShowWarning(strFile, ex); return; } if (!KfxFile.CanLoad(strFile)) { if (!MessageService.AskYesNo(strFile + MessageService.NewParagraph + KPRes.KeyFileNoXml + MessageService.NewParagraph + KPRes.KeyFileUseAnywayQ, null, false)) { return; } } m_cmbKeyFile.Items.Add(strFile); m_cmbKeyFile.SelectedIndex = m_cmbKeyFile.Items.Count - 1; UpdateUIState(); }
public static ProtectedBinary Open(string strName, ProtectedBinary pb, BinaryDataOpenOptions opt) { if (string.IsNullOrEmpty(strName)) { Debug.Assert(false); return(null); } if (pb == null) { Debug.Assert(false); return(null); } if (opt == null) { opt = new BinaryDataOpenOptions(); } byte[] pbData = pb.ReadData(); if (pbData == null) { Debug.Assert(false); return(null); } BinaryDataHandler h = opt.Handler; if (h == BinaryDataHandler.Default) { h = ChooseHandler(strName, pbData, opt); } byte[] pbModData = null; if (h == BinaryDataHandler.InternalViewer) { DataViewerForm dvf = new DataViewerForm(); dvf.InitEx(strName, pbData); UIUtil.ShowDialogAndDestroy(dvf); } else if (h == BinaryDataHandler.InternalEditor) { DataEditorForm def = new DataEditorForm(); def.InitEx(strName, pbData); def.ShowDialog(); if (def.EditedBinaryData != null) { pbModData = def.EditedBinaryData; } UIUtil.DestroyForm(def); } else if (h == BinaryDataHandler.ExternalApp) { pbModData = OpenExternal(strName, pbData, opt); } else { Debug.Assert(false); } ProtectedBinary r = null; if ((pbModData != null) && !MemUtil.ArraysEqual(pbData, pbModData) && !opt.ReadOnly) { if (FileDialogsEx.CheckAttachmentSize(pbModData.LongLength, KPRes.AttachFailed + MessageService.NewParagraph + strName)) { r = new ProtectedBinary(pb.IsProtected, pbModData); } } if (pb.IsProtected) { MemUtil.ZeroByteArray(pbData); } return(r); }