2

i got a file that is store in my appliction directory, and he got some site list. i dont have any problem reading it, but when i want to write to it, i get

System.ArgumentException: Stream is not writeable

this is how i accsess the file:

  FileStream theTextFileStream = new FileStream(Environment.CurrentDirectory + "/fourmlinks.txt",FileMode.OpenOrCreate);

and this is the function that throw me the expection:

public static void WriteNewTextToFile(string text, FileStream theFile)
    {
        string fileText = GetAllTextFromFile(theFile);
        ArrayList fileLIst = populateListFromText(fileText);

        using (StreamWriter fileWriter = new StreamWriter(theFile))
        {
            fileWriter.Write(String.Empty);
            for (int i = 0; i < fileLIst.Count; i++)
            {
                fileWriter.WriteLine(fileLIst[i].ToString());        
            }
        }
    }

the function read the old and new text and add it to an arry. then i clean the file from every thing, and rewriting it with the old and new data from the arry i made.

i dont know if that will help but here is the file proprites:

Build Action: None
Copy To Out Put Directory: Copy always

why i cant rewrite the file?

this is the function i use to read the file content:

public static string GetAllTextFromFile(FileStream theFile)
    {
        string fileText = "";

        using (theFile)
        {
            using (StreamReader stream = new StreamReader(theFile))
            {
                string currentLine = "";
                while ((currentLine = stream.ReadLine()) != null)
                {
                    fileText += currentLine + "\n";
                }

            }
        }

        return fileText;

    }
Mario S
  • 11,715
  • 24
  • 39
  • 47
samy
  • 1,949
  • 6
  • 39
  • 64
  • Are you using the same `FileStream` that you used to open the file? – Dave Zych Oct 17 '12 at 20:28
  • How do you *read* from the file? – Jon Skeet Oct 17 '12 at 20:30
  • i added the function that i use to read the file. – samy Oct 17 '12 at 20:33
  • Are you certified that your file name is correct?! "fourmlinks" – felipekm Oct 17 '12 at 20:34
  • I find it interesting that you've disposed of the stream after reading it, yet you're not getting a `ObjectDisposedException` when you attempt to write to it. I would think that would be raised before your `ArgumentException` – Dave Zych Oct 17 '12 at 20:35
  • @FelipeKM yes im sure, i open it and read is content when i open the app and i can see all the content. – samy Oct 17 '12 at 20:37
  • make sure your file is not opened in some other application - notepad or similar – Ondra Oct 17 '12 at 20:48
  • 1
    BTW, don't trust `Environment.CurrentDirectory` will give you, at any time, the path to the directory you expect. Use this method instead: `System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location))`. Taken from this other [SO thread](http://stackoverflow.com/questions/253468/whats-the-best-way-to-get-the-directory-from-which-an-assembly-is-executing). – Luis Quijada Oct 17 '12 at 21:06

1 Answers1

3

You have to use Read/Write file access as third parameter -

FileStream theTextFileStream = new FileStream(Environment.CurrentDirectory + "/fourmlinks.txt",FileMode.OpenOrCreate, FileAccess.ReadWrite
);

Important - Remove using(theFile) statement:

public static string GetAllTextFromFile(FileStream theFile)
{
        string fileText = "";

        using (StreamReader stream = new StreamReader(theFile))
        {
            string currentLine = "";
            while ((currentLine = stream.ReadLine()) != null)
            {
                fileText += currentLine + "\n";
            }

        }


    return fileText;

}

Do not use using construct in your case as it will close the underlying stream as in your case you have to manually open and close stream objects.

This will allow you to write in the file as well.

For more information refer following links -

Parag Meshram
  • 8,281
  • 10
  • 52
  • 88
  • still not working, im getting the same exeption. i open the file in a Form i made on runtime, can that effect the stream? – samy Oct 17 '12 at 20:46
  • i added the file with visual studio, how can i use visual studio to see if he is read only? – samy Oct 17 '12 at 20:49
  • Remove `using (theFile)` from `GetAllTextFromFile` method – Parag Meshram Oct 17 '12 at 20:53
  • `using (StreamReader stream = new StreamReader(theFile))` also closes the underlying file stream object http://msdn.microsoft.com/en-us/library/system.io.streamreader.close.aspx – zahir Oct 17 '12 at 20:55
  • yes the problem was that i needed to make sure the stream is still alive, i change some stuff and i just reopen it every time i needed to do somthing. thx – samy Oct 17 '12 at 21:00