How to deep copy a IList in C# and VB.NET

To deep copy a IList in C# and VB.NET you can use the snippet.

Sample C#

public static IList<T> Clone<T>(IList<T> listToClone) where T : ICloneable
{
	return listToClone.Select(item => (T)item.Clone()).ToList();
}

Sample VB.NET

Public Shared Function Clone(Of T As ICloneable)(listToClone As IList(Of T)) As IList(Of T)
	Return listToClone.[Select](Function(item) DirectCast(item.Clone(), T)).ToList()
End Function

Please rate this snippet

How to copy one stream to another in C# and VB.NET

To copy one stream to another in C# and VB.NET you can use the following snippet.

Sample C#

//.NET 3.5
public static void CopyStream(Stream inputStream, Stream outputStream)
{
	var bytes = new byte[4096];
	int count;
	while ((count = inputStream.Read(bytes, 0, bytes.Length)) > 0)
	{
		outputStream.Write(bytes, 0, count);
	}
}

//.NET 4.0 +
public static void CopyStream(Stream inputStream, Stream outputStream)
{
	inputStream.CopyTo(outputStream, 4096);
}

//.NET 4.5+
 inputStream.CopyToAsync(outputStream, 4096);

Sample VB.NET

'.NET 3.5
Public Shared Sub CopyStream(inputStream As Stream, outputStream As Stream)
	Dim bytes = New Byte(4095) {}
	Dim count As Integer
	While (InlineAssignHelper(count, inputStream.Read(bytes, 0, bytes.Length))) > 0
		outputStream.Write(bytes, 0, count)
	End While
End Sub

'.NET 4.0 +
Public Shared Sub CopyStream(inputStream As Stream, outputStream As Stream)
	inputStream.CopyTo(outputStream, 4096)
End Sub

'.NET 4.5 +
inputStream.CopyToAsync(outputStream, 4096)

Please rate this snippet

How to copy a Folder/Directory recursive in C# and VB.NET

How to copy a Folder/Directory recursive in C# and VB.NET.

Sample C#

public void RecursiveFolderCopy(string sourceDirectory, string destinationDirectory, bool recursive)
{
	var dir = new DirectoryInfo(sourceDirectory);
	var dirs = dir.GetDirectories();

	if (!dir.Exists)
	{
		throw new DirectoryNotFoundException(
			String.Format("Source directory {0} does not exist!",
				sourceDirectory));
	}

	if (!Directory.Exists(destinationDirectory))
	{
		Directory.CreateDirectory(destinationDirectory);
	}

	var files = dir.GetFiles();
	foreach (var file in files)
	{
		var temppath = Path.Combine(destinationDirectory, file.Name);
		file.CopyTo(temppath, false);
	}

	if (!recursive)
	{
		return;
	}
	foreach (var subdir in dirs)
	{
		var temppath = Path.Combine(destinationDirectory, subdir.Name);
		RecursiveFolderCopy(subdir.FullName, temppath, recursive);
	}
}

Sample VB.NET

Public Sub RecursiveFolderCopy(ByVal sourceDirectory As String, ByVal destinationDirectory As String, ByVal recursive As Boolean)
        Dim dir As var = New DirectoryInfo(sourceDirectory)
        Dim dirs As var = dir.GetDirectories
        If Not dir.Exists Then
            Throw New DirectoryNotFoundException(String.Format("Source directory {0} does not exist!", sourceDirectory))
        End If
        If Not Directory.Exists(destinationDirectory) Then
            Directory.CreateDirectory(destinationDirectory)
        End If
        Dim files As var = dir.GetFiles
        For Each file As var In files
            Dim temppath As var = Path.Combine(destinationDirectory, file.Name)
            file.CopyTo(temppath, false)
        Next
        If Not recursive Then
            Return
        End If
        For Each subdir As var In dirs
            Dim temppath As var = Path.Combine(destinationDirectory, subdir.Name)
            RecursiveFolderCopy(subdir.FullName, temppath, recursive)
        Next
    End Sub

Please rate this snippet

How to paste text or image from clipboard in C# and VB.NET

Sample C#

public string PasteTextFromClipboard()
{
	var dataObject = System.Windows.Forms.Clipboard.GetDataObject();
	var o = System.Windows.Forms.Clipboard.GetDataObject();
	if (o != null)
	{
		return dataObject != null && dataObject.GetDataPresent(DataFormats.Text) ? o.GetData(DataFormats.Text).ToString() : "";
	}
	return String.Empty;
}

public Image PasteImageFromClipboard()
{
	var dataObject = System.Windows.Forms.Clipboard.GetDataObject();
	if (dataObject == null || !dataObject.GetDataPresent(DataFormats.Bitmap)) return (null);
	var o = System.Windows.Forms.Clipboard.GetDataObject();
	if (o != null)
	{
		return ((Bitmap) o.GetData(DataFormats.Bitmap));
	}
	return (null);
}

Sample VB.NET

Public Function PasteTextFromClipboard() As String
	Dim dataObject = System.Windows.Forms.Clipboard.GetDataObject()
	Dim o = System.Windows.Forms.Clipboard.GetDataObject()
	If o IsNot Nothing Then
		Return If(dataObject IsNot Nothing AndAlso dataObject.GetDataPresent(DataFormats.Text), o.GetData(DataFormats.Text).ToString(), "")
	End If
	Return [String].Empty
End Function

Public Function PasteImageFromClipboard() As Image
	Dim dataObject = System.Windows.Forms.Clipboard.GetDataObject()
	If dataObject Is Nothing OrElse Not dataObject.GetDataPresent(DataFormats.Bitmap) Then
		Return (Nothing)
	End If
	Dim o = System.Windows.Forms.Clipboard.GetDataObject()
	If o IsNot Nothing Then
		Return DirectCast(o.GetData(DataFormats.Bitmap), Bitmap)
	End If
	Return (Nothing)
End Function

Please rate this snippet

How to copy a text or a image to the windows clipboard

To copy a text or a image to the windows clipboard you can use the following snippet.

Sample C#

public void CopyToClipboard(string input, bool keepAlive)
{
    System.Windows.Forms.Clipboard.SetDataObject(input, keepAlive);
}

public void CopyToClipboard(Image img, bool keepAlive)
{
    System.Windows.Forms.Clipboard.SetDataObject(img, keepAlive);
}

Sample VB.NET

Public Sub CopyToClipboard(input As String, keepAlive As Boolean)
	System.Windows.Forms.Clipboard.SetDataObject(input, keepAlive)
End Sub

Public Sub CopyToClipboard(img As Image, keepAlive As Boolean)
	System.Windows.Forms.Clipboard.SetDataObject(img, keepAlive)
End Sub

for more informations see the MSDN Clipboard.SetDataObject Method

Please rate this snippet