February 24, 2006

SNMP in .NET

Useful links for reading about SNMP in a .Net context

System.Net.IPAddress Tests

string noadd = "0.0.0.0";
System.Net.IPAddress ipaddr = System.Net.IPAddress.Parse(noadd);
ipaddr = System.Net.IPAddress.None;
string tmp = System.Net.IPAddress.None.ToString(); // produces "255.255.255.255"
tmp = System.Net.IPAddress.Loopback.ToString(); // produces "127.0.0.1"
tmp = System.Net.IPAddress.Any.ToString();      // produces "0.0.0.0"
tmp = System.Net.IPAddress.Broadcast.ToString(); // produces "255.255.255.255"

ipaddr = System.Net.IPAddress.Parse("");
tmp = ipaddr.ToString(); // produces "0.0.0.0"
ipaddr = System.Net.IPAddress.Parse("0");
tmp = ipaddr.ToString(); // produces "0.0.0.0"
ipaddr = System.Net.IPAddress.Parse("0.0");
tmp = ipaddr.ToString(); // produces "0.0.0.0"
ipaddr = System.Net.IPAddress.Parse("0.0.0");
tmp = ipaddr.ToString(); // produces "0.0.0.0"
ipaddr = System.Net.IPAddress.Parse("0.0.0.0");
tmp = ipaddr.ToString(); // produces "0.0.0.0"

February 13, 2006

Executable Path of the Application

string exeDirPath = System.IO.Path.GetDirectoryName(
          System.Reflection.Assembly.GetExecutingAssembly())
or for the full path of the executable:
string exePath = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
or just (only for Windows Forms application)
System.Windows.Forms.Application.ExecutablePath - Executable path of the application in which the assembly is housed:
string exePath = System.Windows.Forms.Application.ExecutablePath;
or
string[] args = Environment.GetCommandLineArgs();
string exePath = args[0];
To get the working directory where the executable lies (but this does not work for a service or so it seems):
string exePath = System.Windows.Forms.Application.StartupPath; 
or alternatively
string cwd = Environment.CurrentDirectory;
Here is the sample output of these for a test application. The application was started in the "D:\Documents" directory but located at "D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug\IdeaTester.exe"
First the list of methods used:
1.  System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()
2.  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
3.  System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssem
bly().GetName().CodeBase)
4.  System.IO.Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetN
ame().CodeBase).LocalPath)
5.  System.IO.Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetN
ame().CodeBase).AbsolutePath)
6.  System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutableP
ath)
7.  System.IO.Path.GetDirectoryName(args[0])
8.  System.Windows.Forms.Application.StartupPath
9.  Environment.CurrentDirectory
10. System.Windows.Forms.Application.ExecutablePath
Here is the output of each method:
1.  IdeaTester, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
2.  file:///D:/Documents/Devel/Tools/IdeaTester/bin/x86/Debug/IdeaTester.exe
3.  file:\D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug
4.  D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug
5.  D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug
6.  D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug
7.  D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug
8.  D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug
9.  D:\Documents
10. D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug\IdeaTester.exe



c# Code for Reading MP3s

C# Code for reading and Parsing MP3 files C# Shell ID3 Tag Reader

.Net Remoting Useful Links

About .NET Remoting: Three concepts Compared and Simplified Design and Develop Seamless Distributed Applications for the Common Language Runtime Remoting with .NET Description

Use ServiceController class to stop and start services.

Use ServiceController class to stop and start services. This is useful within NUnit tests.
StartService(string XXXServiceName)
{
  serviceController 
    = new System.ServiceProcess.ServiceController(XXXServiceName);

  log.Info(XXXServiceName 
    + " service status is currently set to " 
    + serviceController.Status.ToString());

  if  ((serviceController.Status.Equals(
        System.ServiceProcess.ServiceControllerStatus.Stopped)) ||
       (serviceController.Status.Equals(
        System.ServiceProcess.ServiceControllerStatus.StopPending)))
  {
    serviceController.Start();
    serviceController.Refresh();
    log.Info(ConfiguratorManagerServiceName 
      + " service status is currently set to " 
      + serviceController.Status.ToString());
  }
}

StopService(string XXXServiceName)
{
  serviceController.Stop();
  serviceController.  
  log.Info(XXXServiceName 
    + " service status is currently set to " 
    + serviceController.Status.ToString());
}

XslCompiledTransform

New replacement for XslTransform in .NET version 2.0 is XslCompiledTransform. This is apparently faster. Here is a sample to prepare it:
private XslCompiledTransform xctCached = null;

private XslCompiledTransform GetXsltTransform()
{
  if (this.xctCached == null)
  {
    // Retrieve the xslt
    string xslt = RetrieveEmbeddedStringResource(
        "ViewNUnitTestResults.Resources.NUnitTestResultsToHtml.xsl");
    XmlTextReader xsltReader = new XmlTextReader(new
        StringReader(xslt));                        
    // Create the compiled xslt transform
    this.xctCached = new XslCompiledTransform();
    this.xctCached.Load(xsltReader);
  }
  return this.xctCached;
}
and then use it:
public string ApplyXslTransform(
    string sourceXml, XslCompiledTransform xct)
{
  // read XML
  XmlTextReader xmlReader = new XmlTextReader(
    new StringReader(sourceXml));

  //create the output stream
  StringBuilder sb = new StringBuilder();
  using (StringWriter outputWriter = new StringWriter(sb))
  {
    // Do the transform
    XsltArgumentList args = new XsltArgumentList();
    xct.Transform(xmlReader, args, outputWriter);
  }

  return sb.ToString();
}
You can still use the XslTransform class though:
#region Using XslTransform

public string ApplyXslFromManifest(string sourceXml)
{
    XslTransform xslt = GetXslTransform();
    return ApplyXslTransform(sourceXml, xslt);
}

private XslTransform xtCached = null;

private XslTransform GetXslTransform()
{
    if (this.xtCached == null)
    {
        string xsltString = RetrieveEmbeddedStringResource(
            "ViewNUnitTestResults.Resources.NUnitTestResultsToHtml.xsl");
        XmlTextReader xsltReader = new XmlTextReader(new 
            StringReader(xsltString));
        this.xtCached = new XslTransform();
        this.xtCached.Load(xsltReader);
    }
    return this.xtCached;
}

public static string ApplyXslTransform(
    string sourceXml, XslTransform xslt)
{
    // read XML
    XmlTextReader xmlReader = new XmlTextReader(new
    StringReader(sourceXml));

    //create the output stream
    StringBuilder sb = new StringBuilder();
    TextWriter outputWriter = new StringWriter(sb);

    // Transform the Xml
    XPathDocument xPathDocument = new XPathDocument(xmlReader);
    xslt.Transform(xPathDocument, null, outputWriter, null);

    //get result
    return sb.ToString();
}

#endregion Using XslTransform
Althouh the documentation says it is obsolete, I think if the xslt is only going to be used once only it would be slightly faster to use the XslTransform class. The compiled transform is best when the compiled xslt object is going to be used multiple times, see the article "Properly Utilizing XslCompiledTransform". Check this article on a thorough analysis of the 2 different XSLT transforms in .NET, their speed differences and some recommendations on when to use which.

Using 'ArrayList.ToArray()' method

PKMuxlet[] mIds = (PKMuxlet[])mIdList.ToArray(typeof(PKMuxlet));