Skip to content

Instantly share code, notes, and snippets.

@bcr
Created November 27, 2010 06:44
Show Gist options
  • Save bcr/717647 to your computer and use it in GitHub Desktop.
Save bcr/717647 to your computer and use it in GitHub Desktop.
Get a stream on a URL or a file
private static Stream GetFileOrUrlStream(string filenameOrUrl)
{
// This function should accept the following formats:
//
// http://example.com/downloads/mybitstream.bit
// mybitstream.bin
// ..\..\mybitstream.bin
// C:\dir\mybitstream.bin
// If there is no colon, then it must be a file reference. Make it
// a full path (factor out any ".." and stuff) and let Uri do the
// rest.
// If it has a colon, it's either a) a drive reference before it,
// or b) a URL scheme before it. In both cases the Uri constructor
// will do the right thing.
if (!filenameOrUrl.Contains(":"))
{
filenameOrUrl = Path.GetFullPath(filenameOrUrl);
}
return WebRequest.Create(new Uri(filenameOrUrl)).GetResponse().GetResponseStream();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment