filestream

How to check if a file is in use?

大憨熊 提交于 2019-12-01 18:48:50
Is there any way to first test if a file is in use before attempting to open it for reading? For example, this block of code will throw an exception if the file is still being written to or is considered in use: try { FileStream stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read); } catch (IOException ex) { // ex.Message == "The process cannot access the file 'XYZ' because it is being used by another process." } I've looked all around and the best I can find is to perform some sort of polling with a try catch inside, and that feels so hacky. I would expect there

C# - FileStream: both lock a file and at the same time be able to read it without truncating it and write it with truncating it

廉价感情. 提交于 2019-12-01 17:26:12
I suppose my title isn't that clear. I'll try to explain: I can write and read a file using a FileStream FileStream fs = new FileStream("C:\\Users\\Public\\text.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); private void button1_Click(object sender, EventArgs e) { fs.Seek(0,0); StreamReader sr = new StreamReader(fs); textbox.Text = sr.ReadToEnd(); } private void button2_Click(object sender, EventArgs e) { StreamWriter sw = new StreamWriter(fs); sw.Write(textbox.Text); sw.Flush(); } This way other programs can't use the file, but I also can't delete content. Writing to it

file IO, is this a bug in Powershell?

坚强是说给别人听的谎言 提交于 2019-12-01 16:02:39
I have the following code in Powershell $filePath = "C:\my\programming\Powershell\output.test.txt" try { $wStream = new-object IO.FileStream $filePath, [System.IO.FileMode]::Append, [IO.FileAccess]::Write, [IO.FileShare]::Read $sWriter = New-Object System.IO.StreamWriter $wStream $sWriter.writeLine("test") } I keep getting error: Cannot convert argument "1", with value: "[IO.FileMode]::Append", for "FileStream" to type "System.IO.FileMode": "Cannot convert value "[IO.FileMode]::Append" to type "System.IO.FileMode" due to invalid enumeration values. Specify one of the following enumeration

FileStream Read/Write method's limitation

社会主义新天地 提交于 2019-12-01 15:27:15
FileStream's read/write method can take only integer value as length. But FileStream object returns length in long . In this case, what if file size is larger than integer value (approximate more than 2GB). Then how FileStream's read/write method handle long value. Then you read and write in multiple chunks. The CLR has a limit on the size of any particular object anyway (also around 2GB IIRC, even on a 64-bit CLR), so you couldn't have a byte array big enough for it to be a problem. You should always loop when reading anyway, as you can't guarantee that a Read call will read as many bytes as

FileStream Read/Write method's limitation

纵然是瞬间 提交于 2019-12-01 15:12:53
问题 FileStream's read/write method can take only integer value as length. But FileStream object returns length in long . In this case, what if file size is larger than integer value (approximate more than 2GB). Then how FileStream's read/write method handle long value. 回答1: Then you read and write in multiple chunks. The CLR has a limit on the size of any particular object anyway (also around 2GB IIRC, even on a 64-bit CLR), so you couldn't have a byte array big enough for it to be a problem. You

file IO, is this a bug in Powershell?

女生的网名这么多〃 提交于 2019-12-01 14:58:32
问题 I have the following code in Powershell $filePath = "C:\my\programming\Powershell\output.test.txt" try { $wStream = new-object IO.FileStream $filePath, [System.IO.FileMode]::Append, [IO.FileAccess]::Write, [IO.FileShare]::Read $sWriter = New-Object System.IO.StreamWriter $wStream $sWriter.writeLine("test") } I keep getting error: Cannot convert argument "1", with value: "[IO.FileMode]::Append", for "FileStream" to type "System.IO.FileMode": "Cannot convert value "[IO.FileMode]::Append" to

PHP Pass File Handle to user so that file downloads & saves to their machine

[亡魂溺海] 提交于 2019-12-01 13:23:29
问题 I am downloading a file from another server. I wish to push this file to my users rather than saving it to my server. In other words, pass them the file handle so it just passes through my server and saves to their machine. How can I do this? I have this so far: $handle = fopen($_GET['fileURL'], 'r'); $filename = stream_get_contents($handle); How do I push this to the user, maybe using headers? Thank you for any help and direction. EDIT I have the headers: header("Pragma: public"); //

Reading a file from a UNC path and setting the correct MIME type in a HTTP request

最后都变了- 提交于 2019-12-01 10:16:03
问题 How would I go about reading a file from a UNC path, discovering the proper MIME type, and streaming that out to a browser? It feels to me like I'm re-inventing IIS, and I'll also have to maintain my own MIME type database for each file extension. Does the above request sound reasonable, or is there a better way? I plan on streaming this out via a browser HTTP Get request on IIS7. If it matters, I'm also running Cognos on the same server. Any framework is OK (WCF, ASPX, etc) 回答1: Using WCF

Windows Phone 7 : FileStream exception

时间秒杀一切 提交于 2019-12-01 09:05:38
I try to use FileStream (using namespace System.IO) but I get an exception : Attempt to access the method failed Here is the code : FileStream fs = new FileStream("file.txt", FileMode.Create); I searched on microsoft.com and I found that this error is because I use a bad library reference. But in my project, I compile with mscorlib.dll from folder : C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0 I need some help, please. You will need to use IsolatedStorage , for example: Place at the top of your file: using System.IO.IsolatedStorage; Then in your method do

Dynamically creating a file with node.js and make it available for download

人走茶凉 提交于 2019-12-01 08:10:54
I am building a text editor in Node.js where a user create a file in a textarea. When he is done editing the file, he can push an "export" button that triggers a Jquery function that reads the textarea and post the text on the node.js server. The server should read the information and return a file. I would like to avoid creating a file on the server and servicing it, I'd rather create a file on the fly with streams. I have tried using the following and but that didn't work: exports.exportfile = function(req,res){ var Stream = require('stream') var stream = new Stream(); res.setHeader('Content