Friday, March 15, 2013

New website

All the latest posts have been moved to my website:
www.vaibhavchugh.com

Thanks
Vaibhav

Tuesday, October 11, 2011

Find your OS is 64 bit or 32 bit C#

Difficult to search on the web.. There are 100s of tricks being talked about..

In C# its simple

Environment.Is64BitOperatingSystem.ToString();

Thanks

Sunday, March 6, 2011

XAMPP 1.7 - Apache not working on windows7 64 bit.

I tried installing XAMPP with xampp-win32-1.7.4-VC6-installer . I was not able to get apache working. The apache started and then stopped after 3-4 seconds.

I tried various things like running the setup with vista compatibility and as administrator. But nothing worked..

So the best way i found was to fix the issue using old version of xampp i.e 1.6 .
The old versions of xampp can be found at:

http://cdnetworks-us-1.dl.sourceforge.net/project/xampp/XAMPP%20Windows/1.6.8/xampp-win32-1.6.8-installer.exe

now it works just fine :)

Thursday, September 30, 2010

Find the time elapsed between two statements C#

This was some thing i got to learn yesterday. I was using some profiler and some random ways to get the time. but this was a quick one. I knew something like this existed but never bothered until I had to use it

There is a Stopwatch class in c# that does that for you.

all you need to do is

Stopwatch sw = new Stopwatch();
sw.Start();
Debug.WriteLine(sw.ElapsedMilliseconds.ToString());
sw.Stop();


Thursday, March 25, 2010

Visual studio 2008 shortcuts

Visual studio 2008 shortcuts

CTRL + ".": same as CTRL + SHIFT + F10 opens the smart tag window and allows you to add Using statements Or implement interfaces

ALT + CTRL + "e": open the Exceptions window

CTRL + "k" + "f" and CRTL + "k" + "d": these two will format the code in the window to be nicely indented. Using "d" will format all the document while using "f" will format only selected text. The formatting is for all types of documents, HTML, Xaml, XML, C#… This one is my favorite.

SHIFT + Del: cut the entire row from the document and past it to the clipboard.

CTRL + "k" + "c" and CTRL + "k" + "u": These two are for commenting selected text (the "c" options) and uncommenting selected text (the "u" option).

ALT + ENTER: this little shortcut will open up the Properties window

CTRL + "k" + "s": opens up the code snippets dialogue within the code

F12: I think you all know this but still F12 is the shortcut for the "Go to definition" command which will take you to the definition of the object your marker is currently on.

F9: add a breakpoint to the code line your marker is currently at. Clicking F9 again will remove this breakpoint from that line.

CTRL + ALT + "q": This one will open the Quick watch window

Ctrl+K, Ctrl+F - Format selected block of code

Ctrl+Shift+B -Build solution


References

http://www.dev102.com/2008/04/17/10-visual-studio-shortcuts-you-must-know/

http://www.85turns.com/2008/07/03/favorite-visual-studio-2008-keyboard-shortcuts-2/

Friday, March 5, 2010

Get an alert from TFS when a work item is assigned to you(me)


Thanks to :Jan Tanis (http://blog.jantanis.net/2009/09/work-item-email-notification-with-team.html)


How to setup email notification on having assigned a Work Item to you?

1. Download and install Team Foundation Power Tool for Visual Studio @ http://www.microsoft.com/downloads/details.aspx?FamilyId=FBD14EEA-781F-45A1-8C46-9F6BA2F68BF0&displaylang=en#filelist

2. Start Visual Studio and open the Alert Editor

3. Add new Alert -> Work Item alert -> A Work Item was assigned to me

4. Change your name in “Send To” to your current email address

5. Collapse Work Items Alerts to ensure saving (workaround to start saving procedure)

6. DONE!

Friday, February 12, 2010

Executing C# WinForms application in Partial trust mode

Its pretty easy to do in VS 2010
->Open solution explorer for a solution
->Right click the project you wish to run in partial trust mode
->select properties
->select the security tab
->check "Enable ClickOnce security"
-> select "This is a partial trust application"

and your application will run in partial trust mode from now on...

Vaibhav

Tuesday, February 9, 2010

String matching with pattern having wildcard characters in C#

The solution makes use of Regex class in .NET and supports both '*' and '?' wild cards.

private bool IsWildcardMatch(String wildcard, String text, bool casesensitive)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(wildcard.Length + 10);
sb.Append("^");
for (int i = 0; i < wildcard.Length; i++)
{
char c = wildcard[i];
switch(c)
{
case '*':
sb.Append(".*");
break;
case '?':
sb.Append(".");
break;
default:
sb.Append(System.Text.RegularExpressions.Regex.Escape(wildcard[i].ToString()));
break;
}
}
sb.Append("$");

System.Text.RegularExpressions.Regex regex;
if (casesensitive)
regex = new System.Text.RegularExpressions.Regex(sb.ToString(), System.Text.RegularExpressions.RegexOptions.None);
else
regex = new System.Text.RegularExpressions.Regex(sb.ToString(), System.Text.RegularExpressions.RegexOptions.IgnoreCase);

return regex.IsMatch(text);
}

