Zayko in the Net

Personal blog of Vitaly Zayko

NAVIGATION - SEARCH

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.

Inverting colors (C#)

Sometimes, when you change, for example, background color of an object dynamically, you should dynamically change foreground color as well to keep a text readable. Here is very simple way to do it:

public Color InvertColor(Color color){
    return Color.FromArgb(color.ToArgb() ^ 0xFFFFFF);
}

First Blog Post

Hi and welcome to Vitaly Zayko Blog!

My name is Vitaly Zayko and I'm Software Developer forlast 15 years. I created this blog to post some useful code snippets that hardto remember especially when use them time to time. These snippets will be mostlyrelated to Microsoft technologies such as C#, WPF, Silverlight, ASP.NET and SQL.

I going to start posting very soon. So please come back!

Greetings!

Vitaly Zayko