
Back to Blog index.
Sometimes, you need to write to the system registry with .NET, too. Luckily, there's the convenient Microsoft.Win32.RegistryKey class to help you.
This class has a SetValue method, but if you are not careful how you open the registry key you want to write to, you might get the following error message:
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll. Additional information: Cannot write to the registry key.
Why did that happen? It happened because by default, a registry key gets opened with read-only permissions.
It is easy to overlook the second parameter of the OpenSubKey method. Unless you set the second (default) parameters to true, you will get the above error message.
The following code will demonstrate the correct way to do it:
Microsoft.Win32.RegistryKey key =
Registry.CurrentUser.OpenSubKey("Software\\ACME",true);
int myValue = 123;
key.SetValue("My Value",myValue);
key.Close();
Not a complicated example, is it? Just call OpenSubKey the right way, and it will work out okay.
Wouldn't it be convenient if you could dynamically create object instances given an object type? In C#, we don't have class references like Delphi would, but there's an easy remedy.
Take a look at this piece of code:
private object UniversalClassCreator(System.Type typeToCreate)
{
System.Reflection.ConstructorInfo ci;
ci = typeToCreate.GetConstructor(System.Type.EmptyTypes);
object newObj = ci.Invoke(null);
return newObj;
}
Okay, that was easy. But do you want to make things still easier? This is as simple as it can possibly get:
object newObj = System.Activator.CreateInstance(typeToCreate);
Sometimes, you need to know the version of a given .NET assembly. For instance, if your application loads a assembly complied into DLL, you might wish to check its version before starting to use it.
Now, to set the version of an assembly, you would use the System.Reflection.AssemblyVersionAttribute class like this:
[assembly: AssemblyVersion("0.9.*")]
Then, in the assembly itself (or another, just as you choose) you can use the following code to return a version string:
public static string GetEngineVersion()
{
System.Reflection.Assembly thisDLL =
Assembly.GetExecutingAssembly();
return thisDLL.GetName().Version.ToString();
}
So, the key is to get the currently executing assembly using the System.Reflection.Assembly class, and then use the GetName() method to get the System.Reflection.AssemblyName class. This class has a Version property, which is all you need.
Assume you have a .NET Windows Forms application and you want to display the results of a SQL query on a data grid (System.Windows.Forms.DataGrid). This is easy with C# or Delphi once you know how to use those connection, command and data adapter components. But, soon you will want to edit the columns of the data grid. Unlike in the ASP.NET world, there isn't a Columns property that you could simply modify. What to do?
Initially, a data grid doesn't contain any columns. But once you assign a data source to the DataSource property of the grid, the default columns (based on the data source) get generated. How would you change for example the widths (or other properties) of these columns?
The solution can be found by examining the data grid table styles. The WinForms DataGrid component has a public property called TableStyles, which is a collection of assigned styles to the grid. The objects in the collection in turn have a GridColumnStyles property, which is again a collection object. This is how you access the column objects.
But take note. Unless you create a table style manually to your grid, you cannot access the default columns. To create this style, you can either write code, or use the table style editor available in both Visual Studio .NET and Delphi.
When you create a default style for the grid, you don't need to generate any columns -- these will be generated automatically when you assign a data source to the grid ("bind the grid"). After binding the grid to a data source, you could simply set the column widths with code like this:
DefaultDataGridTableStyle.GridColumnStyles[0].Width := 65; { customer no }
DefaultDataGridTableStyle.GridColumnStyles[2].Width := 300; { name }
DefaultDataGridTableStyle.GridColumnStyles[3].Width := 70; { city }
Here, DefaultDataGridTableStyle is a class of type System.Windows.Forms.DataGridTableStyle. It is generated something like this when you use the designer:
Self.DefaultDataGridTableStyle.DataGrid := Self.DataGrid1; Self.DefaultDataGridTableStyle.HeaderForeColor := System.Drawing.SystemColors.ControlText; Self.DefaultDataGridTableStyle.MappingName := 'Customers'; Self.DefaultDataGridTableStyle.PreferredColumnWidth := 60;
Note the setting of the MappingName property. This is an important property, and must match the data table name of the underlying DataSet. For a DataSet, the DataMember property of the grid is the name of the internal DataTable you want to show. However if you bind the grid to a DataTable or a DataView, the DataMember property is an empty string. Even so, the MappingName property of the table style must still match the original name of the table inside the DataSet. Otherwise, the above code won't work.
First it was eWeek, and now Microsoft has finally announced the new name of the forthcoming Windows version: it is called Windows Vista.
It is a good name in my opinion. But then again, it could still change. But this is what we have today, and formely it was codename "Longhorn".
Sometimes, traffic signs can be very amusing. They are there to serve a duty, but sometimes I wonder who decided to put the signs where they are.
University of Wisconsin has some for you. And if you are a Finn, then you might find Matti Gröönroos' web site entertaining.
As I've been playing with Visual Studio .NET lately, I'm interested in learning better those keyboard commands I'm used to with Delphi. For example, to delete a line in Delphi, you would press Ctrl+Y. But in Visual Studio the same is Ctrl+Shift+L.
Naturally, there are also other tips when it comes to the (VS) code editor, and there's a short text about those in MSDN. It is a transcript of a video, but it is lot faster to find the tips from the text than from the video.
If you are instead interested in a full reference, then one is available on MSDN beta.
Seems like Yahoo's DomainKeys has gotten a highlight as the spec has been submitted to IETF.
eWeek has a report about this. It was maybe about 2-3 years ago when I briefly studied DomainKeys, and to me it looked fine. Now it is very good to hear that Microsoft is also backing this standard.
It could well be that we are getting some progress in e-mail authentication now.
Not that many newspieces are available as I'm writing this, but Santa Cruz Sentinel has one at least.
The interesting thing is that "Fuller will continue to serve on Borland's board of directors." But how long, they question is.
At this time, it is too early to predict anything except further cost savings. What that means, time will tell.
Thanks to JP for finding out about this first and letting me know.
Bits and pieces of information flow to us from the Longhorn development team, and it seems RSS is one of those technologies that gets an elevation in that release.
Channel 9 has a blog post regarding RSS support in the platform, and they have also made a one-hour (unedited) video regarding it.
The ideas that the team has put into the operating system and IE 7 are good, but in my opinion, they are not revolutionary, instead evolutionary. But nonetheless, that is progress.
Oh, if you want to save time with the video, and are just interested in the demos, look at these time periods first:
I needed to do some GDI+ drawing with C#, and needed to specify colors. The System.Drawing.Color structure has many ready-selected colors which can be accessed by name.
But the problem was, how do you know how the colors "Chartreuse" or "Honeydew" actually look like?
The SDK documentation doesn't have a color table, but luckily it was easy to find such a reference on the 'net. OpinionatedGeek has it.
› Blog Archive