Truncate File Path with Ellipsis

17 Comments »

The Microsoft .NET Framework is quite comprehensive, but occasionally an obvious function slips through the cracks and you have to use InteropServices to access the Windows API.

One such obvious miss is the ability to truncate a file path. If you are drawing text and know the font and desired output size, you can use the WinForms TextRenderer class. But to truncate a file path to a specific number of characters, you need the “Shell Lightweight Utility Library” function PathCompactPathEx:

Read the rest of this entry »

Determine the .NET Versions on which an Application is Compiled and Running

No Comments »

The version of .NET against which you compile an application or assembly may not be the same version of .NET on which the application is currently running. A .NET application should always be able to run on the same or newer version of .NET against which it was compiled.

This is because .NET is backward compatible. This means that an application compiled on .NET v1.1 should run OK on .NET v2.0 and v3.0. But an application compiled on .NET v2.0 will not run on .NET v1.1.

Read the rest of this entry »

Determine Installed .NET Versions from a Web Page

4 Comments »

You can use the following JavaScript code in a web page to determine which versions of .NET are installed on a client PC:

Read the rest of this entry »

Change Font Style

13 Comments »

Changing a font style is a bit easier than changing its size, as there is a Font constructor that accepts a font and style as arguments. For example, to bold a label’s font:

Read the rest of this entry »

Change Font Size

16 Comments »

An inspection of the Font class will reveal that every public property is read-only. This means to change a font’s size, you need to create a new Font object with all the same properties of your current font but with the new size. Here is a handy method to do just that:

Read the rest of this entry »

Hide Form from Alt+Tab

33 Comments »

When you show a .NET Form, by default the form will appear in the Windows Start bar and in the list of open windows shown when the user presses Alt+Tab.

Read the rest of this entry »

List Drives and Volumes from .NET

2 Comments »

The .NET Framework v2.0 did a nice job filling many holes in the System.IO namespace, especially when it comes to managing the file system. One such addition is the DriveInfo class, which enables you to determine what drives are available, their type, capacity and available free space.

Read the rest of this entry »

Determine if a Loaded .NET Assembly is Signed

9 Comments »

A .NET assembly is “signed” if the developer compiled the assembly with the private key of a digital signature. When the system later loads the assembly, it verifies the assembly with the corresponding public key. Occasionally you may need to determine whether an assembly you have loaded has been signed.

Read the rest of this entry »

Lighten and Darken Colors in .NET

7 Comments »

There is a very handy .NET class called ControlPaint in the System.Windows.Forms namespace that enables you to draw your own controls and control elements using the standard Windows style and theme. Buried in this rich class are four methods that enable you to lighten and darken colors:

Read the rest of this entry »

C# GetPixel and SetPixel

13 Comments »

It’s hard to believe the comprehensive .NET framework would omit such obvious functions as GetPixel and SetPixel from its Drawing library. Fortunately, we can access the GDI functions using Interop, as shown below. Notice the conversion required between the COLORREF integer used by the GDI methods and the Color structure used by our static .NET methods.

Read the rest of this entry »

« go backkeep looking »