by Wojciech Sura

Custom code snippets in Visual Studio

Visual Studio’s editor provides quite useful feature: code snippets. Code snippets are pieces of frequently used code with – optionally – a few blank fields to fill in.

It turns out, that one may create his own code snippets and import them into Visual Studio. I’ll show you, how can you do it.

First of all, create a new file and name it, for instance, SerializedClass.snippet . Then open it in your favorite text editor and let’s start writing.

[xml]<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>XML serializable class</Title>
<Author>Spook</Author>
<Description>Creates an XML serializable class with constructor.</Description>
<Shortcut>xclass</Shortcut>
</Header>[/xml]

So far, everything should be self-explanatory. Now, before entering snippet’s source code, we may define some literals, which will be later replaced by values entered by user.

[xml] <Snippet>
<Declarations>
<Literal>
<ID>ClassName</ID>
<ToolTip>Name of the class</ToolTip>
<Default>MyClass</Default>
</Literal>
</Declarations>[/xml]

ID specifies name, which will be used in snippet’s code. To inform, that piece of snippet is a literal, you have to surround it with dolar-signs: $ClassName$.

[xml] <Code Language="CSharp">
<![CDATA[ [XmlRoot("$ClassName$")]
public class $ClassName$
{
public $ClassName$()
{

}
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>[/xml]

Ok, now we have to import the snippet to Visual Studio. Start the IDE and choose Tools | Code Snippets Manager…. Then use the Import button to import new snippet to the IDE. The snippet will be copied to %USERPROFILE%\Documents\Visual Studio 2013\Code Snippets\Visual C#\My Code Snippets (assuming, that you use Visual Studio 2013, of course).

TooltipManager

When the snippet is imported, it immediately is available. Simply write “xclass”:

snippet_code

And then press Tab key twice. Voila!

snippet_inserted

Read more about custom code snippets in the MSDN library.

by Wojciech Sura

Visual Studio 2013 productivity tips

There are a few things you may not know, which can significantly boost your productivity in Visual Studio 2013.

You may access items named in PascalCase by simply writing word initials, like IOE for InvalidOperationException or IOException.

Capitals

The quick way to open context menu on suggested change (like adding a class, method, renaming identifier etc.) is Ctrl+. (Ctrl + dot). If you remember, which item is default (such as renaming identifier), performing desired refactoring is as quick as pressing Ctrl+., Enter.

Shortcut

You can very quickly search for Visual Studio settings in the top-right field accessible quickly by Ctrl+Q.

Search settings

Visual Studio supports so called progressive search feature. Visually it looks almost identically to the Find dialog, but that dialog is closed immediately, when you finish searching (for instance, when you press the arrow key). Also, it does not fill the input box with what is currently under the cursor – instead it waits until you start typing. Shortcut for the progressive search is Ctrl+I. If you want to search for next occurrence, press F3.

Progressive search dialog:

Progressive search

Regular search dialog:

Regular search

There’s also another type of search, which seeks through all symbols and filenames in the solution. The shortcut for this one is Ctrl+, (Ctrl + comma).

SearchForSymbol

If you work with a huge solution, you may narrow the view of Solution Explorer to specific branch by choosing “Scope to this” from the context menu. Use the home icon to return to the solution view again.

Solution explorer

There are also a few shortcut chords used more frequently than the others:

  • Ctrl+K, Ctrl+F – Auto-format selection
  • Ctrl+M, Ctrl+L – Fold all / Unfold all
  • Ctrl+M, Ctrl+M – Fold current block
  • Ctrl+M, Ctrl+O – Fold to definitions
  • Ctrl+K, Ctrl+K – Set bookmark in place of cursor
  • Ctrl+K, Ctrl+N – (like “Next”) – jump to next bookmark in the code
  • Ctrl+K, Ctrl+P – (like “Previous”) – jump to previous bookmark in the code
by Wojciech Sura

Advanced regular expressions

The man, who invented regular expressions surely should get the Nobel prize. I lost track of how many tasks I have completed a lot faster thanks to this useful feature built in most of text editors.

Today I used a modern extension of regular expressions called negative lookbehind. Let me tell you what it is and how can you use it to your advantage.

Modern editors (including Visual Studio) supports the following regular expression syntax:

  • expr2(?=expr1)
  • expr2(?!expr1)
  • (?<=expr1)expr2
  • (?<!expr1)epxr2

In order, they are:

  • Positive lookahead – matches expr2 if it is immediately followed by expr1.
  • Negative lookahead – matches expr2 if it is not immediately followed by expr1.
  • Positive lookbehind – matches expr2 if it is immediately preceeded by expr1.
  • Negative lookbehind – matches expr2 if it is not immediately preceeded by expr1.

What is most important is the fact, that in each case expr1 does not become part of the match.

What did I use it for today? I needed to clean up part of the HTML – I got a lot of HTML tags in single line and I wanted to break them up automatically. I used the following patterns:

  • Find: (?<![\n\r])<(?!/)
  • Replace: \r\n<

This regular expression finds all “<” characters, which are not preceeded by a newline and not followed by “/” character and adds a newline before them.

Then it was only a matter of pressing Ctrl+K, Ctrl+F, such that Visual Studio formatted the whole HTML for me automatically.

by Wojciech Sura

NTFS Reparse points

Linux ext2 and ext3 file systems supports a very useful feature: hardlinks and symlinks. It is quite uncommon knowledge, that for quite a very long time (as far as Windows XP) NTFS file system had both these features implemented (using special feature called NTFS reparse points, available from NTFS 3.0) and ready to use – except that there is no GUI utility to do so. Fortunately, recent Windows versions provide convenient console command: mklink.

Let’s start the console with administrative rights and make some experiments.

[shell]D:\Temporary\Hardlinks>dir /b
file.txt

D:\Temporary\Hardlinks>mklink /H file2.txt file.txt
Hardlink created for file2.txt <<===>> file.txt[/shell]
What happened? Let’s think of a file in terms of its contents. The file’s contents are placed somewhere on the disk and NTFS index contains entry called file.txt pointing to these contents. Now we have created another entry – file2.txt, which points to the same contents, as file.txt. Effectively now we have one file, which resides in two places at once!

Let’s check, if it is true.
[shell]D:\Temporary\Hardlinks>dir /b
file.txt
file2.txt

D:\Temporary\Hardlinks>type file.txt
Alice has a cat
D:\Temporary\Hardlinks>echo and dog >> file2.txt

D:\Temporary\Hardlinks>type file.txt
Alice has a cat and dog[/shell]
We modified the file2.txt, but contents of file.txt changed as well. So indeed, file.txt and file2.txt are actually the same file!

Now what happens, if we delete one of these files? Well, one entry in the NTFS index is deleted, but since there’s another one pointing to the actual data, file still exists. Only removing last existing NTFS entry for a file will result in actual deletion.

Now what’s the difference between hardlink and symbolic link? Well, the symbolic link does not point to the data, but to a specific path, so it works a little like a Windows shortcut. Let’s make one and observe, how it behaves.
[shell]D:\Temporary\Hardlinks>mklink symfile.txt file.txt
symbolic link created for symfile.txt <<===>> file.txt[/shell]
The first difference can be seen, when we simply list folder’s contents:
[shell]D:\Temporary\Hardlinks>dir
Volume in drive D is Dokumenty
Volume Serial Number is ECD9-AAF9

Directory of D:\Temporary\Hardlinks

2014-09-16 07:34 <DIR> .
2014-09-16 07:34 <DIR> ..
2014-09-16 07:27 16 file.txt
2014-09-16 07:29 <SYMLINK> symfile.txt [file.txt]
2 File(s) 16 bytes
2 Dir(s) 134 664 282 112 bytes free[/shell]
Windows Explorer also knows, that this is not the actual file, but merely a link to a file.

Symlink-explorer

So what’s the difference between a shortcut and symlink? They are very lookalike.

Shortcut is a file, which contents describe some location on the computer and has to be interpreted by the operating system to work properly. Symlink behaves as a regular file (you may open it in Notepad etc.), but it is actually a NTFS entry pointing to another file on the file system level. This means, that if you try to examine symlink’s contents, you’ll actually see the target file’s ones.

There’s another difference between symlink and hardlink. If you create symlink to a file and then delete that file, symlink will remain intact, but will point to no longer existent object. This will cause errors, if you try to open it:
[shell]D:\Temporary\Hardlinks>del file.txt

D:\Temporary\Hardlinks>type symfile.txt
The system cannot find the file specified.[/shell]
Hardlinks can be created only withing the same NTFS volume. Symlinks, on the other hand, can be created cross-volumes.

by Wojciech Sura

How to diagnose your Samsung phone

Most of smartphone owners mostly probably already heard about service codes (“diagnostic”, “magic” etc.) for their phones. Such codes are entered in the dial screen and allow access to restricted parts of the phone. Some of them modify phone’s internal settings and sometimes may even damage the phone if not properly used.

There’s one special code though, which works on most Samsung devices with stock firmware, is safe to use and provide some useful diagnostic information about the phone.

Open the dial screen and type: *#0*# . Backlight of the phone will be boosted to maximum and a new menu will appear, allowing testing most of phone’s features.

Diagnostic menu

Among these you will find filling the display with single color (red, green, blue, black), testing the vibration engine, display dimming, checking both cameras, checking status of all sensors, testing touchscreen (also screen hover and S-pen if device supports these features), speakers, phone keys, notification LED, IR LED and more.

Sensor

Note, that exiting from most diagnostic screens require double back-key press.