2015/08/26

使用 C# 操作 Word 檔案取代字串,字串太長該怎麼辦

解決方式如下
           object replaceAll = Word.WdReplace.wdReplaceAll;
           object missing = System.Reflection.Missing.Value;
           app.Application.Selection.Find.ClearFormatting();
           app.Application.Selection.Find.Text = (string)findMe;
           app.Application.Selection.Find.Replacement.ClearFormatting();

            if (replaceMe.ToString().Length < 250) //字串若超過 250 時會發生 Exception
           {
                      app.Application.Selection.Find.Replacement.Text = (string)replaceMe;
                      app.Application.Selection.Find.Execute(
                               ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref missing, ref missing, ref missing, ref missing, ref missing,
                               ref replaceAll, ref missing, ref missing, ref missing, ref missing);
          }
          else  //使用找到後,換字串的方式
         {
                      app.Application.Selection.Find.Execute(
                            ref findMe, ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing, ref missing);
                      app.Application.Selection.Text = (string)replaceMe;
          }

2015/07/18

TFS 解除舊電腦的鎖定


因同事換電腦,沒將在舊電腦鎖定的檔案解除鎖定。因在鎖定中的工作區隨舊電腦消失,被鎖定的程式任何人皆無法簽出。

作法如下:下列可以移除 David 的 Computer_David 工作區中指定的檔案鎖定

cd C:\Program Files\Microsoft Visual Studio 8\Common7\IDE tf undo /workspace:Computer_David;David $/Health/ShowMain.cs

2015/07/10

憑證驗章範例

 public bool VerifyXML(string xml, out XmlElement sigElement1)
 {
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.LoadXml(xml);

            XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
            nsm.AddNamespace("dsig", SignedXml.XmlDsigNamespaceUrl);
            XmlElement sigElement =
                (XmlElement)doc.SelectSingleNode("//dsig:Signature", nsm);

            SignedXml sig = new SignedXml(doc);
            sig.LoadXml(sigElement);
            Console.WriteLine("Outer:\n" + doc.OuterXml);

            sigElement1 = sigElement;

            if (sig.CheckSignature())
                return true;
            return false;
}

憑證簽章範例

 public string Sign(string xmlString)
 {
            XmlDocument doc = new XmlDocument();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("cdp", "http://www.hl7.org.tw/EMR/CDocumentPayload/v1.0");
            nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
            nsmgr.AddNamespace("xades132", "http://uri.etsi.org/01903/v1.3.2#");
            nsmgr.AddNamespace("xades141", "http://uri.etsi.org/01903/v1.4.1#");

            doc.PreserveWhitespace = true;
            doc.LoadXml(xmlString);
            SignedXml sig = new SignedXml(doc);

            X509Certificate2 cert = new X509Certificate2("d:\\cer.pfx", "password");
            sig.SigningKey = cert.PrivateKey;

            Reference reff = new Reference("");
            reff.AddTransform(new XmlDsigEnvelopedSignatureTransform());
            sig.AddReference(reff);

            KeyInfo keyInfo = new KeyInfo();
            keyInfo.AddClause(new KeyInfoX509Data(cert));

            sig.KeyInfo = keyInfo;

            sig.ComputeSignature();
            doc.DocumentElement.AppendChild(sig.GetXml());
            StringWriter sw = new StringWriter();
            doc.Save(sw);
            return sw.ToString();
}