How to check you are running as administrator using C#
If you are developing applications for Vista+, you require Administrative permissions to access system resources like FileSystem, Registry etc. Already I posted Create UAC Compatible applications in .NET. The following code will help to verify and execute code which can be run by administrator only.
private bool IsCurrentlyRunningAsAdmin()
{
bool isAdmin = false;
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
if (currentIdentity != null)
{
WindowsPrincipal pricipal = new WindowsPrincipal(currentIdentity);
isAdmin = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
pricipal = null;
}
return isAdmin;
}
Quick introduction to SQLite with C#
Recently I am started working with SQLite Database. You can find more information about SQLite Database from the website (http://www.sqlite.org/).
SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is in the public domain. – From SQLite website.
You can download the System.Data.Sqlite from system.data.sqlite.org website.
Here is a sample code snippet which connects to a SQLite DB and prints all the objects in the SQLite database.
string dbName = @"C:\Sample.db3";
using (SQLiteConnection sqlConnection =
new SQLiteConnection("Data Source=" + dbName + ";Version=3;New=True;Compress=True;"))
{
using (SQLiteCommand sqlCommand = sqlConnection.CreateCommand())
{
sqlConnection.Open();
sqlCommand.CommandText = "SELECT * FROM sqlite_master";
using (SQLiteDataReader dataReader =
sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
Console.WriteLine("Name : Type");
while (dataReader.Read())
{
Console.WriteLine(
dataReader["Name"].ToString() + ":" + dataReader["Type"].ToString());
}
}
}
}
Don’t forgot to Add reference of System.Data.SQLite.dll in the project.
How to store TFS credentials
In my machine the build notification tool was in system startup, and I am connected to two TFS Servers, currently team explorer / source control explorer / build notification tool doesn’t support remember password option. Because of this, I had to give the user credentials for TFS every time (My windows credentials are different from TFS credentials). Today I found a solution, which helps to store the TFS credentials in local machine; which helps to avoid the login dialog from Build notification tool as well as from the team explorer and source control explorer windows.
In Windows Vista:
- Go to the User Accounts Control Panel > Manage Your Network Passwords
- Click Add, Enter TFS server, User name and password.
In Windows XP:
- Go to Start > Settings > Control Panel > User Accounts > Advanced > Manage Passwords > Add
- Enter TFS server name, user name and password
How to associate a File Type to your application
Recently I was working with an Open source project which helps to manage SQLCE databases. Yesterday I thought of implementing a feature like associating the SDF files to my application, so that I can double click on any SDF file which will open the launch my application and opens the File in it.
Caution: Incorrectly editing the registry may severely damage your system. Back up the current version of the registry before making any changes. You should also back up any valued data on the computer.
I found various ways to do it, but the easy way I found I am sharing.
Dim SDF As RegistryKey = My.Computer.Registry.ClassesRoot. _
CreateSubKey(".sdx")
SDF.SetValue("", "SDFUtility", RegistryValueKind.String)
Dim OpenCommand As RegistryKey =
My.Computer.Registry.ClassesRoot.CreateSubKey _
("SDFUtility\Shell\Explore with SDFUtility\Command")
OpenCommand.SetValue("", String.Format("{0} ""%1""", appPath), _
Microsoft.Win32.RegistryValueKind.String)
And here is the screenshot
You can remove the association by reseting the sdf key’s value. And you can delete all the keys by calling a DeleteSubKeyTree method on the SDFUtility key to remove all the keys associated with it.
SDF = My.Computer.Registry.ClassesRoot.OpenSubKey(".sdf", True)
SDF.SetValue("", "Microsoft SQL Server Compact Edition Database File", _
RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.DeleteSubKeyTree("SDFUtility")
You need be Administrator do this operation, otherwise you may get an exception in Windows Vista+.



