I just ran up against a requirement to do a deep file and folder/directory copy in .NET (C# to be exact). A quick google didn't turn up any code, so I figured I'd post what I came up with. This is a somewhat naive implementation that may run out of steam for very deep folders or 1000s of files, but works great for most things. This implementation takes a source folder and a destination folder and copies the entire structure including files and folders from the source folder to the destination.
So without further ado:
private void CopyFolder(string folder, string destFolder)
foreach (string file in Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories))
string fileName = Path.GetFileName(file);
string filePath = Path.GetDirectoryName(file.Substring(folder.Length + (folder.EndsWith("\\") ? 0 : 1)));
string destFolderPath = Path.Combine(destFolder, filePath);
destFilePath = Path.Combine(destFolderPath, fileName);
destFilePath = Path.Combine(destFolder, fileName);
I needed a method to get the mime type of a System.Drawing Image instance that I'd loaded from an upload. All articles I was able to find online suggest some kind of lookup table solution. There's actually another way. Given an Image i, you can map from the i.RawFormat.Guid property to an ImageCodecInfo.FormatID. So it becomes a very simple for loop to get the MIME type of an image or bitmap. This works for any format System.Drawing/GDI+ supports (bmp, png, tiff, jpg/jpeg, etc). Here's the code:
public static stringGetMimeType(Imagei)
foreach (ImageCodecInfo codec inImageCodecInfo.GetImageDecoders())
if (codec.FormatID == i.RawFormat.Guid)
This won't work for images that were created on the fly (will return "image/unknown"), but will work for all images that were loaded from some source (file, stream, byte, etc).
Sometimes you want to work with all controls on an ASP.NET page by type rather than id. I finally got fed up with the lack of builtin support, and wrote the following method that returns all controls inside another control (or page) by type:
public List<T> FindControls<T>(Control parent) where <T> : Control
List<T> foundControls = new List<T>();
void FindControls<T>(Control parent, List<T> foundControls) where <T> : Control
foreach (Control c in parent.Controls)
if (c is <T>)
else if(c.Controls.Count > 0)
Many times I've looked for a way to convert an app relative url (such as one using the ~/ app root notation) to an absolute path without ResolveUrl. If the code is executing outside a Control, for example in an IHttpHandler or business layer code somewhere that has no reference to a Control, you can't call Control.ResolveUrl.
Today, I discovered a way to do this without a Control reference. The System.Web.VirtualPathUtility class has some very useful methods for manipulating paths, including one that converts from an app relative path to an absolute path. Instead of Control.ResolveUrl, just call VirtualPathUtility.ToAbsolute:
string url =VirtualPathUtility.ToAbsolute(url);
Viola! An app relative path to an absolute path in one line of code without using Control.ResolveUrl.
I always try to develop my sites to be standards compliant, and this means using css for all formatting and positioning when possible. I keep having to look up how to center an image, so I decided to write a post so I always know where this information is.
To center an image, set the left and right margins to auto. This will make the margins fill the available space. You must also set the display to block so the image is treated as a content block. Here's a full css definition: