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

How to change the File extension in C# and VB.NET

To change the File extension in C# and VB.NET you can use the following snippet.

Sample C#

public static FileInfo ChangeExtension(FileInfo file, string newExtension)
{
	if (!newExtension.StartsWith("."))
	{
		newExtension = "." + newExtension;
	}
	var fileName = string.Concat(Path.GetFileNameWithoutExtension(file.FullName), newExtension);
	if (File.Exists(fileName))
	{
		File.Delete(fileName);
	}
	File.Move(file.FullName,fileName);
	if (File.Exists(fileName))
	{
		File.Delete(file.FullName);
	}
	return new FileInfo(fileName);
}

Sample VB.NET

Public Shared Function ChangeExtension(file__1 As FileInfo, newExtension As String) As FileInfo
	If Not newExtension.StartsWith(".") Then
		newExtension = Convert.ToString(".") & newExtension
	End If
	Dim fileName = String.Concat(Path.GetFileNameWithoutExtension(file__1.FullName), newExtension)
	If File.Exists(fileName) Then
		File.Delete(fileName)
	End If
	File.Move(file__1.FullName, fileName)
	If File.Exists(fileName) Then
		File.Delete(file__1.FullName)
	End If
	Return New FileInfo(fileName)
End Function

How to configure Conditional Mapping in AutoMapper in C# and VB.NET

To configure Conditional Mapping in AutoMapper in C# and VB.NET you can use the following snippet.

Sample C#

 Mapper.CreateMap<Source, Target>().ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.Variable1 == "Value1" ? true : false));

Sample VB.NET

Mapper.CreateMap(Of Source, Target)().ForMember(Function(dest) dest.Value, Function(opt) opt.MapFrom(Function(src) If(src.Variable1 = "Value1", True, False)))

Top 10 reads of December 2014

These are the top 10 reads of December 2014.

  • How to copy a Folder/Directory recursive in C# and VB.NET
  • How to combine multiple Uri in C# and VB.NET
  • How to get the BuildDate of a Assembly in C# and VB.NET
  • How to use the assembly InternalsVisibleTo attribute in C# and VB.NET
  • How to rename a file or folder in Java
  • How to delete a folder in Ruby
  • How to process all files in a directory in Python
  • How to read a file to string in Java
  • How to calculate PercentOf in PHP
  • How to get the char code in C
  • thanks to all visitors, bookmark us, share us, talk about us, Follow us!, Like us! let’s grow bigger!

    Top 10 reads of November 2014

    These are the top 10 reads of November 2014.

  • How to copy a Folder/Directory recursive in C# and VB.NET
  • How to combine multiple Uri in C# and VB.NET
  • How to get the BuildDate of a Assembly in C# and VB.NET
  • How to use the assembly InternalsVisibleTo attribute in C# and VB.NET
  • How to rename a file or folder in Java
  • How to delete a folder in Ruby
  • How to process all files in a directory in Python
  • How to read a file to string in Java
  • How to calculate PercentOf in PHP
  • How to get the char code in C
  • thanks to all visitors, bookmark us, share us, talk about us, Follow us!, Like us! let’s grow bigger!

    Top 10 reads of October 2014

    These are the top 10 reads of October 2014.

  • How to combine multiple Uri in C# and VB.NET
  • How to get the BuildDate of a Assembly in C# and VB.NET
  • How to use the assembly InternalsVisibleTo attribute in C# and VB.NET
  • How to rename a file or folder in Java
  • How to delete a folder in Ruby
  • How to process all files in a directory in Python
  • How to read a file to string in Java
  • How to calculate PercentOf in PHP
  • How to get the char code in C
  • How to rename a file or folder in Ruby
  • thanks to all visitors, bookmark us, share us, talk about us, Follow us!, Like us! let’s grow bigger!

    Top 10 reads of September 2014

    These are the top 10 reads of September 2014.

  • How to combine multiple Uri in C# and VB.NET
  • How to get the BuildDate of a Assembly in C# and VB.NET
  • How to use the assembly InternalsVisibleTo attribute in C# and VB.NET
  • How to rename a file or folder in Java
  • How to delete a folder in Ruby
  • How to process all files in a directory in Python
  • How to read a file to string in Java
  • How to calculate PercentOf in PHP
  • How to get the char code in C
  • How to rename a file or folder in Ruby
  • thanks to all visitors, bookmark us, share us, talk about us, Follow us!, Like us! let’s grow bigger!