Thursday, September 8, 2011

Print particular HTML tag alone

<html>
<title>Hai</title>
<head>
<script language="javascript">
    function printme() {
        var content = document.getElementById("div3");
        var pri = document.getElementById("ifmcontentstoprint").contentWindow;
        pri.document.open();
        pri.document.write(content.innerHTML);
        pri.document.close();
        pri.focus();
        pri.print();
    }
</script>
</head>
<body>
<div id="div1">Welcome to Intel Malaysia</div>
<div id="div2">Good Morning</div>
<div id="div3">detached, divided, or separated by cutting
2. (Life Sciences & Allied Applications / Botany) Botany incised or divided cut leaves
3. made, shaped, or fashioned by cutting
4. reduced or diminished by or as if by cutting cut prices
5. (Life Sciences & Allied Applications / Veterinary Science) gelded or castrated
6. weakened or diluted
7. Brit a slang word for drunk
8. hurt; resentful
cut and dried Informal settled or arranged in advance
</div>
<iframe id="ifmcontentstoprint" style="height: 0px; width: 0px; position: absolute"></iframe>
<form name="frmPrintForm">
<input type="button" name="btnPrint" onclick="javascript:printme();" value="Click Me to Print" />
</form>
</body>
</html>

Set value to Register key

RegistryKey appConfigKey = Registry.LocalMachine.CreateSubKey(REGISTRY_PATH);
appConfigKey.SetValue(name,val);

Get System Register Key value

RegistryKey appConfigKey = Registry.LocalMachine.OpenSubKey(keypath);
if(appConfigKey!=null)
{
  string[] appValues = appConfigKey.GetValueNames();
  foreach(string v in appValues)
    console.write(v);
}

Wednesday, September 7, 2011

Get Temp Directory

  public static string TempDirectory()
        {
            string result = string.Empty;
            result = Environment.ExpandEnvironmentVariables("%TMP%");
            if (string.IsNullOrEmpty(result)) result = Environment.ExpandEnvironmentVariables("%TEMP%");
            return result;
        }

Start Notepad application

   public static void StartNotepad(string filePath)
        {
            try
            {
                WshShell x = new WshShell();
                x.Exec(string.Format("notepad.exe {0}", filePath));
            }
            catch (Exception ex)
            {
                            }
        }

Get XML value by path

  public static string GetXmlByXPath(XmlDocument document, string path, string defaultValue)
        {
            string result = defaultValue;
            if (document != null)
            {
                XmlNodeList list = document.SelectNodes(path);
                if (list != null && list.Count > 0)
                    result = list[0].InnerText;
            }
            return result;
        }

  public void LoadXml(string xmlData)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlData);
            RequestType = GetXmlByXPath(doc, "/ROOT/Person/Name", string.Empty);
         }

Monday, September 5, 2011

How can we make a deep copy of a WPF object?

How can we make a deep copy of a WPF object?
We can make a deep copy of a WPF object using XamlWriter and XamlReader. Here the XamlWriter.Save is used to serialize the contents of a WPF object into xaml string. XamlReader.Load is used to parse XAML string into a WPF object. To make deep copy of an wpf UIelement , you can use the following method.

1: public UIElement DeepCopy(UIElement element)
2: {
3: string shapestring = XamlWriter.Save(element);
 4: StringReader stringReader = new StringReader(shapestring);
5: XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
6: UIElement DeepCopyobject = (UIElement)XamlReader.Load(xmlTextReader);
7: return DeepCopyobject;
8: }

If you would like to make deep copy of WPF objects without XamlWriter, you have to use reflection .For that you have to navigate recursively inside the type to get all Dependency Properties and Dependency Objects as well as plain .net properties