How to hide a Console Window in C# and VB.NET

To hide a Console Window in C# and VB.NET you can use the following snippet.

Sample C#

public static class Consoles
{
	[DllImport("user32.dll")]
	public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

	[DllImport("user32.dll")]
	public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

	public static void SetConsoleWindowVisibility(bool visible, IntPtr hWnd)
	{
		ShowWindow(hWnd, !visible ? 0 : 1);
	}
}

usage:

var hWnd = Consoles.FindWindow(null, Console.Title);
Consoles.SetConsoleWindowVisibility(false, hWnd);

Sample VB.NET

Public NotInheritable Class Consoles
	Private Sub New()
	End Sub
	<DllImport("user32.dll")> _
	Public Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
	End Function

	<DllImport("user32.dll")> _
	Public Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
	End Function

	Public Shared Sub SetConsoleWindowVisibility(visible As Boolean, hWnd As IntPtr)
		ShowWindow(hWnd, If(Not visible, 0, 1))
	End Sub
End Class

usage:

Dim hWnd = Consoles.FindWindow(Nothing, Console.Title)
Consoles.SetConsoleWindowVisibility(False, hWnd)




How to check if Date is Workday in C# and VB.NET

To check if Date is Workday in C# and VB.NET you can use the following snippet.

Sample C#

public static bool IsWorkDay(DateTime date, bool isWorkDaySaturday)
{
	if (date == null) {  throw new ArgumentNullException("date");}
	switch (date.DayOfWeek)
	{
		case DayOfWeek.Saturday:
			return isWorkDaySaturday;
		case DayOfWeek.Sunday:
			return false;
		default:
			return true;
	}
}

Sample VB.NET

Public Shared Function IsWorkDay(date As DateTime, isWorkDaySaturday As Boolean) As Boolean
	If date Is Nothing Then
		Throw New ArgumentNullException("date")
	End If
	Select Case date.DayOfWeek
		Case DayOfWeek.Saturday
			Return isWorkDaySaturday
		Case DayOfWeek.Sunday
			Return False
		Case Else
			Return True
	End Select
End Function




How to scale a Size Object by percentage in C# and VB.NET

To scale a Size Object by percentage in C# and VB.NET you can use the following snippet.

Sample C#

