I have a few files in \AppData\Roaming that my app is writing to. I create the files when the application starts like this:

private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo _File = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"MyApp\myfile.txt"));
}

Later, when I write to the file with a button click, I get an error saying that the file is already in use and cannot be accessed. How would I fix this?

The code to write to the file is correct because when I remove the code above and make the files myself, the application writes to them without any issues. Therefore, I dont think the problem is with the code I use to write to the files. But, here it is for reference:

var myfile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"MyApp\myfile.txt"));
StreamWriter sw = new StreamWriter(myfile);
sw.WriteLine(textBox1.text);
sw.Close();

Thanks in advance for any help!

有帮助吗?

解决方案

There are a few concepts at play here and I am not sure that we have enough information to definitively address the root problem, but I will give you a few pointers.

You need to be aware of the FileMode, FileAccess and FileShare enumerations.

The first, FileMode, specifies what you intend to do with regard to the file's existence. There are various options, documented in the link above. mI don't think that you have a problem here, but it bears mentioning.

The second, FileAccess, concerns your intended interaction with the file (read, write, or both). If you ask for access to read, then anyone else who opens the file or had it open already (including that web browser control) must have allowed sharing with other readers.

The final one, FileShare, defines who you are willing to share access to the file with. You can specify that others can read it, others can write to it, both, or neither.

The code that you are using is accessing the file using the very simplest defaults, which may be incompatible with the WebBrowser's access mode. Here's what I'd suggest instead:

var myfile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"MyApp\myfile.txt"));
using (var fs = new FileStream(myfile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) {
    using (var sw = new StreamWriter(fs)) {

        // You probably want to move to the end of the file before writing...
        fs.Seek(0, SeekOrigin.End);

        sw.WriteLine(textBox1.text);
        sw.Close();
    }
}

This very clearly expresses your intent, as well as the fact that you are willing to share with others who might read or write (we know the webbrowser will not write to the file, but for some reason maybe it is trying to open it with write intent anyway).

其他提示

For file operations (as well as anytime your are accessing unmanaged resources) your best bet is to only grab a handle to the file long enough to perform the operation you want and then release it.

In your case, you are opening the resource, then trying to open it again later. Change this. Don't create the files until you are actually going to do something with it. Also, look into the USING clause. You want to release it as soon as you are done reading or writing from it.

Try to Access the FileInfo object to create/append/write files as follows, the file is already been taken by FileInfo class,

use as following,

 FileInfo fi1 = new FileInfo(path);

        //Create a file to write to.
        using (StreamWriter sw = fi1.CreateText()) 
        {
            sw.WriteLine("Hello");
            sw.WriteLine("And");
            sw.WriteLine("Welcome");
        }   

        //Open the file to read from.
        using (StreamReader sr = fi1.OpenText()) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);


            }
         } 

Try using using.

using (StreamWriter sw = new StreamWriter(myfile))
{
    sw.Write(textBox1.text);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top