Created
November 27, 2010 06:44
-
-
Save bcr/717647 to your computer and use it in GitHub Desktop.
Get a stream on a URL or a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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