I picked up the above sample from http://adamsen.wordpress.com/2008/10/16/c-string-wildcard-match/ but it only supported '*" and not '?'.
I added a case block to support ? as well.


vaibhav

Thursday, June 11, 2009

Restoring a .bak file to a SQL Server database

source: http://blog.sharepointdevelopment.nl/post/Restoring-a-bak-file-to-a-SQL-Server-database.aspx
Author:Nick Boumans

Sometimes you have to restore a .bak file to a SQL Server database where the .bak file was not originally back-upped from. You can do this either by a script or using SQL Server Management studio. By using Management Studio, you have to follow the next steps:

- Open SQL Server Management Studio (either Enterprise or Express)
- Right Click Databases -> Restore Database…
- Select your To Database e.g. OrdersNew
- Select from device and select your .bak file
- Move to the item options (you're standard in General)
- Check or the database paths are correct, otherwise correct them
- Go back to the general tab
- Check your database and hit the OK button

Vaibhav

Wednesday, May 6, 2009

Citrix Receiver for iPhone and Doc Finder

Citrix is a nice company and they work really well towards innovative stuff.

Finally Doc Finder is out. We worked pretty hard for around 6 months for the release of version 1.0 of this project. Both my site director and my test lead were superb in their approach towards the development of this project and I must say without both of them this thing wouldn't have been possible. VPRD team was very nice and innovative as well. They came up with amazing designs for the UI and they also knew what a user expects for the best experience. I really loved working on this project and I must say all the people involved in this project came up with some awesome ideas.

I found this demo video on you tube, it's a must watch at least for all citrites and iPhone/ XenApp users.. This is video for 0.9 version of Doc Finder project and v1.0 has some more cool features like search, recent documents, stylish help screen and an animated settings page:o)



My favorite part of this video are the last 2 minutes of the demo coz that's the part in which we get to see Doc Finder.

It was fun working on it as a developer :o)

All the best Citrix...

Cherio!!!
vaibhav

Thursday, April 16, 2009

Could not find any resources appropriate for the specified culture or the neutral culture.

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "XXXYYY.en-US.resources" was correctly embedded or linked into assembly "AAABBB" at compile time, or that all the satellite assemblies required are loadable and fully signed.

I wasted a lot of time to find out the reason behind this error. This was coming up when I was trying to use culture specific satellite DLL.

After search the complete web I found the following answer:

It is not because the resource manager didn't find the satellite assembly but because the embedded .resources file in the satellite assembly doesn't have a full
name. In this case, the full name is "MyLib.Resources.es-MX.resources", but
the .resources file's name is "Resources.es-MX.resources".

When you use the resgen tool to convert .resx file to common language runtime binary .resources file, you should add the namespace name of your project in the front of the converted .resources file. The following is a sample.

resgen Resources.es-MX.resx MyLib.Resources.es-MX.resources

Thus, the .resources file has a full name. Then you use AL.exe tool to bundle the .resource file into a satellite assembly and place this satellite assembly to the es-MX subdirectory of the application's bin/debug folder.

Run the application again and you should see the correct result.


Thanks to Linda Liu [MSFT] for the answer...
source : http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic55934.aspx


vaibhav Chugh

Wednesday, April 8, 2009

Remove flicker in listbox C#

I read over 100 articles on the web just one was able to help me and that too was ignored by others in the discussion.

Well, if you are adding less number of items to a listbox then
listbox1.beginupdate()
listbox1.add(item);
listbox1.endupdate();

should work for you just fine. There shall be no flicker.
using doublebuffered user control / form may also solve the problem. In some cases

this.SetStyle(ControlStyles.UserPaint  ControlStyles.OptimizedDoubleBuffer 
ControlStyles.AllPaintingInWmPaint ControlStyles.SupportsTransparentBackColor,
true);
may also work.

but in my case the data was coming at a very high speed and high quantity and I had to display this large amount of data in an owner drawn list box.

this.SuspendLayout();
foreach (DataItem ditem in ditems)
{
listbox1.Items.Add(ditem);
}
this.ResumeLayout();


the above code helped me in getting a pretty smooth layout.

hope it helps
Vaibhav Chugh

Thursday, March 5, 2009

CopyRight symbol in C#

This was funny to find....

2 good ways to do it

string copyright="\u00A9";

string completetext=copyright + "myCredits";

Much better way =>
press Alt and then 0 1 6 9..this will convert into the copyright symbol

