dotnet thoughts 

a dotnet developer's technical blog

Open Visual Studio Files As Administrator

Debugging WCF service requires to run the visual studio as Administrator, you can do it by right clicking the Visual Studio shortcut, Select Properties, and in Shortcut tab, click on Advanced button, it will display Advanced Properties, check the Run as administrator option.

Advanced Properties

Advanced Properties

But this option will not work, when you are opening a project or solution by double clicking on the solution file. Here is the tip which will help you to open Visual Studio files with Administrator option. All the solution files are opening using a small utility called VSLauncher.exe, which can be found in C:\Program Files (x86)\Common Files\microsoft shared\MSEnv folder. Right click on the executable, select Properties and select the Compatibility tab, Check the Run this program as an Administrator, under Privilege level. Apply the settings and click OK. Now open any solution, Windows will show the UAC dialog.

VSLauncher.exe Properties

VSLauncher.exe Properties

CSC : error CS1548: Cryptographic failure while signing assembly xxx.dll Error signing assembly — Access is denied

Yesterday I got this error while building my application after installing Windows 7 x64. I thouoght it was because of X64 OS, but later I found it doesn’t have anything to do with X64 OS. I don’t know the actual problem or solution.You can fix this error by setting permissions.Browse to the following folder ‘C:\Users\All Users\Microsoft\Crypto\RSA’ and modify the security settings of the ‘MachineKeys‘ folder such that Everyone have read/write access to it and the Administrators have full control. Don’t worry if you couldn’t find “All Users” folder in the path, just put that path into Run and it will open the Path(Actually it is opening the same folder from ProgramData). Again I don’t know this is an actual fix or not, but it works :)

Changing the diff program used by Visual Studio

You can configure your own diff program, instead of the default diff program provided from Microsoft for comparing two files. In this post I am using Beyond Compare.

  1. Open Visual Studio 2010
  2. Select Tools > Options menu.
  3. Select the Source control Tab in the left side.
  4. Source control options in Visual Studio options

    Source control options in Visual Studio options

  5. Click the “Configure User Tools” button in the right pane. It will popup a Configure user tools dialog like this.
  6. Configure user tools dialog

    Configure user tools dialog

  7. Now you are ready to enter the configuration for whichever tool you choose to use. Below is the settings for Beyond Compare.
  8. Settings for Compare tool

    Settings for Compare tool

  9. Here is the settings.
  10. Extension: .*
    Operation: Compare
    Command: C:\Program Files\Beyond Compare 3\BC2.exe [replace with the proper path for your machine]
    Arguments: %1 %2 /lefttitle=%6 /righttitle=%7

Happy programming :-)

Here is a detailed post regarding diff/merge configuration in Team Foundation – common Command and Argument values from James Manning.

Create your own code snippets in Visual Studio

Code snippets in Visual Studio is nice feature. This will help developers to increase productivity. ;) You can insert a code snippet by typing the shortcut and press the TAB twice. You can get installed code snippets from Tools > Code Snipper Manager; it will help you to view all the code snippets available. Code snippets are physical XML files with .Snippet. You view all the available VC# snippets from this location : C:\Program Files\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#. This post is about creating Code snippets from existing code snippets. There are various tools available for creating / editing code snippets.

Code snippet manager - Location of installed snippets highlighted

Code snippet manager - Location of installed snippets highlighted

Any .Snippet file is XML format. So you can modify this with any standard editor. Here we are creating a code snippet which will insert try – catch – finally block (Currently it is not available with Visual Studio). For this I made a copy of try.snippet, and renamed as
trycf.snippet. And opened the file in Visual Studio for editing.

I modified the snippet header information.

Code snippet information header modified

Code snippet information header modified

Then I modified the code section.

Snippet code section modified

Snippet code section modified

Save the changes. Now it will be available in the Visual Studio editor.

Snippet manager with new code snippet

Snippet manager with new code snippet

Now you can insert try-catch-finally block, by typing trycf and press TAB twice. :)

How to compile C# code snippet in runtime

Yesterday one of colleague called me and asked how I can compile code from a text source. I explored System.CodeDom classes. And I found one way to create executable (libraries too) using CSharpCodeProvider class.

using (CSharpCodeProvider csharpCodeProvider = new CSharpCodeProvider())
{
    this._OutPutFile = Path.ChangeExtension(Path.GetTempFileName(), ".exe");
    CompilerParameters parameters = new CompilerParameters()
    {
        //For creating DLLs it should be false
        GenerateExecutable = true,
        OutputAssembly = this._OutPutFile,
        //For displaying warnings in the compilerResults
        WarningLevel = 4
    };
    //I am reading the text from a WPF RichTextbox
    TextRange textRange =
        new TextRange(this.txtEditor.Document.ContentStart,
            this.txtEditor.Document.ContentEnd);
    CompilerResults compilerResults =
        csharpCodeProvider.CompileAssemblyFromSource(parameters, textRange.Text);
}

For more details you can found in MSDN

DotNet Snippet compiler (I created a WPF application which used to compile C# snippets, I will make it open source soon.) running on my system.

WPF Snippet compiler

WPF Snippet compiler

« Newer PostsOlder Posts »