<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">

<channel>
	<title>Useful scripts | Basic and Advanced Day to day used scripts</title>
	
	<link>http://usefulscripts.wordpress.com</link>
	<description>the scripts which are found rarely on the internet but needed often</description>
	<lastBuildDate>Wed, 30 May 2012 14:14:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain="usefulscripts.wordpress.com" port="80" path="/?rsscloud=notify" registerProcedure="" protocol="http-post" />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Useful scripts | Basic and Advanced Day to day used scripts</title>
		<link>http://usefulscripts.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://usefulscripts.wordpress.com/osd.xml" title="Useful scripts | Basic and Advanced Day to day used scripts" />
	
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/wordpress/ubk1004" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="wordpress/ubk1004" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://usefulscripts.wordpress.com/?pushpress=hub" /><item>
		<title>Run a VBA macro from PowerPoint through C#</title>
		<link>http://usefulscripts.wordpress.com/2012/05/30/run-a-vba-macro-from-powerpoint-through-c/</link>
		<comments>http://usefulscripts.wordpress.com/2012/05/30/run-a-vba-macro-from-powerpoint-through-c/#comments</comments>
		<pubDate>Wed, 30 May 2012 14:14:28 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[asp]]></category>
		<category><![CDATA[C# Script]]></category>
		<category><![CDATA[Office Automation]]></category>
		<category><![CDATA[PowerPoint automation]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[PowerPoint]]></category>
		<category><![CDATA[RunMacro]]></category>
		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=140</guid>
		<description><![CDATA[A small useful post about Office Automation. Hope it will be helpful for others as well. In one of the recent situations I had to run a VBA PowerPoint macro from my C# code. You&#8217;ll find a great post from Microsoft about it from which I have developed my code. You first need to instantiate [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=140&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A small useful post about Office Automation. Hope it will be helpful for others as well.</p>
<p>In one of the recent situations I had to run a VBA PowerPoint macro from my C# code. You&#8217;ll find a great <a href="http://support.microsoft.com/kb/306683" title="HOW TO: Run Office Macros by Using Automation from Visual C# .NET" target="_blank">post from Microsoft</a> about it from which I have developed my code.</p>
<p>You first need to instantiate PowerPoint:</p>
<p><code>using PowerPoint = Microsoft.Office.Interop.PowerPoint;</p>
<p>PowerPoint.Application oPP = new PowerPoint.ApplicationClass();<br />
oPP.DisplayAlerts = Microsoft.Office.Interop.PowerPoint.PpAlertLevel.ppAlertsNone;<br />
oPP.Visible = MsoTriState.msoTrue;</p>
<p>PowerPoint.Presentations oPresSet =  oPP.Presentations;<br />
PowerPoint._Presentation _activePres = oPresSet.Open("PowerPoint File Location",<br />
                                    MsoTriState.msoTrue, MsoTriState.msoFalse,<br />
                                    MsoTriState.msoTrue);<br />
</code></p>
<p>Now, you can use the RunMacro method to call the Macro which are written in that PowerPoint file:</p>
<p><code>object _retobj1 = RunMacro(oPP, new Object[] { "MyMacro1"});<br />
object _retobj2 = RunMacro(oPP, new Object[] { "MyMacro","argument 1"});<br />
</code></p>
<p>If the Macro does not accept any argument then you can use the first one else you can chose the 2nd one.</p>
<p>Here is the run macro method I have used:</p>
<p><code><br />
public static object RunMacro(object oApp, object[] oRunArgs)<br />
        {<br />
			try<br />
                {<br />
					return oApp.GetType().InvokeMember("Run",<br />
                       System.Reflection.BindingFlags.Default |<br />
                       System.Reflection.BindingFlags.InvokeMethod,<br />
                       null, oApp, oRunArgs);</p>
<p>				}<br />
				catch (Exception _exp)<br />
                {<br />
                    //Log Exception<br />
                }</p>
<p>			return null;<br />
		}<br />
</code></p>
<br />Filed under: <a href='http://usefulscripts.wordpress.com/category/asp/'>asp</a>, <a href='http://usefulscripts.wordpress.com/category/c-script/'>C# Script</a>, <a href='http://usefulscripts.wordpress.com/category/microsoft-track/office-automation/'>Office Automation</a>, <a href='http://usefulscripts.wordpress.com/category/microsoft-track/office-automation/powerpoint-automation/'>PowerPoint automation</a> Tagged: <a href='http://usefulscripts.wordpress.com/tag/interop/'>Interop</a>, <a href='http://usefulscripts.wordpress.com/tag/macro/'>macro</a>, <a href='http://usefulscripts.wordpress.com/tag/powerpoint/'>PowerPoint</a>, <a href='http://usefulscripts.wordpress.com/tag/runmacro/'>RunMacro</a>, <a href='http://usefulscripts.wordpress.com/tag/vba/'>VBA</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/140/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=140&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/LDDwyzNGx0Du4BCgekdRqj7_xjU/0/da"><img src="http://feedads.g.doubleclick.net/~a/LDDwyzNGx0Du4BCgekdRqj7_xjU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/LDDwyzNGx0Du4BCgekdRqj7_xjU/1/da"><img src="http://feedads.g.doubleclick.net/~a/LDDwyzNGx0Du4BCgekdRqj7_xjU/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2012/05/30/run-a-vba-macro-from-powerpoint-through-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&amp;d=wavatar&amp;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>DocumentProperties, CustomDocumentProperties and MetaProperties of PowerPoint</title>
		<link>http://usefulscripts.wordpress.com/2012/04/13/documentproperties-customdocumentproperties-and-metaproperties-of-powerpoint/</link>
		<comments>http://usefulscripts.wordpress.com/2012/04/13/documentproperties-customdocumentproperties-and-metaproperties-of-powerpoint/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 13:40:04 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[C# Script]]></category>
		<category><![CDATA[Microsoft Track]]></category>
		<category><![CDATA[Office Automation]]></category>
		<category><![CDATA[PowerPoint automation]]></category>
		<category><![CDATA[CustomDocumentProperties]]></category>
		<category><![CDATA[DocumentProperties]]></category>
		<category><![CDATA[MetaProperties]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=134</guid>
		<description><![CDATA[Some time you want to change the DocumentProperties or CustomDocumentProperties or MetaProperties of a PowerPoint document by programming. I have came around a similar situation in one of my recent project and found some useful bit of knowledge. I was working with PowerPoint automation through C# (2005). So this solution is for PowerPoint only. If [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=134&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Some time you want to change the <code>DocumentProperties </code> or <code>CustomDocumentProperties </code> or <code>MetaProperties </code>of a PowerPoint document by programming. I have came around a similar situation in one of my recent project and found some useful bit of knowledge.</p>
<p>I was working with PowerPoint automation through C# (2005). So this solution is for PowerPoint only. If you want for Word/ Excel you&#8217;ll get lot of useful solutions by googling. But before you try you need to know that many of the solutions might not work in your case and Office Automation is not supported by Microsoft.</p>
<p>Still want to go through the pain! OK, here are the codes:</p>
<p>First add a reference to PowerPoint in your project:</p>
<blockquote><p>
<code>using PowerPoint = Microsoft.Office.Interop.PowerPoint;</code></p></blockquote>
<p>Then, in your suitable method instantiate the Object:</p>
<blockquote><p>
<code>          object oMissing = System.Reflection.Missing.Value;</p>
<p>                //Prepare PowerPoint<br />
                PowerPoint.Application oPP = new PowerPoint.ApplicationClass();<br />
                // If you are running is a batch mode then you might not want the alert message boxes<br />
                oPP.DisplayAlerts = Microsoft.Office.Interop.PowerPoint.PpAlertLevel.ppAlertsNone;<br />
                oPP.Visible = MsoTriState.msoTrue;<br />
                PowerPoint.Presentations oPresSet = oPP.Presentations;<br />
                PowerPoint._Presentation _activePres = oPresSet.Open("YOUR_FILE_PATH",<br />
                                    MsoTriState.msoTrue, MsoTriState.msoFalse,<br />
                                    MsoTriState.msoTrue);<br />
</code>
</p></blockquote>
<p>Now, once you have your PowerPoint object created, then you can go for the Properties.<br />
First, <code>BuiltInDocumentProperties</code></p>
<p>Here is a function which I have created for this purpose:</p>
<p><code>ShowBuiltInDocumentProperties(_activePres.BuiltInDocumentProperties)</code></p>
<blockquote><p>
<code>public static void ShowBuiltInDocumentProperties(object builtInProps)<br />
        {<br />
            Console.WriteLine("Builtin Properties: START");</p>
<p>            Type etype = builtInProps.GetType();<br />
            foreach (int i in Enum.GetValues(etype))<br />
            {<br />
                object item = GetPropertyValue(builtInProps, "Item", i);</p>
<p>                object val = null;<br />
                try { val = GetPropertyValue(item, "Value"); }<br />
                catch { continue; }</p>
<p>                string name = Enum.GetName(etype, i).Substring(10);</p>
<p>                Console.WriteLine(String.Format("Item: {0} Name : {1} Value : {0} ", Convert.ToString(item), name,<br />
                    (null != val) ? Convert.ToString(val) : ""));<br />
            }</p>
<p>            Console.WriteLine("Builtin Properties: END");<br />
        }</p>
<p> public static object GetPropertyValue(object src, object source,<br />
            string name, params object[] parameters)<br />
        {<br />
            return source.GetType().InvokeMember(name,<br />
                BindingFlags.Default | BindingFlags.GetProperty,<br />
                null, source, parameters);<br />
        }</p>
<p></code></p></blockquote>
<p>So, you are through with <code>BuiltInDocumentProperties</code>. Now, lets look at <code>CustomDocumentProperties</code>. For this I also have a function <code>ShowCustomProperties(_activePres.CustomDocumentProperties)</code></p>
<blockquote><p><code><br />
public static void ShowCustomProperties(object customProps)<br />
        {<br />
            Console.WriteLine("Custom Properties: START");<br />
            Type typeCustomProps = customProps.GetType();</p>
<p>            int count = Convert.ToInt32(<br />
                GetPropertyValue(typeCustomProps, customProps, "Count"));</p>
<p>            Console.WriteLine("Custom Properties Count: " + Convert.ToString(count));</p>
<p>            for (int i = 1; i &lt;= count; i++)<br />
            {<br />
                object item = GetPropertyValue(typeCustomProps, customProps, &quot;Item&quot;, i);<br />
                Type typeProp = item.GetType();</p>
<p>                object val = GetPropertyValue(typeProp, item, &quot;Value&quot;);<br />
                string name = GetPropertyValue(typeProp, item, &quot;Name&quot;).ToString();</p>
<p>                Console.WriteLine(String.Format(&quot;Item: {0} Type {1} Name : {2} Value : {3} &quot;,<br />
                    Convert.ToString(item), typeProp.ToString(), name, Convert.ToString(val)));<br />
            }</p>
<p>           Console.WriteLine(&quot;Custom Properties: END&quot;);<br />
        }</p>
<p></code></p></blockquote>
<p>As you can see, I have used the same <code>GetPropertyValue </code>function in this as well.</p>
<p>Ok, let&#8217;s see how to set a <code>CustomDocumentProperty</code>:</p>
<blockquote><p><code><br />
public static void SetCustomProperties(object customProps, string _propName, string _value)<br />
        {<br />
            Console.WriteLine(String.Format("Set Custom Properties: START &gt;&gt; Name: {0} Value {1}", _propName, _value));<br />
            Type typeCustomProps = customProps.GetType();</p>
<p>            int count = Convert.ToInt32(<br />
                GetPropertyValue(typeCustomProps, customProps, "Count"));</p>
<p>            for (int i = 1; i &lt;= count; i++)<br />
            {<br />
                object item = GetPropertyValue(typeCustomProps, customProps, &quot;Item&quot;, i);<br />
                Type typeProp = item.GetType();</p>
<p>                object val = GetPropertyValue(typeProp, item, &quot;Value&quot;);<br />
                string name = GetPropertyValue(typeProp, item, &quot;Name&quot;).ToString();</p>
<p>               Console.WriteLine(String.Format(&quot;Item: {0} Type {1} Name : {2} Value : {3} &quot;,<br />
                    Convert.ToString(item), typeProp.ToString(), name, Convert.ToString(val)));</p>
<p>                if (name.ToLower().Equals(_propName.ToLower()))<br />
                {<br />
                    object[] oArgs = {_propName,false,<br />
                     MsoDocProperties.msoPropertyTypeString,<br />
                     _value};</p>
<p>                    customProps.GetType().InvokeMember(&quot;Item&quot;,<br />
                                               BindingFlags.Default |<br />
                                               BindingFlags.SetProperty,<br />
                                               null, customProps,<br />
                                               oArgs);<br />
                }<br />
            }</p>
<p>            Console.WriteLine(&quot;Set Custom Properties: END&quot;);<br />
        }<br />
</code></p></blockquote>
<p>Ok! We are through with Custom Document Properties and Built In Document Properties. Now, let&#8217;s check Meta Properties.</p>
<p>This Meta Properties are bit tricky. Support you have SharePoint document Library for PowerPoint document type. You have designed a content type with few columns. Some of them are type of string or look up or Integer or multi value look up. Now, you want to change those values programmatically. In this case you need to use the <code>MetaProperty </code>of a PowerPoint document.</p>
<blockquote><p><code><br />
MetaProperties _activePresentationContentTypeProperties<br />
                     = _activePres.ContentTypeProperties;</p>
<p>                    MetaProperty _mp = null;<br />
</code></p></blockquote>
<p>If you want to find a specific property then you can use the following method:</p>
<p><code>FindSpecificContentTypeProperty(_activePres.ContentTypeProperties, "Test")</code></p>
<blockquote><p><code><br />
public static MetaProperty FindSpecificContentTypeProperty(MetaProperties _props, String _propName)<br />
        {<br />
            if (null == _props || _props.Count == 0)<br />
                return null;</p>
<p>            foreach (object _o in _props)<br />
            {<br />
                try<br />
                {<br />
                    MetaProperty _mp = (MetaProperty)_o;</p>
<p>                    if (null != _mp)<br />
                    {<br />
                        //Check if the ppt contains valie for a specific property or not<br />
                        // if value was there then add the value to the template</p>
<p>                        if (_mp.Id.ToLower().Equals(_propName.ToLower()))<br />
                        {<br />
                            Console.WriteLine("Content Type Property found.");<br />
                            return _mp;<br />
                        }<br />
                    }</p>
<p>                }<br />
                catch (Exception _e)<br />
                {<br />
                   throw _e;<br />
                }<br />
            }</p>
<p>            return null;<br />
        }</code></p></blockquote>
<p>Now, once you find the Property you might want to get the data or set the data. Following methods are for that purpose:</p>
<blockquote><p><code><br />
public static String GetContentTypePropertyValue(MetaProperty _cp)<br />
        {<br />
            String sReturn = null;</p>
<p>            try<br />
            {<br />
                if (null == _cp)<br />
                    throw new Exception("Content Type Property is NULL");</p>
<p>                if (null == _cp.Value)<br />
                    throw new Exception("Content Type Property Value is NULL");</p>
<p>                switch (_cp.Type)<br />
                {<br />
                    case MsoMetaPropertyType.msoMetaPropertyTypeMultiChoiceFillIn:<br />
                        String[] _arr = (String[])_cp.Value;<br />
                        sReturn = "";<br />
                        for (int i = 0; i &lt; _arr.Length; i++)<br />
                        {<br />
                            sReturn += _arr[i] + &quot;,&quot;;<br />
                        }<br />
                        sReturn = sReturn.Substring(0, sReturn.Length - 1);<br />
                        break;<br />
                    case MsoMetaPropertyType.msoMetaPropertyTypeNumber:<br />
                        sReturn = Convert.ToString(_cp.Value);<br />
                        break;<br />
                    case MsoMetaPropertyType.msoMetaPropertyTypeLookup:<br />
                        sReturn = Convert.ToString(_cp.Value);<br />
                        break;<br />
                    default:<br />
                        sReturn = Convert.ToString(_cp.Value);<br />
                        break;<br />
                }<br />
            }<br />
            catch (Exception _exp)<br />
            {<br />
                throw _exp;<br />
            }</p>
<p>            return sReturn;<br />
        }</p>
<p>public static void SetContentTypePropertyValue(ref MetaProperty _mp, String _value)<br />
        {<br />
            try<br />
            {<br />
                if (null == _mp)<br />
                    throw new Exception(&quot;Content Type Property is NULL. Please check whether the slide is properly created.&quot;);<br />
                else<br />
                    Console.WriteLine(&quot;Content Type Property: &quot; + _mp.Name);</p>
<p>                if (null == _value)<br />
                    throw new Exception(&quot;Value is NULL.: &quot; + _mp.Name);<br />
                else<br />
                    Console.WriteLine(&quot;Value: &quot; + _value);</p>
<p>                switch (_mp.Type)<br />
                {<br />
                    case MsoMetaPropertyType.msoMetaPropertyTypeMultiChoiceFillIn:<br />
                        Console.WriteLine(&quot;Value Type: MultiChoiceFillIn&quot;);<br />
                        try<br />
                        {<br />
                            String[] _arr = _value.Split(&#039;;&#039;);<br />
                            _mp.Value = _arr;<br />
                        }<br />
                        catch (Exception)<br />
                        {<br />
                            try<br />
                            {<br />
                                _mp.Value = _value;<br />
                            }<br />
                            catch (Exception _ex)<br />
                            {<br />
                                throw _ex;<br />
                            }<br />
                        }</p>
<p>                        break;<br />
                    case MsoMetaPropertyType.msoMetaPropertyTypeNumber:<br />
                        Console.WriteLine(&quot;Value Type: Number&quot;);<br />
                        _mp.Value = _value;<br />
                        break;<br />
                    case MsoMetaPropertyType.msoMetaPropertyTypeLookup:<br />
                        Console.WriteLine(&quot;Value Type: Lookup&quot;);<br />
                        _mp.Value = _value;<br />
                        break;<br />
                    default:<br />
                        _mp.Value = _value;<br />
                        break;<br />
                }<br />
            }<br />
            catch (Exception _exp)<br />
            {<br />
                throw _exp;<br />
            }<br />
        }</p>
<p></code></p></blockquote>
<p>And finally here is how I have closed my PowerPoint instances. This is not related to the topic but as I have instantiated those then I need to release them, right <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<blockquote><p><code><br />
if (null != _activePres)<br />
                    {<br />
                        Console.WriteLine("Close Active Presentation.");<br />
                        _activePres.Close();</p>
<p>                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_activePres);<br />
                        _activePres = null;<br />
                    }</p>
<p>                    if (null != oPresSet)<br />
                    {<br />
                        Console.WriteLine("Close Presentation.");<br />
                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oPresSet);<br />
                        oPresSet = null;<br />
                    }</p>
<p>                    if (null != oPP)<br />
                    {<br />
                        Console.WriteLine("QUIT Power Point.");<br />
                        oPP.Quit();<br />
                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oPP);<br />
                        oPP = null;<br />
                    }</p>
<p>                    GC.Collect();<br />
                    GC.WaitForPendingFinalizers();<br />
                    GC.Collect();<br />
</code></p></blockquote>
<p>I hope this post will come as useful to some one.. Please let me know if there is any issues running the code&#8230;</p>
<br />Filed under: <a href='http://usefulscripts.wordpress.com/category/c-sharp/'>C Sharp</a>, <a href='http://usefulscripts.wordpress.com/category/c-script/'>C# Script</a>, <a href='http://usefulscripts.wordpress.com/category/microsoft-track/'>Microsoft Track</a>, <a href='http://usefulscripts.wordpress.com/category/microsoft-track/office-automation/'>Office Automation</a>, <a href='http://usefulscripts.wordpress.com/category/microsoft-track/office-automation/powerpoint-automation/'>PowerPoint automation</a> Tagged: <a href='http://usefulscripts.wordpress.com/tag/customdocumentproperties/'>CustomDocumentProperties</a>, <a href='http://usefulscripts.wordpress.com/tag/documentproperties/'>DocumentProperties</a>, <a href='http://usefulscripts.wordpress.com/tag/metaproperties/'>MetaProperties</a>, <a href='http://usefulscripts.wordpress.com/tag/powerpoint-automation/'>PowerPoint automation</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=134&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/HuhZ1nW4DdxBQpNr3t_FcKBnzZ8/0/da"><img src="http://feedads.g.doubleclick.net/~a/HuhZ1nW4DdxBQpNr3t_FcKBnzZ8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/HuhZ1nW4DdxBQpNr3t_FcKBnzZ8/1/da"><img src="http://feedads.g.doubleclick.net/~a/HuhZ1nW4DdxBQpNr3t_FcKBnzZ8/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2012/04/13/documentproperties-customdocumentproperties-and-metaproperties-of-powerpoint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&amp;d=wavatar&amp;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>User validation with LDAP in C# (2005)</title>
		<link>http://usefulscripts.wordpress.com/2012/04/11/user-validation-with-ldap-in-c-2005/</link>
		<comments>http://usefulscripts.wordpress.com/2012/04/11/user-validation-with-ldap-in-c-2005/#comments</comments>
		<pubDate>Wed, 11 Apr 2012 11:11:03 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[C# Script]]></category>
		<category><![CDATA[Microsoft Track]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[LDAP]]></category>
		<category><![CDATA[System.DirectoryServices]]></category>
		<category><![CDATA[User Validation]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=126</guid>
		<description><![CDATA[Once in my project I came to a problem where I need to validate a user with LDAP server. My project was in C# (2005). I have searched a lot in Google and at the end I got a working solution. There is another solution as well which does not worked for me but I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=126&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Once in my project I came to a problem where I need to validate a user with LDAP server. My project was in C# (2005). I have searched a lot in Google and at the end I got a working solution. There is another solution as well which does not worked for me but I guess for somebody else it might. So, I&#8217;ll present both of them here:</p>
<p>First the one which is widely used (but didn&#8217;t worked for me <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  )</p>
<p>For this one you need to add a reference to <code>System.DirectoryServices</code> in your code.</p>
<blockquote><p><code><br />
public static bool ValidateUserCredentials(string _domain, string _userName, string _password)<br />
{<br />
DirectoryEntry nRoot = null;</p>
<p>try<br />
{<br />
nRoot = new DirectoryEntry("YOUR_LDAP_SERVER_PATH", _domain + "//" + _userName, _password);<br />
//You can omit the domain if you want<br />
nRoot.AuthenticationType = AuthenticationTypes.None;</p>
<p>Object obj = nRoot.NativeObject;</p>
<p>DirectorySearcher search = new DirectorySearcher(nRoot);</p>
<p>search.SearchScope = SearchScope.Subtree;<br />
search.Filter = "(sAMAccountName=" + _userName + ")";<br />
//You can specify the properties to Load<br />
//search.PropertiesToLoad.Add("uid");</p>
<p>SearchResult _sr = search.FindOne();</p>
<p>if (null != _sr)<br />
{<br />
ResultPropertyCollection myResultPropColl = _sr.Properties;</p>
<p>foreach (string myKey in myResultPropColl.PropertyNames)<br />
{<br />
string tab = "    ";<br />
Console.WriteLine(myKey + " = ");</p>
<p>foreach (Object myCollection in myResultPropColl[myKey])<br />
{<br />
string _val = tab + myCollection;<br />
}<br />
}<br />
return true;<br />
}<br />
else<br />
{<br />
throw new Exception("User Not found");<br />
}</p>
<p>}<br />
catch (Exception _exp)<br />
{<br />
throw _exp;<br />
}<br />
finally<br />
{<br />
if (null != nRoot)<br />
nRoot.Close();<br />
}<br />
}<br />
</code>
</p></blockquote>
<p>And, following is the one worked for me.</p>
<p>For this one you need to add <code>System.DirectoryServices.AccountManagement</code> namespace in your reference.</p>
<blockquote><p> <br />
<code><br />
public static bool ValidateUserCredentials(string _domain, string _userName, string _pwd)<br />
{<br />
try<br />
{<br />
PrincipalContext pc = new PrincipalContext(ContextType.Domain, _domain);</p>
<p>bool isValid = pc.ValidateCredentials(_userName, _pwd);<br />
return isValid;</p>
<p>}<br />
catch (Exception _Exp)<br />
{<br />
throw _Exp;<br />
}<br />
}<br />
</code>
</p></blockquote>
<p>Don&#8217;t forget to tell me which one works for you <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />Filed under: <a href='http://usefulscripts.wordpress.com/category/c-sharp/'>C Sharp</a>, <a href='http://usefulscripts.wordpress.com/category/c-script/'>C# Script</a>, <a href='http://usefulscripts.wordpress.com/category/microsoft-track/'>Microsoft Track</a> Tagged: <a href='http://usefulscripts.wordpress.com/tag/c/'>c#</a>, <a href='http://usefulscripts.wordpress.com/tag/ldap/'>LDAP</a>, <a href='http://usefulscripts.wordpress.com/tag/system-directoryservices/'>System.DirectoryServices</a>, <a href='http://usefulscripts.wordpress.com/tag/user-validation/'>User Validation</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/126/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=126&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/y4_qo0rFNNVAELUIMSNTAC8Rj68/0/da"><img src="http://feedads.g.doubleclick.net/~a/y4_qo0rFNNVAELUIMSNTAC8Rj68/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/y4_qo0rFNNVAELUIMSNTAC8Rj68/1/da"><img src="http://feedads.g.doubleclick.net/~a/y4_qo0rFNNVAELUIMSNTAC8Rj68/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2012/04/11/user-validation-with-ldap-in-c-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&amp;d=wavatar&amp;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>How to use appSettings and connectionStrings in C#</title>
		<link>http://usefulscripts.wordpress.com/2010/12/27/how-to-use-appsettings-and-connectionstrings-in-c/</link>
		<comments>http://usefulscripts.wordpress.com/2010/12/27/how-to-use-appsettings-and-connectionstrings-in-c/#comments</comments>
		<pubDate>Mon, 27 Dec 2010 04:54:37 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[C# Script]]></category>
		<category><![CDATA[Microsoft Track]]></category>
		<category><![CDATA[app.config]]></category>
		<category><![CDATA[AppSettings]]></category>
		<category><![CDATA[ConfigurationManager]]></category>
		<category><![CDATA[connectionStrings]]></category>
		<category><![CDATA[system.configuration]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=109</guid>
		<description><![CDATA[For the developers in C# the most useful file is the App.Config or the Web.Config file and the most used settings in appSettings. You can use any name for the configuration file. Even you can use Multiple configuration file in you application. This configuration file is an XML file having  .config extension. The .NET Framework [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=109&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For the developers in C# the most useful file is the App.Config or the Web.Config file and the most used settings in appSettings. You can use any name for the configuration file. Even you can use Multiple configuration file in you application.</p>
<p>This configuration file is an XML file having  .config extension. The .NET Framework          provides the <strong>System.Configuration.ConfigurationManager.AppSettings</strong> namspace which lets you to retrieve appSettings elements          from the configuration file. Data is stored in key/value pairs as shown in this          sample .config file:</p>
<pre>&lt;appSettings&gt;
&lt;add key="ServiceName" value="Test  Service"/&gt;
&lt;add key="ServiceDisplayName" value="Test Display Service"/&gt;
&lt;add key="ServiceDescription" value="Test Description"/&gt;
&lt;/appSettings&gt;</pre>
<p>Now, to read this appSettings you need to include <strong>System.Configuration </strong>(2.0) in to your application. <strong></strong></p>
<p>Here is a sample code that reads the required appSettings for your application:</p>
<p>String _serviceName = ConfigurationManager.AppSettings["ServiceName"].Trim();<br />
String _serviceDisplayName = ConfigurationManager.AppSettings["ServiceDisplayName"].Trim();<br />
String _serviceDesc = ConfigurationManager.AppSettings["ServiceDescription"].Trim();</p>
<p>Now, you can use all these 3 variables as you like in your application. You can also use a NULL value check before assignment like this:</p>
<p>String _serviceDesc = (null != ConfigurationManager.AppSettings["ServiceDescription"].Trim())<br />
? ConfigurationManager.AppSettings["ServiceDescription"].Trim() : &#8220;Default Value&#8221;;</p>
<p>The following example code reads all the appSettings from the config file in your application:</p>
<pre>    <span style="color:#99cc00;">// Get the AppSettings section.        </span>
<span style="color:#99cc00;">    // This function uses the AppSettings property</span>
<span style="color:#99cc00;">    // to read the appSettings configuration </span>
<span style="color:#99cc00;">    // section.</span>
    public static void ReadAppSettings()
    {
      try
      {
        // Get the AppSettings section.
        NameValueCollection appSettings =
           ConfigurationManager.AppSettings;

        // Get the AppSettings section elements.
        Console.WriteLine();
        Console.WriteLine("Using AppSettings property.");
        Console.WriteLine("Application settings:");

        if (appSettings.Count == 0)
        {
          Console.WriteLine("[ReadAppSettings: {0}]",
          "AppSettings is empty Use GetSection command first.");
        }
        for (int i = 0; i &lt; appSettings.Count; i++)
        {
          Console.WriteLine("#{0} Key: {1} Value: {2}",
            i, appSettings.GetKey(i), appSettings[i]);
        }
      }
      catch (ConfigurationErrorsException e)
      {
        Console.WriteLine("[ReadAppSettings: {0}]",
            e.ToString());
      }
    }</pre>
<p>In some cases you might want to save user input to your appSettings. So, you need to create new appSettings Dynamically or Change an existing one. The following code section demonstrates how to create a new appSettings section. I believe the modification part can be done by looking at these 2 examples:</p>
<pre>    <span style="color:#99cc00;">// Create the AppSettings section.</span>
<span style="color:#99cc00;">    // The function uses the GetSection(string)method </span>
<span style="color:#99cc00;">    // to access the configuration section. </span>
<span style="color:#99cc00;">    // It also adds a new element to the section collection.</span>

    public static void CreateAppSettings()
    {
      // Get the application configuration file.
      System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(
              ConfigurationUserLevel.None);

      string sectionName = "appSettings";

      // Add an entry to appSettings.
      int appStgCnt =
          ConfigurationManager.AppSettings.Count;
      string newKey = "NewKey" + appStgCnt.ToString();

      string newValue = DateTime.Now.ToLongDateString() +
        " " + DateTime.Now.ToLongTimeString();

      config.AppSettings.Settings.Add(newKey, newValue);

      // Save the configuration file.
      config.Save(ConfigurationSaveMode.Modified);

      // Force a reload of the changed section. This
      // makes the new values available for reading.
      ConfigurationManager.RefreshSection(sectionName);

      // Get the AppSettings section.
      AppSettingsSection appSettingSection =
        (AppSettingsSection)config.GetSection(sectionName);

      Console.WriteLine();
      Console.WriteLine("Using GetSection(string).");
      Console.WriteLine("AppSettings section:");
      Console.WriteLine(
        appSettingSection.SectionInformation.GetRawXml());
    }</pre>
<p><strong>ConnectionStrings:</strong></p>
<p>To Read a connectionString from app.config / web.config you can use the following code:</p>
<pre>public string GetConnectionString(string str)
{
//variable to hold our return value
string conn = string.Empty;
//check if a value was provided
if (!string.IsNullOrEmpty(str))
{
//name provided so search for that connection
conn = ConfigurationManager.ConnectionStrings[str].ConnectionString;
return conn;
}
//In all other cases return null
return null;
}</pre>
<p>This method will return the named connection string. The Key Name needs to be pass to the method as input , else it will return null.</p>
<p>Assuming you have the following in your config file:</p>
<p>&lt;connectionStrings&gt;<br />
&lt;add name=&#8221;MyConnString1&#8243; connectionString=&#8221;&lt;connection string 1&gt;&#8221; providerName=&#8221;&lt;provider1&gt;&#8221; /&gt;<br />
&lt;add name=&#8221;MyConnString2&#8243; connectionString=&#8221;&lt;connection string 2&gt;&#8221; providerName=&#8221;&lt;provider2&gt;&#8221; /&gt;<br />
&lt;/connectionStrings&gt;</p>
<p>Here, we have 2 separate connectionStrings. You can have as many as you want. To use it, you just need to call the above method with the name like this:</p>
<pre>String _connecitonString = GetConnectionString("MyConnectionString1");

if(null != _connecitonString){
//Your code
//
}</pre>
<p>Now if you want to read all Connection Strings then you need to use the following code:</p>
<pre>    <span style="color:#99cc00;">// Get the ConnectionStrings section.        </span>
<span style="color:#99cc00;">    // This function uses the ConnectionStrings </span>
<span style="color:#99cc00;">    // property to read the connectionStrings</span>
<span style="color:#99cc00;">    // configuration section.</span>
    public static void ReadConnectionStrings()
    {

      // Get the ConnectionStrings collection.
      ConnectionStringSettingsCollection connections =
          ConfigurationManager.ConnectionStrings;

      if (connections.Count != 0)
      {
        Console.WriteLine();
        Console.WriteLine("Using ConnectionStrings property.");
        Console.WriteLine("Connection strings:");

        // Get the collection elements.
        foreach (ConnectionStringSettings connection in
          connections)
        {
          string name = connection.Name;
          string provider = connection.ProviderName;
          string connectionString = connection.ConnectionString;

          Console.WriteLine("Name:               {0}",
            name);
          Console.WriteLine("Connection string:  {0}",
            connectionString);
         Console.WriteLine("Provider:            {0}",
            provider);
        }
      }
      else
      {
        Console.WriteLine();
        Console.WriteLine("No connection string is defined.");
        Console.WriteLine();
      }
    }</pre>
<p>You can read more about <a title="appSettings" href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx">appSettings </a>and <a title="ConnectionStrings" href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx" target="_blank">connectionStrings </a>in MSDN.</p>
<br />Filed under: <a href='http://usefulscripts.wordpress.com/category/c-sharp/'>C Sharp</a>, <a href='http://usefulscripts.wordpress.com/category/c-script/'>C# Script</a>, <a href='http://usefulscripts.wordpress.com/category/microsoft-track/'>Microsoft Track</a> Tagged: <a href='http://usefulscripts.wordpress.com/tag/app-config/'>app.config</a>, <a href='http://usefulscripts.wordpress.com/tag/appsettings/'>AppSettings</a>, <a href='http://usefulscripts.wordpress.com/tag/configurationmanager/'>ConfigurationManager</a>, <a href='http://usefulscripts.wordpress.com/tag/connectionstrings/'>connectionStrings</a>, <a href='http://usefulscripts.wordpress.com/tag/system-configuration/'>system.configuration</a>, <a href='http://usefulscripts.wordpress.com/tag/webconfig/'>web.config</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/109/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=109&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/OFUrBuC9Smck4w5FgAEatF89Hb8/0/da"><img src="http://feedads.g.doubleclick.net/~a/OFUrBuC9Smck4w5FgAEatF89Hb8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/OFUrBuC9Smck4w5FgAEatF89Hb8/1/da"><img src="http://feedads.g.doubleclick.net/~a/OFUrBuC9Smck4w5FgAEatF89Hb8/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2010/12/27/how-to-use-appsettings-and-connectionstrings-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&amp;d=wavatar&amp;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>How to Get the Time Difference / Total run time of an application in C#</title>
		<link>http://usefulscripts.wordpress.com/2010/12/22/how-to-get-the-time-difference-total-run-time-of-an-application-in-c/</link>
		<comments>http://usefulscripts.wordpress.com/2010/12/22/how-to-get-the-time-difference-total-run-time-of-an-application-in-c/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 18:43:14 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Application run time]]></category>
		<category><![CDATA[Difference between dates]]></category>
		<category><![CDATA[Time]]></category>
		<category><![CDATA[Timespan]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=93</guid>
		<description><![CDATA[In the practical world of coding you&#8217;ll come around a situation when you want to know how long it takes your application to finish. This is very useful to judge the user experience. Depending on the time it takes you can also think of modifying the code to remove extra loops or time consuming methods [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=93&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the practical world of coding you&#8217;ll come around a situation when you want to know how long it takes your application to finish. This is very useful to judge the user experience. Depending on the time it takes you can also think of modifying the code to remove extra loops or time consuming methods so that the application can run faster.</p>
<p>Here is how I did it:</p>
<p>First at the beginning of every coding I have instantiated a new Date object:</p>
<p>DateTime _startdt = DateTime.Now;</p>
<p>and at the end of the application I again instantiate another date object:</p>
<p>DateTime _enddt = DateTime.Now;</p>
<p>Now, we can get the difference between them by just using Timespan class:</p>
<p>TimeSpan ts = _enddt &#8211; _startdt;</p>
<p>You can now display the difference in human readable format:</p>
<p>Console.WriteLine(&#8220;Total Time taken &gt;&gt; &#8221; +<br />
                &#8221; Day :&#8221; + ts.Days +<br />
                &#8221; Hour(s) :&#8221; + ts.Hours +<br />
                &#8221; Minute(s) :&#8221; + ts.Minutes +<br />
                &#8221; Second(s) : &#8221; + ts.Seconds +<br />
                &#8221; Milli Second(s) : &#8221; + ts.Milliseconds<br />
                ); </p>
<p>This is very short but useful trick for us. Hope you&#8217;ll like it.</p>
<br />Filed under: <a href='http://usefulscripts.wordpress.com/category/scripts/'>Scripts</a> Tagged: <a href='http://usefulscripts.wordpress.com/tag/application-run-time/'>Application run time</a>, <a href='http://usefulscripts.wordpress.com/tag/difference-between-dates/'>Difference between dates</a>, <a href='http://usefulscripts.wordpress.com/tag/time/'>Time</a>, <a href='http://usefulscripts.wordpress.com/tag/timespan/'>Timespan</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/93/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&#038;blog=1889208&#038;post=93&#038;subd=usefulscripts&#038;ref=&#038;feed=1" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/zDVCh50BkSGhCjwjdUz6y4Fbsm8/0/da"><img src="http://feedads.g.doubleclick.net/~a/zDVCh50BkSGhCjwjdUz6y4Fbsm8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/zDVCh50BkSGhCjwjdUz6y4Fbsm8/1/da"><img src="http://feedads.g.doubleclick.net/~a/zDVCh50BkSGhCjwjdUz6y4Fbsm8/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2010/12/22/how-to-get-the-time-difference-total-run-time-of-an-application-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&amp;d=wavatar&amp;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
	</channel>
</rss>

