C#: Use exit code from application in Windows batch file (script)

14:58

Marek Śliwiński No comments Print This Post  (115)

That’s pretty easy stuff if you know it already.

Take a look on example (TestApp.exe):

  static int Main(string[] args)
        {
            int myCodeNumber = 1;

            // Return some code number
            return myCodeNumber;
        }

and here it is how to catch it in Windows batch file (test.bat):

REM Run app which will return some code number
TestApp.exe

REM If returned code is not == 1 then "do something" else continue to NEXT section
if %errorlevel% NEQ 1 GOTO :NEXT
DO SOMETHING HERE

:NEXT
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: C#, C# Snippets Tags: ,

C#: Reading embedded XML file

13:04

Marek Śliwiński No comments Print This Post  (136)

Snippet:

Assembly assembly = Assembly.GetExecutingAssembly();

string filePath = assembly.GetName().Name + ".filename.xml";

using (Stream stream = assembly.GetManifestResourceStream(filePath))
{
    if (stream != null)
    {
      // do something with Stream from file
    }
}
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: C#, C# Snippets, WinForms Tags:

Transact-SQL: Compare strings in SQL and spaces

13:00

Marek Śliwiński No comments Print This Post  (546)

Just a quick reminder/note. What will be the result for below string comparision? What do you think?

IF ('   ' <> '')
	print 'different'
ELSE
	print 'equal'

If you answered ‘different’ then you are wrong :) The result will be ‘equal’. Why?
Because SQL ignores trailing spaces when comparing strings.

Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print

C#: WinForms – Send POST,GET HTTP data and retrieve webpage source using WebClient with cookies or HttpWebRequest,HttpWebResponse. Phpbb.com login example.

23:26

Marek Śliwiński No comments Print This Post  (1,299)

WebClient class is very handy and useful but has one big gap. There is no build in cookies handling between requests. Luckily somebody wrote briliant solution for this :) http://couldbedone.blogspot.com/2007/08/webclient-handling-cookies.html

With above snippet the WebClient class will be enough for most common web tasks.

Below you find my rich (maybe to rich) commented example of use WebClient class (enhanced) to send data(POST, GET) and retrieve response (webpage source). There are also HttpWebRequest/HttpWebResponse classes example so you can compare both methods.

In my examples I ‘m going to connect to http://www.phpbb.com, login into this community site and after success pull some data from Phpbb User Configuration Panel.

Full code available to download: WebClientEnhancedFetchWebpage Example (84)

So in below example we will login into phpbb.com forum.

phpBB-control-panel-csharp-15
Read more…

Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: WinForms Tags: ,

C#: Get all inner exceptions messages

22:22

Marek Śliwiński No comments Print This Post  (232)

Very often InnerException of Exception has some valuable details what happens. Quite often InnerException has an InnerException so more error messages to investigate. 

Theoretically there can be unlimited number of Inner Exceptions so below are 2 small snippets to get all messages from them. Both snippets do the same but first in a “while method”, second in recursive way.

        /// <summary>
        /// Gets all inner exceptions messages.
        /// </summary>
        /// <param name="ex">The exception</param>
        /// <returns>Messages from all inner exceptions.</returns>
        public static string GetInnerExceptionMessages(Exception ex)
        {
            Exception inner = ex.InnerException;
            string messages = String.Empty;

            while (inner != null)
            {
                messages += inner.Message;

                if (!messages.EndsWith("."))
                {
                    messages += ".";
                }

                inner = inner.InnerException;
            }

            return messages;
        }

And another method as recursive function from:
http://stackoverflow.com

public string GetInnerException(Exception ex)
{
     if (ex.InnerException != null)

     {
        return string.Format("{0} > {1} ", ex.InnerException.Message,GetInnerException(ex.InnerException));
     }

   return string.Empty;
}
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: C# Snippets Tags: ,

C#: Language not supported in Ajax Control Toolkit? No problem, do localization by yourself

8:01

Marek Śliwiński 1 comment Print This Post  (646)

In my projects I often have to implement user interface in two languages: English and Polish. Although localization in .NET is quite easy, one thing was annoying to me: AjaxControlToolkit doesn’t support Polish language.

