September 2004 - Posts

echo %RANDOM% ... {0, 32767}

I knew there are environment variables for SystemRoot, UserProfile, etc, but I didn't know about the %RANDOM% variable that when used will generate a random number between 0 and 32767.
 
I remember a few years ago I needed to generate a unique name for something in a batch file and I had to use an external program to generate a random number. This can come in handy for generating temporary and/or unique file names.
 
For a complete list of Windows XP Environment variables see Command shell overview.

Run++ Beta 0.7.2.0 released

I have released a new version of Run++
 
If you don't know about Run++ then you can check out this post or the list of features.
 
With this release I also put up a discussion forum to more easily get feedback from the community.
 

How to create a stream from a string in .Net

Yesterday I needed a way to take a string and create a stream from it. I did some searching but I didn't find any clean way. Here what I came up with in C#:

Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes("Test String"));

Does anyone know of any better or the recommended way to do this?

Why doesn't .Net have a string stream class?

Weblogs @ ASP.Net MainFeed...?

I just now noticed that http://weblogs.asp.net/MainFeed.aspx == http://weblogs.asp.netMainFeed.aspx?GroupID=2, meaning that the MainFeed now only has Microsoft Bloggers. Did I miss an announcement about this or is this just a mistake? I was thinking that Weblogs @ ASP.NET has been a little light as of late. I only subscribe to the Main Feed so I have been missing some posts lately.
 
I hope this is only a mistake and will be fixed soon because I get most of my traffic from the MainFeed or Google, I don't think anyone subscribes directly to my blog. Maybe I will need to start following Scoble's advice How your blog will get discovered.
 

First Beta Release of Run++

Well I think I'm finally ready to make my first public beta release of Run++. I wrote this application solely in C# Express Beta 1 and thus using .Net Framework 2.0 Beta 1. It is going to be available for free and eventually I will release the source code as well. The feature list is list below and if you are interested you can download it here. I would appreciate any feedback (good or bad). Enjoy!

Run++
... An replacement run command ...

 
Run++ is a replacement for the windows run command. Currently the windows run command is very limited it only provides a drop down list of the last 26 typed commands and a auto complete functionality for the file system and browser history as described hereRun++ provides the following feature set:

Features

Auto Complete

Recognizes Commands

Create Custom Commands

Pre-Loaded List of Commands

File System Path Auto Complete

Side Features

Motivation:
This project was inspired by my over use of the windows run command and also my hopes to familiarize myself with the new features in .NET and C# version 2.0. It was developed using C# Express Beta 1.

Tooltip now has the ability to be displayed as a Balloon

While adding tooltips to the application I'm working on in C# Express (more on that later)  I noticed the property IsBalloon and though hmmmm. So I set it true and now my tooltips display as balloons instead of the normal rectangular popups. There are a few more changes to the ToolTip Members. Two others properties I think are helpful are ToolTipTitle and ToolTipIcon (ie Info, Warning, Error).
 
Also on a side note when getting the links from the MSDN beta documentation for .Net 2.0, I noticed that the site is different. Does anyone know what is with the change to the MSDN beta documentation?

Copernic Desktop Search

Copernic Desktop Search - They claim to be "The Search Engine for your PC".
 
I just installed it and it is indexing my files right now, we'll see if it is as good as they say. The first thing I see that I don't like about it is their Web Search, it uses http://www.alltheweb.com with no option to change it, that I can find, however I will over look this since that is not its main purpose.

Using C# Generics on Static Classes

I was playing around with some C# generics today and I began thinking about different things I could do with them. Then I began to wonder what happens if you give a static class generic type parameters? I wasn't even sure it would compile but sure enough it did. Then I tried to come up with an example where this might come in handy. Here is a simple factory pattern example using a C# generics:

