пятница, 21 ноября 2008 г.

Walking through Enum entries in C#

Sometimes the need appears in make foreach() construction for Enum entries. Here I'll show how to do it.

Type theType = typeof(MyEnum);
MemberInfo[] theMembers = theType.GetMembers();

foreach (MemberInfo entry in theMembers)
if ((entry.MemberType.ToString() == "Field"))
if((entry.Name != "value__"))
MyEnum theTyp = (MyEnum)Enum.Parse(typeof(MyEnum), entry.Name);

As earlier I'm trying to present finished solutions ready to use in your own projects. :)

пятница, 7 ноября 2008 г.

C# numeric format string with fixed length

Do you want to get from 'int' value 'string' representation with fixed length?
Here is solution:) Example:

int iVal = 53;
string strVal = iVal.ToString("0000");



пятница, 24 октября 2008 г.

Create Registry Path in PowerShell

Here is script to create registry path (or separate key) in PowerShell

function vrIsRegistryPathContainsKey($basePath, $key)
if($key.Length -le 0)
{ return 0 }

$childItems = Get-ChildItem $basePath -Force -ErrorAction SilentlyContinue
foreach ($childItem in $childItems)
if($childItem.name -match $key)
return 1
return 0

function vrCreateRegistryPath($basePath, $createdPath)
$pathTokens = $createdPath.split("\")
foreach ($pathToken in $pathTokens)
if($pathToken.Length -le 0)
{ break }

if(-not(vrIsRegistryPathContainsKey $basePath $pathToken))
echo ('not found ' + $pathToken);
New-Item -Path $basePath -Name $pathToken
$basePath = $basePath + "\" + $pathToken
echo ('basepath ' + $basePath);

vrCreateRegistryPath 'Microsoft.PowerShell.Core\Registry::\HKEY_LOCAL_MACHINE\SOFTWARE' 'Key\SubKey1\Subkey2\'

вторник, 21 октября 2008 г.

Veeam Reporter v.3.0 Enterprise Release

We did it! Veeam Reporter v.3.0 Enterprise for VMware infrastructure 3 has been released today. See more...

Using .NET Enum values in PowerShell

For using values of enum nested in type and/or namespace of already loaded assembly in PowerShell you would use following statement:


пятница, 22 августа 2008 г.

CarlosAg.ExcelXmlWriter: freeze top row

For freeze top row in Microsoft Excel 2007 worksheet using CarlosAg.ExcelXmlWriter
from CarlosAg Excel Xml Writer Library use following code:

Workbook myWorkBook = new Workbook();
Worksheet myWorksheet = myWorkBook.Worksheets.Add("Data");
myWorksheet.Options.Selected = true;
myWorksheet.Options.FreezePanes = true;
myWorksheet.Options.SplitHorizontal = 1;
myWorksheet.Options.TopRowBottomPane = 1;
myWorksheet.Options.ActivePane = 2;

Also you can use CarlosAg.ExcelXmlWriter.Generator.exe from CarlosAg Excel Xml Writer Library for get c# code for Excel *.xml files saved in XML Spreadsheet format.

вторник, 19 августа 2008 г.

Microsoft.Office.Interop.Excel reference isn't resolved

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?
Each time as I saw this strange phenomena I would need some magic to resolve the problem.
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.

понедельник, 18 августа 2008 г.

Types casting safety in .NET

Not long ago I clashed with following code:

namespace Test
class A
public object mVal = new B();

class B
public string mVal = "ValueB";

class Program
static void Main(string[] args)
A a = new A();
B b = new B();
a.mVal = a.mVal + b.mVal;

At first I wondered how it was compiled.
At second do you mean what value will assigned to
a.mVal? The answer is: "Test.BValueB". I'll trying to explain what happened in this code. b.mVal type is 'string' but a.mVal type is 'object', and types casting problem resolved by C# through call a.mVal.ToString(). I.e we have expression:

= a.mVal.ToString() + b.mVal

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.
Open question is: "Where is types casting safety in .NET?"

How to get PowerShell current runspace from C#

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 RunspaceFactory.CreateRunspace(), but the trouble is that new runspace doesn't contain any script variables and added earlier snap-ins.

Here is example how to do this.

Runspace theRunSpace = System.Management.Automation.Runspaces.Runspace.DefaultRunspace;

if (theRunSpace.RunspaceStateInfo.State == RunspaceState.Opened)
string theCommand = "$MyScriptVariable";
using (Pipeline thePipeline = theRunSpace.CreateNestedPipeline(theCommand, true))
Collection theRetVal = thePipeline.Invoke();

Note that in shown code CreateNestedPipeline() called instead CreatePipeline(), because at the moment of call CreateNestedPipeline() we stand in executing pipeline already.

Hello World!

Hello! This is my first topic.
Here is I will post some information about programming (predominantly .NET) and related areas: performance tools, coding tools, useful assistants etc.