When my user choose Polish language on the webpage then he still see in Calendar control ‘Today’ word instead Polish version (‘Dzisiaj’). Same thing for several other controls, for example PasswordStrength control. So one day I decide to correct this once and for all. Below I will show you how to localize Ajax Control Toolkit.

To localize AjaxControlToolkit to our langauge (Culture) we will have to do few simple steps. In below examples I use library version for .NET 2.0 but the same steps are in case of Ajax Control Toolkit for .NET 3.5

First step, download library source from http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326

Unzip, and open in Visual Studio AjaxControlToolkit project which is located in directory AjaxControlToolkit in zip file.
Read more…

Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: ASP.NET Tags: ,

Regular Expressions: Count the occurences of characters set/group in a string

9:15

Marek Śliwiński No comments Print This Post  (852)

Let’s say, you would like to count the number of characters group, for example: all upper case chars in string. How to do it? That’s easy by regular expressions.

Pattern for all upper case chars will be just: [A-Z] So here is a code:

string text = "Hmm, how to count ALL UpperCase chars?";
int result = Regex.Matches(text, "[A-Z]").Count;
// result = 6

We can use of course multiple rules at once. For example, to count all occurences of UpperCase chars, digits and 2 special symbols “#?”

string text = "#Hmm#, how to count ALL UpperCase chars?";
// pattern [A-Z] for UpperCase, [0-9] for numeric, [#?] for our special symbols
string pattern = @"[A-Z0-9#?]";
int result = Regex.Matches(text, pattern).Count;
// result = 9

and another trivial example.. Count all comma in a string.

string text = "1, 2, or maybe 3?";
string pattern = @"[,]";
int result = Regex.Matches(text, pattern).Count;
// result = 2
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print

Visual Studio: Automatically increment assembly build version with custom settings (add-in)

18:25

Marek Śliwiński 1 comment Print This Post  (2,239)

I always wanted to have automatic build version increment for my apps and libraries to avoid setting it manually again and again. Additionally there should be support inside Visual Studio and  possibility for custom settings because I like the “date signing” where 3 last fields are just YYYY-MM-DD.

A dream come true ;) in form of Build Version Increment add-in for Visual Studio 2005/2008. This great add-in is very simple in use but do exactly what everyone developer needs. Add-in webpage: http://autobuildversion.codeplex.com/

My screenshots should say everything. Enjoy!

build_version_incremental_1

build_version_incremental_1a
Read more…

Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print

Visual Studio: DevExpress menu not visible after CodeRush Xpress installation

10:46

Marek Śliwiński No comments Print This Post  (378)

DevExpress company has a lot of free goodies for .NET developers. Unfortunately Microsoft has asked them to hide DevExpress entry from main menu (only in case of DevExpress freeware tools, for example CodeRush Xpress or DXCore). That’s very annoying behaviour because most of the DXCore community plugins can be accessed from DevExpress menu.

To show DevExpress entry in Visual Studio menu you have to edit or create special registry entry.

If you installed CodeRush Xpress “only for you” then apply this to HKCU hive.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Developer Express\CodeRush for VS\9.2]
"HideMenu"=dword:00000000

if for “everyone” then HKLM hive:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Developer Express\CodeRush for VS\9.2]
"HideMenu"=dword:00000000

In both cases you have to edit CodeRush Xpress version number to particular one installed on your computer. I had version 9.2

Ready to import registry files are below (but remember to change version number).

Fix - show DevExpress menu in Visual Studio (92)
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print

Visual Studio 2005: ASP.NET debugger doesn’t work with Internet Explorer 8 installed

10:13

Marek Śliwiński No comments Print This Post  (183)

I had yesterday this very odd problem and again Google came to rescue. Here is a solution: http://weblogs.asp.net/abdullaabdelhaq/archive/2009/06/01/VS-Debug-Problem-with-IE8.aspx

This problem only apply to VS 2005 and prior.

You can download ready to import registry file below:

Fix for Visual Studio and ASP.NET debug problem with Internet Explorer 8 (84)
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: Visual Studio Tags: