Zayko in the Net

Personal blog of Vitaly Zayko

NAVIGATION - SEARCH

Another way to get path to a running assembly or how to get list of command line arguments in a non console App?

There is an another way how to get path to a running assembly from itself besides I mentioned here. Use this simple snippet:

string[] cmd = Environment.GetCommandLineArgs();
string path_to_assembly = cmd[0];
/* cmd[1] and above contains command line arguments is any */

Resulting array is at least one element length or longer. First element of this array always contains path to the assembly. As a side effect – you can get command line params beginning from second element.

Unfortunately doesn’t work in .NET Compact Framework.

Technorati Tags: ,

Adding /r/n in .NET (C#)

When your App outputs some text, you probably will want to split your strings by adding carriage return. Of course, you can declare this as a constant string: private const string CRLF = "/r/n";

or use predefined constant from Environment: Environment.NewLine

Using Windows 7 Jump List in Visual Studio 2010 Beta 2

If you are on Windows 7, you probably have noticed about new feature – Windows 7 Jump List. If you right-click on an App icon (let say – Internet Explorer) you will see additional menu items which you as a programmer can handle in your solutions. If you are on Visual Studio 2008, the only way is to use an additional Windows API Code Pack for Microsoft .NET Framework. But there is a modern way for Visual Studio 2010 (at least for WPF):

  • Open App.xaml file of your App
  • Add reference to JumpList as shown below:

<Application x:Class="WpfApplication1.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             StartupUri="MainWindow.xaml">

    <Application.Resources>    

    </Application.Resources>

    <!-- Add reference to JumpList here: -->

    <JumpList.JumpList>

        <JumpList ShowFrequentCategory="True" ShowRecentCategory="True">

            <JumpTask Title="Notepad"

                      Description="Open Notepad"

                      ApplicationPath="notepad.exe"

                      IconResourcePath="notepad.exe"/>

        </JumpList>

    </JumpList.JumpList>

</Application>

Technorati Tags: ,,

Microsoft Visual Studio 2010 Beta 2 is available for MSDN Subscribers!

VS_v_rgb_ai If you are lucky MSDN Subscriber, you can download VS2010 as well as TFS2010 right now! I already did.
BTW: TFS2010 has "go live" license which means you can use it in your daily work instead of just testing purposes. Here are more details: Brian Keller's blog.
If you don't have the subscription, you just need little wait - it will be available for everyone in October 22nd.

Technorati Tags:

Most useful Windows 7 shortcut keys

I’m a happy MSDN subscriber so have Windows 7 installed for few months by now and got some useful shortcuts to share.

  • Multi monitor system? Use Win+Shift+Left to move to the left monitor or Win+Shift+Right – to the right
  • Win+P – switches between monitor options (multi monitor/projector settings)
  • One monitor? Then Win+Left is snapping current window to the left side and Win+Right – to the right
  • Win+Space – peeks at the Desktop
  • Win+X – calls Mobility Center
  • Win+T – first Taskbar entry
  • Win+G – brings desktop gadgets to the top

Enjoy!

Technorati Tags:

How to boot Windows 7 faster?

Check your system configuration: active services, what items run at startup ect. All of those are in one place: MSCONFIG.

MSCONFIG in Microsoft Windows 7

Technorati Tags:

How to enable menu “Create GUID” in Visual Studio 2008

If you need to create a GUID in VS2008 but this menu is disabled then you are facing the same problem as I did. Probably some options were disabled during installation. Fortunately it is easy to fix.

Copy two files guidgen.exe and Uuidgen.Exe form
C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin
to
C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools
and you don’t even need to restart your Visual Studio.

C#: How To Disable Windows Screensaver programmatically?

In some cases you may want to prevent Windows screensaver from running. For example, when a customer watch video or does VoIP call, his mouse and keyboard activities are low.

I didn’t find a managed function in .NET Framework but fortunately we can make it through PInvoke.

1. Add PInvoke namespace:

using System.Runtime.InteropServices;

2. Import SystemParametersInfo function and its enums as below:

public enum SPI : uint
{
   SPI_GETSCREENSAVEACTIVE = 0x0010,
   SPI_SETSCREENSAVEACTIVE = 0x0011
}
public enum SPIF : uint
{
   None = 0x00,
   SPIF_UPDATEINIFILE = 0x01,
   SPIF_SENDCHANGE = 0x02,
   SPIF_SENDWININICHANGE = 0x02
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, ref uint pvParam, SPIF fWinIni);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, uint pvParam, SPIF fWinIni);

3. You may want to check is screensaver enabled by Control panel setting or not:

uint vParam = 0;
if (SystemParametersInfo(SPI.SPI_GETSCREENSAVEACTIVE, 0, ref vParam, SPIF.None))
{
   if (vParam == 1)
       MessageBox.Show("Screensaver is enabled");
   else
        MessageBox.Show("Screensaver is disabled");
}
else
    MessageBox.Show("Error!");

4. And finally - disable it:

if (SystemParametersInfo(SPI.SPI_SETSCREENSAVEACTIVE, 0, 0, SPIF.None))
{
   MessageBox.Show("Screensaver has been disabled");
}
else
    MessageBox.Show("Error!");

5. Don’t forget to re-enable if it was enabled initially:

SystemParametersInfo(SPI.SPI_SETSCREENSAVEACTIVE, 1, 0, SPIF.None);

New version of ASP.NET AJAX Control Toolkit available!

Three new controls have been added:

  1. HTMLEditor - The HTMLEditor control allows you to easily create and edit HTML content. You can edit in design mode, as a rich text editor, or in source view to edit the HTML markup directly.
  2. ComboBox - The ComboBox control provides a DropDownList of items, combined with TextBox. Different modes determine the interplay between the text entry and the list of items.
  3. ColorPicker - The ColorPicker Control Extender can be attached to any ASP.NET TextBox control. It provides client-side color-picking functionality with UI in a popup control.

Download from here.