static void Main(string[] args)
{
    IFactory item = Factory<IFactory, A>.Create(); // Outputs 1
    Console.WriteLine(item.Name);                  // Outputs A
    item = Factory<IFactory, B>.Create();          // Outputs 1
    Console.WriteLine(item.Name);                  // Outputs B
    item = Factory<IFactory, B>.Create();          // Outputs 2
    Console.WriteLine(item.Name);                  // Outputs B
}
public static class Factory<F,T> where T : F, new()
{
    static int count = 0;
    public static F Create()
    {
        count++;
        Console.WriteLine(count);
        return new T();
    }
}
public interface IFactory
{
    string Name { get; }
}
public class A : IFactory
{
    public string Name { get { return "A"; } }
}
public class B : IFactory
{
    public string Name { get { return "B"; } }
}

Now if you take a look at the count variable why is it 1 after I create the first B? Why is it not 2?

The answer is simple once you think about it. For every combination of generic parameters that are used a completely different class is created for that combination. So every combination has its own class thus its own static space, and therefore its own count variable in this example.

This is not a very interesting example but it taught me that generic types can be used by static members as well as static classes. Even deeper you come to realize that for every combination of generic parameters a new class is literally created for each combination.

I hope this will save a few people a couple hours of debugging time. ;)

How to store secrets on a machine... Using the new ProtectData class in the .Net Framework 2.0

If you haven't read How to store secrets on a machine from Keith Brown's book-in-a-wiki, you should it is a great read. It talks about how to use the DPAPI to store data locally on a machine. Here is an excerpt from it about the ProtectedData class provided in .Net 2.0:

The DataProtection class

Version 2.0 of the .NET Framework introduces a class called DataProtection that wraps DPAPI. It's simple to use; in fact, it looks almost exactly like the wrapper class I provided above. I've shown an example in figure 70.2.

using System; 
using System.Text;
using System.Security.Cryptography;
class Program
{
   const string applicationEntropy = "Some application secret";
   static void Main()
   {
      string secret = "Attack at dawn";
      Console.WriteLine("Encrypting: {0}", secret);
      string base64Ciphertext = Encrypt(secret);
      Console.WriteLine("Decrypting: {0}", base64Ciphertext);
      Console.WriteLine("Result: {0}", Decrypt(base64Ciphertext));
   }
   static string Encrypt(string plaintext)
   {
      byte[] encodedPlaintext = Encoding.UTF8.GetBytes(plaintext);
      byte[] encodedEntropy = Encoding.UTF8.GetBytes(applicationEntropy);

      byte[] ciphertext = ProtectedData.Protect(encodedPlaintext,
         encodedEntropy, DataProtectionScope.LocalMachine);
      return Convert.ToBase64String(ciphertext);
   }
   static string Decrypt(string base64Ciphertext)
   {
      byte[] ciphertext = Convert.FromBase64String(base64Ciphertext);
      byte[] encodedEntropy = Encoding.UTF8.GetBytes(applicationEntropy);


      
      byte[] encodedPlaintext = ProtectedData.Unprotect(ciphertext,
         encodedEntropy, DataProtectionScope.LocalMachine);
      return Encoding.UTF8.GetString(encodedPlaintext);
   }
}
Figure 70.2: Wrapping DPAPI in Managed C#
Posted by puzzlehacker | with no comments

Standard Windows Password Character

I was writing a login form for an application I'm working on and I decided I didn't want to use the standard '*' as my password character. I wanted to use the black circle (●), the same one that windows login uses. I figured out what the Unicode value for it was (0x25CF) and then I tried to set that as the password character for my textbox through the designer but no luck it would only take standard ASCII characters. So I decided to set in my form constructor, like:

this.txtPassword.PasswordChar = '\u25CF';

Which worked so now when the user types in their password the see '●' as opposed to '*'. I just figured I would pass this along incase anyone else wants to do the same. I don't know if the same kind of thing will work in ASP.NET or not but it would be cool to find out.

UPDATE: It appears that the new textbox in .NET 2.0 contains a property UseSystemPasswordChar, that when set will make it use the system password character and literally treat it as a standard windows password textbox. Thanks to David Kean for pointint this out.

More Posts