How to read a embedded resource to String in C# and VB.NET

To read a embedded resource to String in C# and VB.NET you can use the snippet below.
Keep in mind that this snippet is using Encoding.Default which might not always work in all situations correctly.

Sample C#

Sample VB.NET (autogenerated)

How to get all Description Attributes of an Enum using C# or VB.NET

To get all Description Attributes of an Enum using C# or VB.NET you can use the snippet below.

Sample C# – Class

public class DescriptionAttributes<T>
    {
        protected List<DescriptionAttribute> Attributes = new List<DescriptionAttribute>();
        public List<string> Descriptions { get; set; }

        public DescriptionAttributes()
        {
            RetrieveAttributes();
            Descriptions = Attributes.Select(x => x.Description).ToList();
        }

        private void RetrieveAttributes()
        {
            foreach (var attribute in typeof(T).GetMembers().SelectMany(member => member.GetCustomAttributes(typeof(DescriptionAttribute), true).Cast<DescriptionAttribute>()))
            {
                Attributes.Add(attribute);
            }
        }
    }

Usage C#

class Program
    {
        public enum TestEnum
        {
            [Description("An apple a day keeps the doctor away")]
            Apple=0,
            [Description("Yummie Strawberry")]
            Strawberry=1,
            [Description("Banana for scale")]
            Banana=2
        }

        static void Main(string[] args)
        {
            var descriptions = new DescriptionAttributes<TestEnum>().Descriptions.ToList();
            foreach (var description in descriptions)
            {
                Console.WriteLine(description);
            }
            Console.Read();
        }
    }

Sample VB.NET – Class

Public Class DescriptionAttributes(Of T)
	Protected Attributes As New List(Of DescriptionAttribute)()
	Public Property Descriptions() As List(Of String)
		Get
			Return m_Descriptions
		End Get
		Set
			m_Descriptions = Value
		End Set
	End Property
	Private m_Descriptions As List(Of String)

	Public Sub New()
		RetrieveAttributes()
		Descriptions = Attributes.[Select](Function(x) x.Description).ToList()
	End Sub

	Private Sub RetrieveAttributes()
		For Each attribute As var In GetType(T).GetMembers().SelectMany(Function(member) member.GetCustomAttributes(GetType(DescriptionAttribute), True).Cast(Of DescriptionAttribute)())
			Attributes.Add(attribute)
		Next
	End Sub
End Class

Usage VB.NET

Class Program
	Public Enum TestEnum
		<Description("An apple a day keeps the doctor away")> _
		Apple = 0
		<Description("Yummie Strawberry")> _
		Strawberry = 1
		<Description("Banana for scale")> _
		Banana = 2
	End Enum

	Private Shared Sub Main(args As String())
		Dim descriptions = New DescriptionAttributes(Of TestEnum)().Descriptions.ToList()
		For Each description As var In descriptions
			Console.WriteLine(description)
		Next
		Console.Read()
	End Sub
End Class

Result / Output

Enum Descriptions

Enum Descriptions

How to set the ExecutionPolicy using Powershell

To set the ExecutionPolicy using Powershell, run Powershell as Administrator.
Type in Set-ExecutionPolicy followed by the ExecutionPolicy you wish to use.

  • Restricted: Restricted is the default execution policy. PowerShell scripts are not allowed to run.
  • All Signed: All Signed means thath if they are signed by a trusted publisher.
  • Remote Signed: Remote Signed means, any PowerShell scripts that have been locally created will be allowed to run.
  • Unrestricted: As the name implies, Unrestricted removes all restrictions from the execution policy.
  • Microsoft has disabled scripting by default to prevent malicious code from being executed.

    How to Set / Clear and Toggle a single bit in C/C++

    To Set / Clear and Toggle a single bit in C++ see the methods below.

    Setting a bit

    Use the bitwise OR operator (|) to set a bit.

    number |= 1 << x;
    

    Clearing a bit

    Use the bitwise AND operator (&) to clear a bit.

    number &= ~(1 << x);
    

    Toggling a bit

    The XOR operator (^) can be used to toggle a bit.

    number ^= 1 << x;
    

    Source

    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 read a embedded resource image in C# and VB.NET

    To read a embedded resource image in C# and VB.NET you can use the following snippet.

    Sample C#

    public static Image ReadEmbeddedRessourceImage(Assembly assembly, string searchPattern)
    {
    	var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(x => x.Contains(searchPattern));
    	using (var stream = assembly.GetManifestResourceStream(resourceName))
    	{
    		if (stream != null) return Image.FromStream(stream);
    	}
    	return null;
    }
    

    Sample VB.NET

    Public Shared Function ReadEmbeddedRessourceImage(assembly As Assembly, searchPattern As String) As Image
    	Dim resourceName = assembly.GetManifestResourceNames().FirstOrDefault(Function(x) x.Contains(searchPattern))
    	Using stream = assembly.GetManifestResourceStream(resourceName)
    		If stream IsNot Nothing Then
    			Return Image.FromStream(stream)
    		End If
    	End Using
    	Return Nothing
    End Function
    

    How to set the start location of FolderBrowserDialog in C# and VB.NET

    To set the start location of FolderBrowserDialog in C# and VB.NET you can use the following snippet.
    To do so, simply set the SelectedPath Value to the path you want.

    Sample C#

    MyFolderBrowserDialog.SelectedPath = @"C:\Test";
    

    Sample VB.NET

    MyFolderBrowserDialog.SelectedPath = "C:\Test"
    

    How to format bytes to human readable Size in Python

    To format bytes to human readable Size in Python you can use the following snippet.

    Sample Python

    fileSize = 12454162221
    for count in ['Bytes','KB','MB','GB']:
        if fileSize > -1024.0 and fileSize < 1024.0:
            print "%3.1f%s" % (fileSize, count)
        fileSize /= 1024.0
    print "%3.1f%s" % (fileSize, 'TB')
    

    How to format bytes to human readable Size in Javascript

    To format bytes to human readable Size in Javascript you can use the following snippet.

    Sample Javascript

    function BytesToHumanReadableSize(bytes){
    	if(!bytes) {return '0 bytes';}
        var sizeUnits = ['bytes','KB','MB','GB','TB','PB','EB','ZB','YB'];
        var calc = Math.floor( Math.log(bytes) / Math.log(1024) );
        calc = Math.min( Math.max(0,calc), sizeUnits.length-1);
        var bytesNew = Math.round((bytes/ Math.pow(1024,calc))*100)/100;
        return bytesNew + ' ' + sizeUnits[calc];
    }