tag:blogger.com,1999:blog-72252821671661856722024-03-05T23:42:17.928-08:00dot net figured outkmdm blogUnknownnoreply@blogger.comBlogger10125tag:blogger.com,1999:blog-7225282167166185672.post-30337755479020785332008-11-21T01:44:00.000-08:002008-12-08T01:22:18.066-08:00Walking through Enum entries in C#<span style="font-size:85%;font-family:arial;"><br />Sometimes the need appears in make foreach() construction for Enum entries. Here I'll show how to do it.<br /></span><br /><span style="font-size:85%;font-family:courier new;"><br /><br /> Type theType = typeof(MyEnum);<br /> MemberInfo[] theMembers = theType.GetMembers();<br /><br /> foreach (MemberInfo entry in theMembers)<br /> {<br /> if ((entry.MemberType.ToString() == "Field"))<br /> if((entry.Name != "value__"))<br /> {<br /> MyEnum theTyp = (MyEnum)Enum.Parse(typeof(MyEnum), entry.Name);<br /> }<br /> }<br /></span><br /><span style="font-size:85%;font-family:arial;"><br />As earlier I'm trying to present finished solutions ready to use in your own projects. :)<br /><br /></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7225282167166185672.post-6089399422996371172008-11-07T08:03:00.000-08:002008-11-11T04:55:35.078-08:00C# numeric format string with fixed length<span style="font-size:85%;font-family:arial;"><br />Do you want to get from 'int' value 'string' representation with fixed length?<br />Here is solution:) Example:<br /></span><br /><span style="font-size:85%;font-family:courier new;"><br />int iVal = 53;<br />string strVal = iVal.ToString("0000");<br /></span><br /><span style="font-size:85%;font-family:courier new;"><br />Result:<br />0053<br /></span><br /><span style="font-size:85%;font-family:arial;"><br />Enjoy:)<br /></span><br /><br/>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7225282167166185672.post-82905591755760115892008-10-24T05:55:00.000-07:002008-11-07T08:12:32.556-08:00Create Registry Path in PowerShell<span style=";font-family:arial;font-size:85%"><br />Here is script to create registry path (or separate key) in PowerShell<br /></span><br /><span style=";font-family:courier new;font-size:85%"><br /><br />function vrIsRegistryPathContainsKey($basePath, $key)<br />{<br /> if($key.Length -le 0)<br /> { return 0 }<br /> <br /> $childItems = Get-ChildItem $basePath -Force -ErrorAction SilentlyContinue<br /> foreach ($childItem in $childItems)<br /> {<br /> if($childItem.name -match $key)<br /> {<br /> return 1<br /> }<br /> } <br /> return 0<br />}<br /><br />function vrCreateRegistryPath($basePath, $createdPath)<br />{ <br /> $pathTokens = $createdPath.split("\")<br /> foreach ($pathToken in $pathTokens)<br /> {<br /> if($pathToken.Length -le 0)<br /> { break }<br /> <br /> if(-not(vrIsRegistryPathContainsKey $basePath $pathToken))<br /> { <br /> echo ('not found ' + $pathToken);<br /> New-Item -Path $basePath -Name $pathToken<br /> }<br /> $basePath = $basePath + "\" + $pathToken<br /> echo ('basepath ' + $basePath);<br /> }<br />}<br /></span><br /><span style=";font-family:arial;font-size:85%"><br />Usage:<br />vrCreateRegistryPath 'Microsoft.PowerShell.Core\Registry::\HKEY_LOCAL_MACHINE\SOFTWARE' 'Key\SubKey1\Subkey2\'<br /></span><br /><br>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7225282167166185672.post-84223456897384230282008-10-21T07:49:00.000-07:002008-10-21T07:58:05.982-07:00Veeam Reporter v.3.0 Enterprise Release<span style=";font-family:arial;font-size:85%"><br />We did it! Veeam Reporter v.3.0 Enterprise for VMware infrastructure 3 has been released today. See <a href="http://www.veeam.com/vmware-esx-reporting_enterprise.html">more...</a><br /><span><br>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7225282167166185672.post-47139634020645040062008-10-21T04:58:00.000-07:002008-10-21T07:57:39.603-07:00Using .NET Enum values in PowerShell<span style=";font-family:arial;font-size:85%;"><br />For using values of enum nested in type and/or namespace of already loaded assembly in PowerShell you would use following statement:</span><br><span style=";font-family:arial;font-size:85%;"><br />[SomeNamespace.EnclosingType+NestedEnum]::EnumValue</span><br><br>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7225282167166185672.post-45860988139007606352008-08-22T05:18:00.000-07:002008-08-22T06:58:34.985-07:00CarlosAg.ExcelXmlWriter: freeze top row<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgc4wd9YWKeyfwu91PJ4hGqAZ2LTCyq7BoMI8mbiNdnm3lh39gsAVw0Kj2Vq8gFXg4tILLVLyeiHlBbnFxZxzlXLz3IpQ_dn-OhdOCt_0B7i-Z0hF-7Hj0lo6DsqYOGtuj6P3oC5Sk8xSs/s1600-h/untitled3.bmp"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 220px; height: 148px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgc4wd9YWKeyfwu91PJ4hGqAZ2LTCyq7BoMI8mbiNdnm3lh39gsAVw0Kj2Vq8gFXg4tILLVLyeiHlBbnFxZxzlXLz3IpQ_dn-OhdOCt_0B7i-Z0hF-7Hj0lo6DsqYOGtuj6P3oC5Sk8xSs/s320/untitled3.bmp" alt="" id="BLOGGER_PHOTO_ID_5237315709870314370" border="0" /></a><span style="font-size:85%;"><span style="font-family:arial;"><br /><br /><br /><br /><br /><br /><br /><br /><br />For freeze top row in Microsoft Excel 2007 worksheet using CarlosAg.ExcelXmlWriter</span></span><span style=";font-family:arial;font-size:85%;" > from </span><span style=";font-family:arial;font-size:85%;" ><a href="http://www.carlosag.net/Tools/ExcelXmlWriter/Default.aspx"><span class="PageTitle">CarlosAg Excel Xml Writer Library</span></a> </span><span style="font-size:85%;"><span style="font-family:arial;">use following code:</span><br /></span><br /><span style="font-size:85%;"><span style="font-family:courier new;">Workbook myWorkBook = new Workbook();</span></span><br /><span style="font-size:85%;"><span style="font-family:courier new;">Worksheet myWorksheet = </span></span><span style="font-size:85%;"><span style="font-family:courier new;">myWorkBook</span></span><span style="font-size:85%;"><span style="font-family:courier new;">.Worksheets.Add("Data");</span></span><br /><span style="font-size:85%;"><span style="font-family:courier new;">myWorksheet</span></span><span style="font-family:courier new;"><span style="font-size:85%;">.Options.Selected = true;</span></span><span style="font-size:85%;"><br /><span style="font-family:courier new;"></span></span><span style="font-size:85%;"><span style="font-family:courier new;">myWorksheet</span></span><span style="font-size:85%;"><span style="font-family:courier new;">.Options.FreezePanes = true;</span><br /><span style="font-family:courier new;"></span></span><span style="font-size:85%;"><span style="font-family:courier new;">myWorksheet</span></span><span style="font-size:85%;"><span style="font-family:courier new;">.Options.SplitHorizontal = 1;</span><br /><span style="font-family:courier new;"></span></span><span style="font-size:85%;"><span style="font-family:courier new;">myWorksheet</span></span><span style="font-size:85%;"><span style="font-family:courier new;">.Options.TopRowBottomPane = 1;</span><br /></span><span style="font-size:85%;"><span style="font-family:courier new;">myWorksheet</span></span><span style="font-size:85%;"><span style="font-family:courier new;">.Options.ActivePane = 2;<br /><br /></span></span><span style=";font-family:arial;font-size:85%;" >Also you can use CarlosAg.ExcelXmlWriter.Generator.exe from <a href="http://www.carlosag.net/Tools/ExcelXmlWriter/Default.aspx"><span class="PageTitle">CarlosAg Excel Xml Writer Library</span></a> for get c# code for Excel </span><span style=";font-family:arial;font-size:85%;" >*.xml files saved in </span><span style="font-size:85%;"><span style="font-family:arial;">XML Spreadsheet format.</span><br /></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7225282167166185672.post-33312926282063150822008-08-19T13:41:00.000-07:002008-08-19T14:20:40.411-07:00Microsoft.Office.Interop.Excel reference isn't resolved<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWZTkvcZuA3e15v_4KFDcK2XsFcVOxKlGvLEg8FuOxzD4hq_-bYxYMbiXEUaI84f-ncFxhJolTz9UOwFGXFjka16okaSh7PEWB5kkbAwOa-CaeK3YIh_WWk7SyRRPsuzGLaMhXlnb3x8E/s1600-h/111.GIF"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWZTkvcZuA3e15v_4KFDcK2XsFcVOxKlGvLEg8FuOxzD4hq_-bYxYMbiXEUaI84f-ncFxhJolTz9UOwFGXFjka16okaSh7PEWB5kkbAwOa-CaeK3YIh_WWk7SyRRPsuzGLaMhXlnb3x8E/s320/111.GIF" alt="" id="BLOGGER_PHOTO_ID_5236337118202299186" border="0" /></a><span style="font-size:85%;"><span style="font-family:arial;">For .net developers which works with MS Office interop, maybe following information would seem useful. Did you happen to face with unresolved Office Interop references?<br />Each time as I saw this strange phenomena I would need some magic to resolve the problem.<br />Mostly the reason hides in conflicting of different asseblies references. In this case usually it's better to remove all unresolved references and add each new assembly refererence step-by-step checking conflict absence.<br /></span></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7225282167166185672.post-48616291285809375732008-08-18T08:10:00.000-07:002008-12-08T01:22:38.217-08:00Types casting safety in .NET<span style="font-size:85%;"><span style="font-family:arial;">Not long ago I clashed with following code:</span></span><br /><br /><span style="font-size:85%;"><span style="font-family:courier new;">namespace Test</span><br /><span style="font-family:courier new;">{</span><br /><span style="font-family:courier new;"> class A</span><br /><span style="font-family:courier new;"> {</span><br /><span style="font-family:courier new;"> public object mVal = new B();</span><br /><span style="font-family:courier new;"> };</span><br /><br /><span style="font-family:courier new;"> class B</span><br /><span style="font-family:courier new;"> {</span><br /><span style="font-family:courier new;"> public string mVal = "ValueB";</span><br /><span style="font-family:courier new;"> };</span><br /><br /><span style="font-family:courier new;"> class Program</span><br /><span style="font-family:courier new;"> {</span><br /><span style="font-family:courier new;"> static void Main(string[] args)</span><br /><span style="font-family:courier new;"> {</span><br /><span style="font-family:courier new;"> A a = new A();</span><br /><span style="font-family:courier new;"> B b = new B();</span><br /><span style="font-family:courier new;"> a.mVal = a.mVal + b.mVal;</span><br /><span style="font-family:courier new;"> }</span><br /><span style="font-family:courier new;"> }</span><br /><span style="font-family:courier new;">}<br /><br /><span style="font-family:arial;">At first I wondered how it was compiled.<br />At second do you mean what value will assigned to </span></span></span><span style="font-size:85%;"><span style="font-family:courier new;"><span style="font-weight: bold;font-family:arial;" >a.mVal</span><span style="font-family:arial;">? </span></span></span><span style="font-size:85%;"><span style="font-family:courier new;"><span style="font-family:arial;">The answer is: "<span style="font-weight: bold;">Test.BValueB</span>". I'll trying to explain what happened in this code. <span style="font-weight: bold;">b.mVal</span> type is 'string' but <span style="font-weight: bold;">a.mVal</span> type is 'object', and types casting problem resolved by C# through </span></span></span><span style="font-size:85%;"><span style="font-family:courier new;"><span style="font-family:arial;">call </span></span></span><span style="font-weight: bold;font-size:85%;" ><span style="font-family:courier new;"><span style="font-family:arial;">a.mVal.ToString()</span></span></span><span style="font-weight: bold;">.</span><span style="font-size:85%;"><span style="font-family:arial;"> I.e</span><span style="font-family:arial;"> we have expression:<br /></span></span><span style="font-size:85%;"><span style="font-family:courier new;"><br /><span style="font-family:courier new;">a.mVal</span></span><span style="font-family:courier new;"> = a.mVal.ToString() + b.mVal</span></span><br /><br /><span style="font-size:85%;"><span style="font-family:courier new;"><span style="font-family:arial;">In other words System.Object casts to System.String implicitly and result string reference assigned to System.Object again. Real trouble is C# compiler doesn't write any errors or warnings about danger of types casting safety.<br />Open question is: "Where is types casting safety in .NET?"<br /><br /></span> </span></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7225282167166185672.post-46097948175052706322008-08-18T02:09:00.000-07:002008-12-08T01:22:48.515-08:00How to get PowerShell current runspace from C#<span style="font-size:85%;"><span style="font-family:arial;">It's possible you faced with situation when you need access to PowerShell current runspace in C# code. For example you need get value of script variable which exists only in runspace of hosting application (i.e. usually poweshell.exe). Of course you can use </span><b style="font-family: arial;">RunspaceFactory.CreateRunspace()</b><span style="font-family:arial;">, but the trouble is that new runspace doesn't contain any script variables and added earlier snap-ins.</span><br /><br /><span style="font-family:arial;">Here is example how to do this.</span></span><br /><br /><span style="font-size:85%;"><span style="font-family:courier new;">Runspace theRunSpace = System.Management.Automation.Runspaces.Runspace.DefaultRunspace;<br /><br />if (theRunSpace.RunspaceStateInfo.State == RunspaceState.Opened)<br />{<br /> string theCommand = "$MyScriptVariable";<br /> using (Pipeline thePipeline = theRunSpace.CreateNestedPipeline(theCommand, true))<br /> {<br /> Collection theRetVal = thePipeline.Invoke();<br /> }<br />}</span><span style="font-weight: bold;font-family:courier new;" ><br /></span></span><br /><span style="font-size:85%;"><span style="font-family:arial;">Note that in shown code </span><b style="font-family: arial;">CreateNestedPipeline()</b></span><span style=";font-family:arial;font-size:85%;" > called instead </span><span style="font-size:85%;"><b style="font-family: arial;">CreatePipeline()</b><span style="font-family:arial;">, because at the moment of call </span><b style="font-family: arial;">CreateNestedPipeline()</b><span style="font-family:arial;"> we stand in executing pipeline already.</span></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7225282167166185672.post-74495664687459149742008-08-18T02:07:00.000-07:002008-08-18T02:13:55.543-07:00Hello World!<span style="font-size:85%;"><span style="font-family: arial;">Hello! This is my first topic.</span><br /><span style="font-family: arial;">Here is I will post some information about programming (predominantly .NET) and related areas: performance tools, coding tools, useful assistants etc.</span></span>Unknownnoreply@blogger.com0