Zayko in the Net

Personal blog of Vitaly Zayko

NAVIGATION - SEARCH

How to convert System.Drawing.Color to a Html representation (C#)?

If you are working on some Web-related App then you sooner or later will need to convert a System.Drawing.Color structure to its Html format. Below are three overloaded functions.

        /// <summary>
/// Convert System.Drawing.Color struct to a Html color representation
/// </summary>
/// <param name="color">System.Drawing.Color struct to convert from</param>
/// <returns>Html color format as a string</returns>
public string ToHtmlColor(Color color)
{
   return "#" + color.ToArgb().ToString("x").Substring(2);
}

/// <summary>
/// Convert Red, Green and Blue components to a Html color representation
/// </summary>
/// <param name="r">The Red component value</param>
/// <param name="g">The Green component value</param>
/// <param name="b">The Blue component value</param>
/// <returns>Html color format as a string</returns>
public string ToHtmlColor(int r, int g, int b)
{
   Color color = System.Drawing.Color.FromArgb(r, g, b);
   return "#" + color.ToArgb().ToString("x").Substring(2);
}

/// <summary>
/// Convert ARGB color to a Html color representation
/// </summary>
/// <param name="argb">ARGB color value</param>
/// <returns>Html color format as a string</returns>
public string ToHtmlColor(int argb)
{
   Color color = System.Drawing.Color.FromArgb(argb);
   return "#" + color.ToArgb().ToString("x").Substring(2);
}

Dispose pattern (C#)

Pretty common pattern: if you are working on a class that utilize either unmanaged resources or managed disposable classes, you need to make your class disposable as well.

    class DisposePatternClass : IDisposable
    {
       /// <summary>
        /// Doesn't allow to dispose same instance twice
        /// </summary>
        private bool disposed = false;

       /// <summary>
        /// Privately cleaning up both managed and unmanaged resources
        /// </summary>
        /// <param name="disposing">True is DisposePatternClass.Dispose() was called; false if disposing by GC</param>
        private void CleanUp(bool disposing)
       {
           if (!this.disposed)
           {
               if (disposing)
               {
                   // Disponse Managed resources here
                }

               // Clean up Unmanaged resources here
            }

           disposed = true;
       }

       /// <summary>
        /// Manual Dispose
        /// </summary>
        public void Dispose()
       {
           CleanUp(true);
           GC.SuppressFinalize(this);
       }

       /// <summary>
        /// Destructor
        /// </summary>
        ~DisposePatternClass()
       {
           CleanUp(false);
       }
   }

Have fun!

How to get Assembly version string (C#)?

First of all: in this post I'm talking about Assembly version, not its file version which is different thing:

Here you go:

/// <summary>
/// Returns version of the current Assembly
/// </summary>
/// <returns>Version string in format x.x.x.x</returns>
private string GetAppVersion()
{
   return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}

Do you know that you can use system variable "|DataDirectory|" to connect to your local SQL database (C#)?

In my last post I shared a function that returns path to current Assembly that could be used in a database connection string. But do you know that you have a shorter way to get this path? Use "|DataDirectory|" in your connection string instead:

 private string GetConnectionString()
 {
     return "Microsoft.SQLSERVER.MOBILE.OLEDB.3.0;Data Source=|DataDirectory|\\Data.sdf";
 }

By default this variable will be expanded as:
  • For a local Application this will be the Assembly (App's .exe) folder;

  • For ClickOnes running Apps it will be a special data folder created by ClickOnes;

  • For ASP.NET App it will be App_Data folder.

Because the |DataDirectory| is a property of App domain, you can change its value by doiung

 AppDomain.CurrentDomain.SetData("DataDirectory", newDataPath);

How to get Assembly path in .NET Compact Framework? (C#)

It's not that easy as in WinForms:

 public string GetAssemblyPath()
 {
     return System.IO.Path.GetDirectoryName(
         System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
 }

Note: last back slash doesn't add!

How to check is your App running under Admin privileges (C#)?

You have to know this before your App does something that requires Administrator privileges. Actually it is quite easy:

using System.Security.Principal; public bool IsAdmin() { WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); return principal.IsInRole(WindowsBuiltInRole.Administrator); }

Where do you want to connect today?

If you are developing database driven Applications, then you definitely need to add this resource to your Internet Favorites: http://www.connectionstrings.com
This is the most complete resource I ever seen that contains connection strings to probably any database system around.
Great job! Thank you, guys!

Do you know how to get a Visual Studio command GUID?

I have met this problem when I worked on some Visual Studio add-ins:
VS Add-in Wizard does all work around to insert your add-in to Visual Studio menu "Tools" but if you want to make your add-in available in other menus, let say in context menu of Solution Explorer, you have to know some additional information about the place.
To get this know you need to add Registry value DWORD EnableVSIPLogging to  HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\General and set it to "1". Then just press Ctrl+Shift and do an operation you interesting in. You will get a dialog box, similar to this:

Don't forget to switch it to "0" (zero) when you will no longer need it because Visual Studio has many commands that start from Ctrl+Shift.
It works in Visual Studio 2005 SP1 or higher.