filestream

How to correctly open a FileStream for usage with an XDocument

左心房为你撑大大i 提交于 2019-11-29 13:50:56
I want to append some nodes to an xml document using Linq2XML. The file in question is being used by other processes and they should be able to read the file while I update it. So I came up with this solution, which obviously isn't the correct way (The method doc.Save() fails and says that another process is using the file): using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { doc = XDocument.Load(new StreamReader(fs)); doc.Root.Add(entry); doc.Save(filename); fs.Close(); } Any help is greatly appreceated. Load the document, close the stream,

Will a using clause close this stream?

情到浓时终转凉″ 提交于 2019-11-29 13:07:23
I've apparently worked myself into a bad coding habit. Here is an example of the code I've been writing: using(StreamReader sr = new StreamReader(File.Open("somefile.txt", FileMode.Open))) { //read file } File.Move("somefile.txt", "somefile.bak"); //can't move, get exception that I the file is open I thought that because the using clause explicitly called Close() and Dispose() on the StreamReader that the FileStream would be closed as well. The only way I could fix the problem I was having was by changing the above block to this: using(FileStream fs = File.Open("somefile.txt", FileMode.Open))

Using FileStream.Seek

一曲冷凌霜 提交于 2019-11-29 12:24:52
I am trying to work with FileStream.Seek to quickly jump to a line and read it. However, I am not getting the right results. I have tried to look at this for a while and can't understand what I am doing wrong. Environment: OS: Windows 7 Framework: .NET 4.0 IDE: Visual C# Express 2010 Sample Data in file location: C:\Temp\Temp.txt 0001|100!2500 0002|100!2500 0003|100!2500 0004|100!2500 0005|100!2500 0006|100!2500 0007|100!2500 0008|100!2500 0009|100!2500 0010|100!2500 The code: class PaddedFileSearch { private int LineLength { get; set; } private string FileName { get; set; } public

Return Stream from WCF service, using SqlFileStream

雨燕双飞 提交于 2019-11-29 11:57:44
问题 I have a WCF service, from which users can request large datafiles (stored in an SQL database with FileStream enabled). These files should be streamed, and not loaded into memory before sending them off. So I have the following method that should return a stream, which is called by the WCF service, so that it can return the Stream to the client. public static Stream GetData(string tableName, string columnName, string primaryKeyName, Guid primaryKey) { string sqlQuery = String.Format( "SELECT

django return file over HttpResponse - file is not served correctly

时间秒杀一切 提交于 2019-11-29 11:55:38
问题 I want to return some files in a HttpResponse and I'm using the following function. The file that is returned always has a filesize of 1kb and I do not know why. I can open the file, but it seems that it is not served correctly. Thus I wanted to know how one can return files with django/python over a HttpResponse. @login_required def serve_upload_files(request, file_url): import os.path import mimetypes mimetypes.init() try: file_path = settings.UPLOAD_LOCATION + '/' + file_url fsock = open

What does Filestream.Read return value mean? How to read data in chunks and process it?

孤人 提交于 2019-11-29 11:42:13
I'm quite new to C# so please bear with me. I'm reading (using FileStream) data (fixed size) to small array, process the data and then read again and so on to the end of file. I thought about using something like this: byte[] data = new byte[30]; int numBytesToRead = (int)fStream.Length; int offset = 0; //reading while (numBytesToRead > 0) { fStream.Read(data, offset, 30); offset += 30; numBytesToRead -= 30; //do something with the data } But I checked documentation and their examples and they stated that return value of the above read method is: "Type: System.Int32 The total number of bytes

Empty Path Name Is Not Legal [duplicate]

心不动则不痛 提交于 2019-11-29 11:17:11
This question already has an answer here: empty path name not legal 2 answers So I'm trying to compile an Asteroids game. It's almost working, all the files are in place etc etc... The issue comes when it hits this code. FileStream myFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); string myTempFile = @"F:\Documents\Junior School\Computer Programming (Java 1)\AsteroidsWithSound\AsteroidsWithSound\temp\mysound" + i.ToString() + ".wav"; It gives me an Error/Warning, not sure exactly what it is called but it says ArgumentException was unhandled. Empty path

FileSystemWatcher triggers for filestream open

纵饮孤独 提交于 2019-11-29 09:39:22
I have a filesystemwatcher that will trigger an event when a file is modified. I want to read from that file once the lock has been removed. At the moment I am just trying to open the file once the event is triggered, when A large file is being copied the file lock stays on for a while after the events have been sent, preventing the file from being opened for read access. Any suggestions? This one's actually a bit of a doozie, unless the problem space has changed significantly since I last had to deal with it. The easiest way is to simply try to open the file, catch the resulting IOException ,

FileStream and StreamWriter - How to truncate the remainder of the file after writing?

妖精的绣舞 提交于 2019-11-29 09:11:56
var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); using(var writer = new StreamWriter(fs)) writer.Write(....); If the file previously contained text and the newly-written text is shorter than what was already in the file, how do I make sure that the obsolete trailing content in the file is truncated? Note that opening the file in truncate mode isn't an option in this case. The file is already open when I receive the FileStream object. The above code is just to illustrate the stream's properties. EDIT Expanding on the answer below, the solution is: var fs = new

Get file to FileStream from remote path with another user credentials

老子叫甜甜 提交于 2019-11-29 07:49:00
In my application I use FileStream to read from a file, that is on the fileshare somewhere in the network. So my remoteFilePath variable is something like: \\computername\folder\file.pdf FileStream fileStream = new FileStream(remoteFilePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024 * 1024) Unfortunately, the user that I'm running this application with (that I'm logged into the PC with) does not have access to this fileshare. I have another user (domain, login & password), that has access to those files. Is it possible to use the other user credentials to get a file to filestream?