Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Friday, December 21, 2012

Always run Visual Studio as administrator under Windows 8

This is a default state for many developers and this functionality is available under properties menu in Windows 7 but has vanished in Windows 8.

In order to enable VS running as administrator in Windows 8:

you have to right-click devenv.exe and select "Troubleshoot compatibility".

1. select "Troubleshoot program"
2. check "The program requires additional permissions"
3. click "Next", click "Test the program..."
4. wait for the program to launch
5. click "Next"
6. select "Yes, save these settings for this program"
7. click "Close"

Source: http://stackoverflow.com/questions/12257110/can-you-force-visual-studio-to-always-run-as-an-administrator-in-windows-8

Saturday, December 20, 2008

Transfer Visual Studio settings

I’m reinstalling Windows on my work computer and wanted to preserve all the current Visual Studio settings.

In case you didn’t know how it is done:

 

1. Open Visual Studio

2. Go to Tools->Import and Export Settings

image

3. Choose “Export selected…”

image

4. Choose which settings you want to export

image

5. Choose filename and location

6. Done :)

7. All you need to do now is to go to same menu on other VS and select import.

Friday, August 29, 2008

Visual Studio - how to restore missing templates

 

DevEnv.exe /installvstemplates

The file is located (for vs2005) at:

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE

Saturday, July 19, 2008

Jump between braces in Visual Studio

Put your cursor before or after the brace (your choice) and then press Ctrl+].

Source

Wednesday, May 14, 2008

Add Windows Explorer to your Visual Studio tools menu

Sometimes it is needed to access the project folder in Windows Explorer, but there is no easy way to do that using VS.

It can be solved by adding Explorer to External Tools in VS:

To set it up, click Tools, then External Tools..., then click Add.  Now enter the following data:
Title: Windows Explorer
Command: explorer.exe
Arguments: /select,"$(ItemPath)"

Leave Initial directory blank, and click OK.  Now when you click Tools, Windows Explorer, Windows Explorer will open with the current file you are editing selected.

Monday, April 7, 2008

C#- SMTP mail - System.Net.Mail

 

   1: MailMessage message = new MailMessage();
   2: message.From = new MailAddress("sender@foo.bar.com");
   3: message.To.Add(new MailAddress("recipient1@foo.bar.com"));
   4: message.To.Add(new MailAddress("recipient2@foo.bar.com"));
   5: message.To.Add(new MailAddress("recipient3@foo.bar.com"));
   6: message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
   7: message.Subject = "This is my subject";
   8: message.Body = "This is the content";
   9: SmtpClient client = new SmtpClient();
  10: client.Send(message);
  11:  
  12:  

Example of web.config configuration:

   1: <system.net>
   2:     <mailSettings>
   3:       <smtp from="test@foo.com">
   4:         <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
   5:       </smtp>
   6:     </mailSettings>
   7: </system.net>

My example using "localhost":

   1: MailMessage message = new MailMessage();
   2: message.From = new MailAddress("hello@world.com");
   3: message.To.Add(new MailAddress("hello@world.com"));
   4: message.Subject = "Mail from Idea Catcher";
   5: message.Body = "New idea was posted";
   6: SmtpClient client = new SmtpClient("Localhost");
   7: client.UseDefaultCredentials = false;
   8: client.Send(message);

here is the link for iis configuration in case of error - 5.7.1 Unable to relay for xxx - here

source1

source2

Tuesday, March 18, 2008

Add Windows Explorer to Visual Studio tools menu

To set it up, click Tools, then External Tools..., then click Add.  Now enter the following data:
Title: Windows Explorer
Command: explorer.exe
Arguments: /select,"$(ItemPath)"

Leave Initial directoy blank, and click OK.  Now when you click Tools, Windows Explorer, Windows Explorer will open with the current file you are editing selected.

Friday, February 1, 2008

Debugger variable $exception

If your catch block do nothing with caught exception you may declare block argument without name (to avoid warning message "CS0168: The variable 'ex' is declared but never used"):

try

    {

        ...

    }

catch (Exception)

    {

// deliberately suppressing all exceptions

    }

But one day during debugging you may actually want to examine Exception. Since you don't have variable where exception is stored you can use debugger variable $exception provided by the Visual Studio.NET 2005 Debugger to examine the exception in a catch block. Just add it to Watch Window.

 

Source

Use Path.GetRandomFileName() or Path.GetTempFileName() when working with temp files

