StreamWriter and IsolatedStorageFile - c#

IsolatedStorageFile iF = IsolatedStorageFile.GetUserStoreForApplication();
if (!iF.DirectoryExists("aaa"))
{
MessageBox.Show("No directory, create!");
iF.CreateDirectory("aaa");
}
StreamWriter fW = new StreamWriter(new IsolatedStorageFileStream("girls\\list.txt", FileMode.OpenOrCreate, iF));
fW.WriteLine(this.tb_name.Text);
So, I create file, or open it, and add to it content of textbox. I need append this file, but it rewrites. Please, help me to solve this problem :) Thank you!

You want FileMode.Append, not FileMode.OpenOrCreate
See this page for details http://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.95).aspx
Append: Opens the file if it exists and seeks to the end of the file, or
creates a new file.

Use FileMode.Append for if it exists, FileMode.Create if it does not.

Related

BinaryWriter to overwrite an existing file c#

To write a picture on the memory of my pocket pc i use the following code:
pic = (byte[])myPicutureFromDatabase;
using (var fs = new BinaryWriter(new FileStream(filepath, FileMode.Append, FileAccess.Write)))
{
fs.Write(pic);
fs.Flush();
continue;
}
I wanted to ask you if this method overwrite the file with new values if the file with this name already exist or do nothing because already exist this file?
I need to overwrite the file in eventuality that this file already exist but with old value.
From MSDN FileMode.Create
Specifies that the operating system should create a new file. If the
file already exists, it will be overwritten. This requires
FileIOPermissionAccess.Write permission. FileMode.Create is equivalent
to requesting that if the file does not exist, use CreateNew;
otherwise, use Truncate. If the file already exists but is a hidden
file, an UnauthorizedAccessException exception is thrown.
Where as FileMode.Append
Opens the file if it exists and seeks to the end of the file, or
creates a new file. This requires FileIOPermissionAccess.Append
permission. FileMode.Append can be used only in conjunction with
FileAccess.Write. Trying to seek to a position before the end of the
file throws an IOException exception, and any attempt to read fails
and throws a NotSupportedException exception.
So, you should use this
pic = (byte[])myPicutureFromDatabase;
using (var fs = new BinaryWriter(new FileStream(filepath, FileMode.Create, FileAccess.Write)))
{
fs.Write(pic);
fs.Flush();
continue;
}
No it appends the lines, you have specified it by writing FileMode.Append, you should specify FileMode.Create in order to append lines (or create a new file if it not exists)

File is used by another process: How to solve this Error?

I am trying to open a file, but I received:
The process cannot access the file because it is being used by another process. The File is an XML-Document. Can anyone help?
string activeDirectory = #"X:\SubGraph\";
string[] files = Directory.GetFiles(activeDirectory);
foreach (string fileName in files){
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
After using a file, you must to close it, I think:
foreach (string fileName in files)
{
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//your code
file.Close();
}
If you are using this piece of code in some kind of loop you need to close your FileStream each time before finishing loop cycle.
file.Close();
Or use "using" construction like this:
using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
// your code goes here
file.Close();
}
Moreover, you better to accustom yourself to close all manually created streams after they are unnecessary anymore.
Under some circumstances Windows locks the files. In your case can be:
Another process is locking the file. It might be windows or you av software or who knows.
In order to discover who is locking the file you might several tools like wholockme or Unlocker. These tools will tell you which process is locking the file and even allow you to unlock it.
Maybe you are locking your own file. In your code snippet seems you are not closing the file you are reading (Maybe you can edit your question and add all code). You should remember to include:
file.Close();
... or file will remain open.

Rename File open by self

My program is logging data to a file, at the same time a user interface displays the incoming data live. I want the logged data to be on disk within a second or two if computer/program/os/whatever shuts down. Data is coming in at least 100 times/sec.
I want the user to be able to give the log-file a new name, while logging is active. The problem is that i can't change the name of the file while it is open, even if it is by the same process.
Test case:
string fileName1 = "test.txt";
string fileName2 = "test2.txt";
using (StreamWriter sw = new StreamWriter(new FileStream(fileName1, FileMode.Create)))
{
sw.WriteLine("before");
File.Move(fileName1, fileName2); //<<-- IOException - The process cannot access the file because it is being used by another process.
w.WriteLine("after");
}
So, How do i rename a file from a process while the same process is having a stream to the file open?
You should close the first stream, rename the file, then reopen the stream:
using (StreamWriter sw = new StreamWriter(new FileStream(fileName1, FileMode.Create)))
{
sw.WriteLine("before");
sw.Close();
}
File.Move(fileName1, fileName2);
using (StreamWriter sw = new StreamWriter(new FileStream(fileName2, FileMode.Append)))
{
sw.WriteLine("after");
}
I know this answer is a bit late to help you on your porting project but perhaps it will help others!
If you open the file with the FileShare.Delete flag it will let you rename it even though it is still open :)
You can't rename a file while it is open by a process, but if you want to write to it from the other instance of your program, do this.
Try FileShare.Write. You can use it in File.Open.
using (StreamWriter sw = new StreamWriter (File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
...
}
Opening en closing the file 100 times a second will have a impact on your performance, you can log to a temp file and append the temp file every 10 seconds or so. That will give you what you want.

add resourse files to project with code

How could I archive this:
programmatically?
Is there a way of creating resource files with code?
I think you can't watch in resources but you can use resource.
FileStream fs = new FileStream("Resources",FileMode.OpenOrCreate, FileAccess.Write);
IResourceWriter writer = new ResourceWriter(fs);
writer.AddResource("TextFile1", "SomeText");
writer.Generate();
writer.Close();

File.OpenWrite appends instead of wiping contents?

I was using the following to write to a file:
using(Stream FileStream = File.OpenWrite(FileName))
FileStream.Write(Contents, 0, Contents.Length);
I noticed that it was simply writing to file file correctly, but didn't wipe the contents of the file first. I then decided to simply use:
File.WriteAllBytes(FileName, Contents);
This worked fine.
However, why doesn't File.OpenWrite automatically delete the contents of the file as the other languages i've used do for their OpenWrite style function, and have a instead of appending?
Is there any method to do this?
This is the specified behavior for File.OpenWrite:
If the file exists, it is opened for writing at the beginning. The existing file is not truncated.
To do what you're after, just do:
using(Stream fileStream = File.Open(FileName, FileMode.Create))
fileStream.Write(Contents, 0, Contents.Length);
Your current call is equivalent to use FileMode.OpenOrCreate, which does not cause truncation of an existing file.
The FileMode.Create option will cause the File method to create a new file if it does not exist, or use FileMode.Truncate if it does, giving you the desired behavior. Alternatively, you can use File.Create to do this directly.
Yes you are right. File.OpenWrite does not overwrite the file.
The File.Create is used to overwrite the file if exists.

Categories

Resources