Access to the path denied error in C#

You are trying to create a FileStream object for a directory (folder). Specify a file name (e.g. @”D:\test.txt”) and the error will go away. By the way, I would suggest that you use the StreamWriter constructor that takes an Encoding as its second parameter, because otherwise you might be in for an unpleasant surprise when … Read more

File.ReadLines without locking it?

No… If you look with Reflector you’ll see that in the end File.ReadLines opens a FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, FileOptions.SequentialScan); So Read-only share. (it technically opens a StreamReader with the FileStream as described above) I’ll add that it seems to be child’s play to make a static method to do it: public static IEnumerable<string> … Read more

OutOfMemoryException when send big file 500MB using FileStream ASPNET

I’ve created download page which allows user to download up to 4gb (may be more) few months ago. Here is my working snippet: private void TransmitFile(string fullPath, string outFileName) { System.IO.Stream iStream = null; // Buffer to read 10K bytes in chunk: byte[] buffer = new Byte[10000]; // Length of the file: int length; // … Read more

PHP Streaming MP3

Here’s what did the trick. $dir = dirname($_SERVER[‘DOCUMENT_ROOT’]).”/protected_content”; $filename = $_GET[‘file’]; $file = $dir.”https://stackoverflow.com/”.$filename; $extension = “mp3”; $mime_type = “audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3”; if(file_exists($file)){ header(‘Content-type: {$mime_type}’); header(‘Content-length: ‘ . filesize($file)); header(‘Content-Disposition: filename=”‘ . $filename); header(‘X-Pad: avoid browser bug’); header(‘Cache-Control: no-cache’); readfile($file); }else{ header(“HTTP/1.0 404 Not Found”); }

FileUpload to FileStream

Since FileUpload.PostedFile.InputStream gives me Stream, I used the following code to convert it to byte array public static byte[] ReadFully(Stream input) { byte[] buffer = new byte[input.Length]; //byte[] buffer = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); … Read more