n u r good to go :)

vaibhav

Wednesday, February 18, 2009

How to release IntPtr handle C#

If you are using IntPtr as handles in C# code and wish to avoid handle leaks you may use :

IntPtr handle = some handle;
Marshal.Release(handle);

Google search didn't give me correct answer instantly so I added it here :)

cherio!!
Vaibhav


Saturday, February 7, 2009

height and width of text with specific font

When you draw a string on a C# form if you want to figure out how many pixel wide and high the text will be use:

TextRenderer Class

To measure the size you may use

TextRenderer.MeasureText :

TextRenderer.MeasureText (String, Font, Size)
TextRenderer.MeasureText (IDeviceContext, String, Font, Size)
TextRenderer.MeasureText (String, Font, Size, TextFormatFlags)
TextRenderer.MeasureText (IDeviceContext, String, Font, Size, TextFormatFlags)

It returns back System.Drawing.Size which has height and width of the text you wanted.


To draw text you may use:
private void RenderText1(PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
new Point(10, 10), SystemColors.ControlText);
}


for more details you may visit:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer(VS.80).aspx

Wednesday, February 4, 2009

Disable right click pop up on textbox in C# winform

It took me almost 3 hours to google the correct way to fix this one...

There are 2 ways of disabling right click on textbox in a C# winform.

1. create a new context menu and add it to the text box control :

TextBox textbox1 = new TextBox();
ContextMenu cm = new ContextMenu();
textbox1.controls.add(cm);

and you are done.

2. Just do
textbox1.ShortcutsEnabled = false;

second one is short and sweet....

Wednesday, January 7, 2009

Adding icon to C# application

To add an icon to the exe given out by your C# code. Do the following:

In visual studio. Select

  1. Project
  2. [Project Name] Properties
  3. Application
  4. Icon
  5. (From the Combobox select)
and apply the .ico file :)

you are all set :)

vaibhav

Saturday, December 13, 2008

Remove selected items from listview in C#

I killed some time figuring this out myself though the solution was there on the first link given out by google..

thanx to the guy who posted this one..

list is a listview here...

while (list.SelectedItems.Count > 0)
{
list.Items.Remove(list.SelectedItems[0]);
}

vaibhav

Monday, December 8, 2008

How to find file system disk space in linux

My linux VM started showing me that it ran outof space... So I tried to figure out what is the space left over... so I found this on the man page of df command...

df - report file system disk space usage

Show information about the file system on which each FILE resides, or all file systems by default.

Mandatory arguments to long options are mandatory for short options too.

-a, --all
include dummy file systems

-B, --block-size=SIZE use SIZE-byte blocks

-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)

-H, --si
likewise, but use powers of 1000 not 1024

-i, --inodes
list inode information instead of block usage

-k like --block-size=1K

-l, --local
limit listing to local file systems

--no-sync
do not invoke sync before getting usage info (default)

-P, --portability
use the POSIX output format

--sync invoke sync before getting usage info

-t, --type=TYPE
limit listing to file systems of type TYPE

-T, --print-type
print file system type

-x, --exclude-type=TYPE
limit listing to file systems not of type TYPE

-v (ignored)

--help display this help and exit

--version
output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following: kB 1000, K 1024, MB
1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.


n thats what my VM gave me :)

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 3945128 3945112 0 100% /
none 385124 0 385124 0% /dev/shm

//14.4.2.55/CodePart
117218300 58824100 58394200 51% /mnt/myshare


vaibhav

Friday, October 3, 2008

export not working in shell script - solution

Another small problem and another nice thing learnt :o)

I was trying to make a shell script that exports all my environment variables as I was tired exporting them manually each time I connected to my lab machine from my computer. I wrote the script and tried running. to my surprise it was printing the echo statements but not exporting the variable. After searching for 5 minutes on the web I found that when we try exporting some variables it runs in a private shell. This make the changes not visible in the current shell.

I found this on a linux forum and found it interesting:
The program that you use to access the shell is called a terminal emulator. Examples are Konsole, Gnome Terminal, xterm, Eterm, aterm, etc. When you open one of these, the program also creates a subshell. The shell that you enter commands into is different from the one running your X server, though it has inherited all the attributes of the main shell.

When you run a script in a shell, that script runs in a subshell that works very similarly. A script runs in its own environment, and does not affect the parent at all. When the "source" command is used, changes to the subshell are duplicated in the parent shell


http://www.linuxforums.org/forum/linux-newbie/55168-export-doesnt-work-shell-script.html
so the solution:
simple ... run your shell script with the following command
test_script.sh=>

#!/bin/sh
export MY_TRACE_FILE=/root/mytrace.trc;

and then do a

. ./test_script.sh

or

source ./test_script.sh


I tried the second one and it worked just fine :)