Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

June 23, 2010

Disable Current Line Highlighting in Visual Studio 2010

After installing the Visual Studio 2010 Pro Power Tools, Current Line Highlighting is enabled. This is alright for some, but it causes frustration when trying to view parenthesis, bracket and brace boundaries. Below is a screenshot of a program with the current line highlighting turned on.

To disable current line highlighting, Navigate to Tools -> Options. In the options dialog box, expand the environment section and click on the Fonts and Colors subsection. The screen contains a list of different types of text in Visual Studio and their associated formatting. Find the Current Line (Extension) Item and change the Item Background from Default to White. This will make the background of the Current Line White.

April 21, 2010

The using Statement - C#

Something that I have thoroughly enjoyed when working more with C# and .NET has been data access with LINQ to SQL. It is a nice mix between using an ORM and making direct changes to a database. One issue remains with using LINQ to SQL is handling the database connection properly. I was recently told to wrap data access code in a using statement for better performance.

The using statement guarantees that resources are properly disposed of after their use. This eliminates the need to remember to close database connections and will dispose of objects wrapped in the statement as soon as they leave scope. Even though the .NET Framework has built in memory management, there is still time between when the objects are no longer needed and when their unneeded memory is released to the rest of the program.

The following is an example of data access using the using statement:
using(_ctx = new SampleDBDataContext()){

var mainQuery = (from c in _ctx.GetTable<SampleTable>()
where c.isMain == true select c);
...
}
The using statement defines the scope of _ctx. So as soon as the database operations are complete, _ctx is trashed.

Here is a more in-depth writeup of using using with data access - http://www.w3enterprises.com/articles/using.aspx

April 18, 2010

Listing Files in a Directory - C#

A portion of my senior design project is a media center component, so I needed an easy way to check the contents of multiple directories. From there, I compare the contents with a database that contains more information about the files.

Retrieving the contents of a directory in C# is very easy, and the code is included below.
DirectoryInfo directory = new DirectoryInfo("C:/some/directory");
FileInfo[] files = directory.GetFiles();
foreach (FileInfo onefile in files){
Console.WriteLine(onefile.Name);
}
The DirectoryInfo object points at a directory on the system. From here, the program has access to create and delete other directories, list directories and files, and view last accessed time information. GetFiles() returns an array of FileInfo objects, and from there, the program has access to information about each file in the directory.