public static Size Scale(this Size size, float scalePercent)
{
	if (Math.Abs(scalePercent - 1) < float.Epsilon)
	{
		return size;
	}

	var height = (int) (size.Height*(scalePercent/100));
	var width = (int)(size.Width * (scalePercent / 100));

	return new Size(width, height);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function Scale(size As Size, scalePercent As Single) As Size
	If Math.Abs(scalePercent - 1) < Single.Epsilon Then
		Return size
	End If

	Dim height = CInt(size.Height * (scalePercent / 100))
	Dim width = CInt(size.Width * (scalePercent / 100))

	Return New Size(width, height)
End Function




How to get a random Color in C# and VB.NET

To get a random Color in C# and VB.NET you can use the following snippet.

Sample C#

public static System.Drawing.Color RandomColor()
{
	var rand = new Random();
	var r = rand.Next(0, 256);
	Thread.Sleep(1);
	var g = rand.Next(0, 256);
	Thread.Sleep(1);
	var b = rand.Next(0, 256);
	return System.Drawing.Color.FromArgb(255, r, g, b);
}

Sample VB.NET

Public Shared Function RandomColor() As System.Drawing.Color
	Dim rand As var = New Random
	Dim r As var = rand.Next(0, 256)
	Thread.Sleep(1)
	Dim g As var = rand.Next(0, 256)
	Thread.Sleep(1)
	Dim b As var = rand.Next(0, 256)
	Return System.Drawing.Color.FromArgb(255, r, g, b)
End Function




How to get Devexpress Wizardpage by Name in C# and VB.NET

To get Devexpress Wizardpage by Name in C# and VB.NET you can use the following snippet.

Sample C#

public static WizardPage GetWizardPageByName(WizardControl wizard, string name)
{
	var controls = wizard.Controls.Find(name, false);
	if (controls.Length > 0 && controls[0] is WizardPage)
	{
		return controls[0] as WizardPage;
	}
	return null;
}

Sample VB.NET

Public Shared Function GetWizardPageByName(wizard As WizardControl, name As String) As WizardPage
	Dim controls = wizard.Controls.Find(name, False)
	If controls.Length > 0 AndAlso TypeOf controls(0) Is WizardPage Then
		Return TryCast(controls(0), WizardPage)
	End If
	Return Nothing
End Function




How to get the Youtube VideoId from Url in C# and VB.NET

To get the Youtube VideoId from an Url in C# and VB.NET you can use the following snippet.
It matches these 18 type of Youtube Links (Sample Data):

Sample Input

  • http://youtu.be/AAAAAAAAA01
  • http://www.youtube.com/embed/watch?feature=player_embedded&v=AAAAAAAAA02
  • http://www.youtube.com/embed/watch?v=AAAAAAAAA03
  • http://www.youtube.com/embed/v=AAAAAAAAA04
  • http://www.youtube.com/watch?v=AAAAAAAAA06
  • www.youtube.com/watch?v=AAAAAAAAA07
  • www.youtu.be/AAAAAAAAA08
  • youtu.be/AAAAAAAAA09
  • http://www.youtube.com/watch?feature=player_embedded&v=AAAAAAAAA05
  • http://www.youtube.com/watch?v=i-AAAAAAA14&feature=related
  • http://www.youtube.com/attribution_link?u=/watch?v=AAAAAAAAA15&feature=share&a=9QlmP1yvjcllp0h3l0NwuA
  • http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&u=/watch?v=AAAAAAAAA16&feature=em-uploademail
  • http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&feature=em-uploademail&u=/watch?v=AAAAAAAAA17
  • http://www.youtube.com/v/A-AAAAAAA18?fs=1&rel=0
  • youtube.com/watch?v=AAAAAAAAA10
  • http://www.youtube.com/watch/AAAAAAAAA11
  • http://www.youtube.com/v/AAAAAAAAA12
  • http://www.youtube.com/v/AAAAAAAAA13


  • Sample C#

    using System.Linq;
    using System.Text.RegularExpressions;
    
    namespace de.fesslersoft.YoutubeRegexTest
    {
        internal static class Youtube
        {
            private const string YoutubeLinkRegex = "(?:.+?)?(?:\\/v\\/|watch\\/|\\?v=|\\&v=|youtu\\.be\\/|\\/v=|^youtu\\.be\\/)([a-zA-Z0-9_-]{11})+";
    
            internal static string GetVideoId(string input)
            {
                var regex = new Regex(YoutubeLinkRegex, RegexOptions.Compiled);
                foreach (Match match in regex.Matches(input))
                {
                    //Console.WriteLine(match);
                    foreach (var groupdata in match.Groups.Cast<Group>().Where(groupdata => !groupdata.ToString().StartsWith("http://") && !groupdata.ToString().StartsWith("https://") && !groupdata.ToString().StartsWith("youtu") && !groupdata.ToString().StartsWith("www.")))
                    {
                        return groupdata.ToString();
                    }
                }
                return string.Empty;
            }
        }
    }
    



    Sample VB.NET

    Imports System.Linq
    Imports System.Text.RegularExpressions
    
    Namespace de.fesslersoft.YoutubeRegexTest
    	Friend NotInheritable Class Youtube
    		Private Sub New()
    		End Sub
    		Private Const YoutubeLinkRegex As String = "(?:.+?)?(?:\/v\/|watch\/|\?v=|\&v=|youtu\.be\/|\/v=|^youtu\.be\/)([a-zA-Z0-9_-]{11})+"
    
    		Friend Shared Function GetVideoId(input As String) As String
    			Dim regex = New Regex(YoutubeLinkRegex, RegexOptions.Compiled)
    			For Each match As Match In regex.Matches(input)
    				'Console.WriteLine(match);
    				For Each groupdata As var In match.Groups.Cast(Of Group)().Where(Function(groupdata) Not groupdata.ToString().StartsWith("http://") AndAlso Not groupdata.ToString().StartsWith("https://") AndAlso Not groupdata.ToString().StartsWith("youtu") AndAlso Not groupdata.ToString().StartsWith("www."))
    					Return groupdata.ToString()
    				Next
    			Next
    			Return String.Empty
    		End Function
    	End Class
    End Namespace
    



    Output / Sample

    var urlList = new List<String>
    {
    	"http://youtu.be/AAAAAAAAA01",
    	"http://www.youtube.com/embed/watch?feature=player_embedded&v=AAAAAAAAA02",
    	"http://www.youtube.com/embed/watch?v=AAAAAAAAA03",
    	"http://www.youtube.com/embed/v=AAAAAAAAA04",
    	"http://www.youtube.com/watch?feature=player_embedded&v=AAAAAAAAA05",
    	"http://www.youtube.com/watch?v=AAAAAAAAA06",
    	"www.youtube.com/watch?v=AAAAAAAAA07",
    	"www.youtu.be/AAAAAAAAA08",
    	"youtu.be/AAAAAAAAA09",
    	"youtube.com/watch?v=AAAAAAAAA10",
    	"http://www.youtube.com/watch/AAAAAAAAA11",
    	"http://www.youtube.com/v/AAAAAAAAA12",
    	"http://www.youtube.com/v/AAAAAAAAA13",
    	"http://www.youtube.com/watch?v=i-AAAAAAA14&feature=related",
    	"http://www.youtube.com/attribution_link?u=/watch?v=AAAAAAAAA15&feature=share&a=9QlmP1yvjcllp0h3l0NwuA",
    	"http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&u=/watch?v=AAAAAAAAA16&feature=em-uploademail",
    	"http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&feature=em-uploademail&u=/watch?v=AAAAAAAAA17",
    	"http://www.youtube.com/v/A-AAAAAAA18?fs=1&rel=0"
    };
    
    foreach (var entry in urlList)
    {
    	Console.WriteLine(Youtube.GetVideoId(entry));
    }
    Console.Read();
    

    will result in the follwing output:

    Youtube VideoId Regex Console Window Output

    Youtube VideoId Regex Console Window Output




    A simpler but less matching solution can be found here: How to get the VideoID of a youtube video using Regex



    How to set a Property Value by Name in C# and VB.NET

    To set a Property Value by Name in C# and VB.NET you can use the following snippet.

    Sample C#

    public static bool SetPropertyByName(this Object obj, string name, Object value)
    {
    	var prop = obj.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
    	if (null == prop || !prop.CanWrite) return false;
    	prop.SetValue(obj, value, null);
    	return true;
    }
    

    Sample VB.NET

    Public Shared Function SetPropertyValueByName(obj As [Object], name As String, value As [Object]) As Boolean
    	Dim prop = obj.[GetType]().GetProperty(name, BindingFlags.[Public] Or BindingFlags.Instance)
    	If prop Is Nothing OrElse Not prop.CanWrite Then
    		Return False
    	End If
    	prop.SetValue(obj, value, Nothing)
    	Return True
    End Function
    




    How to check if Directory has Files in C# and VB.NET

    To check if Directory has Files in C# and VB.NET you can use the following snippet.

    Sample C#

    public static bool HasFilesInFolder(DirectoryInfo folder)
    {
    	return folder.GetFiles("*.*").Any();
    }
    

    Sample VB.NET

    Public Shared Function HasFilesInFolder(folder As DirectoryInfo) As Boolean
    	Return folder.GetFiles("*.*").Any()
    End Function
    




    How to read a File to Textreader in C# and VB.NET

    To read a File to Textreader in C# and VB.NET you can use the following snippet.

    Sample C#

    TextReader reader = File.OpenText(@"C:\dummy.txt");
    

    Sample VB.NET

    Dim reader As TextReader = File.OpenText(@"C:\dummy.txt")
    




    How to convert a boolean to Json Boolean in C# and VB.NET

    To convert a boolean to Json Boolean in C# and VB.NET you can use the following snippet.

    Sample C#

    public static string ToJson(bool inputVal)
    {
    	return inputVal ? "true" : "false";
    }
    

    Sample VB.NET

    Public Shared Function ToJson(inputVal As Boolean) As String
    	Return If(inputVal, "true", "false")
    End Function