How to combine multiple Uri in C# and VB.NET

To combine multiple Uri in C# and VB.NET you can use the following snippet.

Simple Sample (combining 2 uri)

Sample C#

public string CombineUri(string uri1, string uri2)
{
	if (uri1.Length == 0){return uri2;}
	if (uri2.Length == 0){return uri1;}
	uri1 = uri1.TrimEnd('/', '\\');
	uri2 = uri2.TrimStart('/', '\\');
	return string.Format("{0}/{1}", uri1, uri2);
}

Sample VB.NET (autogenerated)

Public Function CombineUri(uri1 As String, uri2 As String) As String
	If uri1.Length = 0 Then
		Return uri2
	End If
	If uri2.Length = 0 Then
		Return uri1
	End If
	uri1 = uri1.TrimEnd("/", "\")
	uri2 = uri2.TrimStart("/", "\")
	Return String.Format("{0}/{1}", uri1, uri2)
End Function

UPDATE 23.06.2015

Combine multiple Uri

Since this snippet is one of our most read snippets of all time, i decided to upgrade this snippet.
This method now uses params string[] as method parameter which allows much more uri strings to be combine at once.

Sample C#

public string CombineUri(params string[] uriParams)
{
	var retVal = string.Empty;
	foreach (var uriParam in uriParams)
	{
		var tempParam = uriParam.Trim();
		if (!String.IsNullOrEmpty(tempParam))
		{
			tempParam = uriParam.TrimEnd('/', '\\');
			retVal = !String.IsNullOrEmpty(retVal) ? string.Format("{0}/{1}", retVal, tempParam) : string.Format("{0}", tempParam);
		}
	}
	return retVal;
}

Sample VB.NET (autogenerated)

Public Function CombineUri(ParamArray uriParams As String()) As String
	Dim retVal = String.Empty
	For Each uriParam As var In uriParams
		Dim tempParam = uriParam.Trim()
		If Not [String].IsNullOrEmpty(tempParam) Then
			tempParam = uriParam.TrimEnd("/"C, "\"C)
			retVal = If(Not [String].IsNullOrEmpty(retVal), String.Format("{0}/{1}", retVal, tempParam), String.Format("{0}", tempParam))
		End If
	Next
	Return retVal
End Function

For more informations about params see the msdn: params (C# Reference)

Please rate this snippet

How to get the BuildDate of a Assembly in C# and VB.NET

Sample C#

public DateTime GetBuildDate(Assembly assembly)
{
	var location = assembly.Location;
	const int headerOffset = 60;
	const int linkerTimestampOffset = 8;
	var buffer = new byte[2048];
	Stream stream = null;

	try
	{
		stream = new FileStream(location, FileMode.Open, FileAccess.Read);
		stream.Read(buffer, 0, 2048);
	}
	finally
	{
		if (stream != null)
		{
			stream.Close();
		}
	}

	var i = BitConverter.ToInt32(buffer, headerOffset);
	var secondsSince1970 = BitConverter.ToInt32(buffer, i + linkerTimestampOffset);
	var dt = new DateTime(1970, 1, 1, 0, 0, 0);
	dt = dt.AddSeconds(secondsSince1970);
	dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
	return dt;
}

Sample VB.NET

Public Function GetBuildDate(assembly as Assembly) As DateTime
	Dim location = assembly.Location
	Const  headerOffset As Integer = 60
	Const  linkerTimestampOffset As Integer = 8
	Dim buffer = New Byte(2047) {}
	Dim stream As Stream = Nothing

	Try
		stream = New FileStream(location, FileMode.Open, FileAccess.Read)
		stream.Read(buffer, 0, 2048)
	Finally
		If stream IsNot Nothing Then
			stream.Close()
		End If
	End Try

	Dim i = BitConverter.ToInt32(buffer, headerOffset)
	Dim secondsSince1970 = BitConverter.ToInt32(buffer, i + linkerTimestampOffset)
	Dim dt = New DateTime(1970, 1, 1, 0, 0, 0)
	dt = dt.AddSeconds(secondsSince1970)
	dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours)
	Return dt
End Function

Please rate this snippet

How to use the assembly InternalsVisibleTo attribute

If your code contains classes, interfaces or structs that have the internal (c#) access qualifier or the Friend (VB.NET) access qualifier, you cannot access them from another assembly (e.g. unit testing). Types have this qualifier for one of two reasons

– You have explicitly marked them as internal.
– By default, top-level classes, interfaces, or structs that have no explicit access qualifier are treated as internal.

But what if you need to make them visible just for a specific assembly? Thats where the assembly InternalsVisibleTo attribute comes into play.
If your Assembly which has the Internal access qualifiers has no strong name you simply have to put the following line of code to its AssemblyInfo-File.

Sample C#

[assembly:InternalsVisibleTo("OtherAssemblyName")]

Sample VB.NET

<Assembly:InternalsVisibleTo("OtherAssemblyName")>

If your Assembly which has the Internal access qualifiers has strong name you simply have to put the following line of code to its AssemblyInfo-File.
The PublicKey depends on your Assembly, so you need to put the right PublicKey in it, check the links on the bottom of this Posts on how to get the PublicKey.

Sample C#

[assembly:InternalsVisibleTo("OtherAssemblyName,PublicKey=00240000048000009400000006020000002400005253413100040000010001002B4D3670141EEBB497143AD5400105B520B65C7CC91076C86BDEBDFB9BBB527D4B98F51F70D12B92008DB4D0715C26E7C4857CD1D44EC12AD17CED8E7DC94CD4F28E4962BB88EF6224F4BC6C1A7F35238383B70E56323B48B1C57F4E0510C0E9C5625A0A485AB22A03C7B199CB529FB234D279F883633A18509AC231B643E1E5")]

Sample VB.NET

<Assembly:InternalsVisibleTo("OtherAssemblyName,PublicKey=00240000048000009400000006020000002400005253413100040000010001002B4D3670141EEBB497143AD5400105B520B65C7CC91076C86BDEBDFB9BBB527D4B98F51F70D12B92008DB4D0715C26E7C4857CD1D44EC12AD17CED8E7DC94CD4F28E4962BB88EF6224F4BC6C1A7F35238383B70E56323B48B1C57F4E0510C0E9C5625A0A485AB22A03C7B199CB529FB234D279F883633A18509AC231B643E1E5")>

More informations: Setting the InternalsVisibleTo Attribute, Getting Public Key Token of Assembly Within Visual Studio, AssemblyHelperTool

Please rate this snippet