Do not reinvent function for generating unique name for temporary files. Use one of the existing methods:

 

Source

Conditional breakpoints in Visual Studio

You can specify a breakpoint condition which will be evaluated when a breakpoint is reached. The debugger will break only if the condition is satisfied.

To specify a condition:

  1. In a source window, right-click a line containing a breakpoint glyph and choose Condition from Breakpoints in the shortcut menu. Conditional Breakpoint
  2. In the Breakpoint Condition dialog box, define a boolean condition using the code in your local scope. For example, you can only break when _culture != "en-US".
  3. Choose Is true if you want to break when the expression is satisfied or Has changed if you want to break when the value of the expression has changed.
  4. Click OK.

 

Source

Saturday, November 10, 2007

Single Instance Application

Making an application single instance, can be achieved by using a mutex (Mutual Exclusion Semaphore). A Windows application loads the main form through the Application.Run( ) method. In the Main method, create a new mutex. If a new mutex is created the application is allowed to run. If the mutex has already been created, the application cannot start. This will ensure that only one instance will be running at any time.

// Used to check if we can create a new mutex
bool newMutexCreated = false;
// The name of the mutex is to be prefixed with Local\ to make
// sure that its is created in the per-session namespace,
// not in the global namespace.
string mutexName = "Local\\" +
 System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

Mutex mutex = null;
try
{
   // Create a new mutex object with a unique name
   mutex = new Mutex(false, mutexName, out newMutexCreated);
}
catch(Exception ex)
{
   MessageBox.Show (ex.Message+"\n\n"+ex.StackTrace+
        "\n\n"+"Application Exiting...","Exception thrown");
   Application.Exit ();
}

// When the mutex is created for the first time
// we run the program since it is the first instance.
if(newMutexCreated)
{
   Application.Run(new AnimatedWindowForm());
}

When a new mutex is created the mutex name can be prefixed with either Global\ or Local\. Prefixing with Global\ means the mutex is effective in the global namespace.

Prefixing with Local\ means the mutex is effective in the user's session namespace only.

Windows XP and Windows 2003 allow fast user switching through Terminal Services Sessions. So if a mutex is created with a Global\ prefix, the application can have only one instance system wide. So if one user launches the application, other users cannot create a second instance in their sessions. If the mutex is not prefixed with Local\ it is effective per session only.

Placing Your C# Application in the System Tray

  1. To get started, open an existing C# Windows form (or create a new one).
  2. Open the Visual Studio Toolbox.
  3. Drag a NotifyIcon control onto the form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.
  4. Set the NotifyIcon control's Text property to the name you want to appear when the user pauses the mouse over the application's icon. For example, this value could be "KillerApp 1.0".
  5. Set the control's Icon property to the icon that you want to appear in the System Tray
  6. Add an event handler for the form's Resize event that will hide the application when it's minimized. That way, it won't appear on the task bar.
  7. private void Form1_Resize(object sender, System.EventArgs e)
    {
      if (FormWindowState.Minimized == WindowState)
         Hide();
    }
    
  8. Add an event handler for the NotifyIcon.DoubleClick event and code it as follows so that the application will be restored when the icon is double-clicked.
  9. private void notifyIcon1_DoubleClick(object sender,
                                        System.EventArgs e)
    {
       Show();
       WindowState = FormWindowState.Normal;
    }
    

At this point, your application will fuction perfectly in terms of an icon appearing in the System Tray when the application is run (see Figure 1), the application not appearing on the task bar when minimized and the application restoring itself when the Tray icon is double-clicked.

Figure 1

Now, let's see the steps involved with adding a context menu to the icon.

  1. From the Visual Studio Toolbox, drag a ContextMenu control onto the form.
  2. Right-click the ContextMenu control and select the Edit Menu.option.
  3. Type in the options that you want to appear in your context menu. For example, you can add options such as Restore and Close Application.
  4. As with any menu, double-click the menu item to create and code each item's handler. As an example, you could copy the code from the form's DoubleClick handler into the context menu's Restore handler and for the Close Application menu item; simply call the form's Close method.
  5. Finally, set the NotifyIcon control's ContextMenu property to the new context menu you just created by selecting the menu from the drop-down list. Figure 2 shows a simple Tray context menu.

Visual Studio Setup and Deployment

Video: Delayed Startup Setup Project CodeProjec Example