file io causes exception

Hello all,

I'm having a problem with file io. Here's my code, though already suspect it has little to do with the problem...

Random rand = new Random(DateTime.Now.Millisecond); // random is a function that I made.

int i = 0;

while (i < 100)

{

StreamWriter newstream = new StreamWriter("output.txt");

newstream.WriteLine(rand.GenerateRand());

++i;

}

Anyways, I'm getting a system.security.securityexception as soon as the executable starts. I googled it and found that it probably has to do with the fact that I'll be writing directly to a network share and the permissions being inherited do not allow for direct file-io. I couldn't find how to change them, however. Can someone point me in the right direction here Thanks!




Answer this question

file io causes exception

  • jambi

    All of your input on this matter was helpful. Putting the streamwriter in the loop was my bad.

    However, I'm having still having this problem when I go to debug. When I run it without debugging, the application doesn't crash, but it generates an exception when I debug it.

    Additional information: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

    That is what is generated. I followed the instructions on the blog to add a strong name to my assembly, and that worked out and eliminated the big exception when I went to run it without debugging...any other ideas Thanks in advance.


  • Brad Hehe

    do you have permission to read/write to that directory

  • Huckster

    The following link might be useful for you:

    http://blogs.msdn.com/shawnfa/archive/2003/06/20/57023.aspx

    Did that solve it


  • centexbi

    Hi

    Sorry, but I don't understand what is the point of that procedure. Do you really want open same file at the same time max. 100 times

    If so, you must open files with FileShare.Write access.

    If don't you should move file open command outside the loop.

    Markku


  • snakeoooooo

    Can you try this code:
    StreamWriter newstream = File.CreateText("output.txt");
    Random
    rand = new Random(DateTime.Now.Millisecond); // random is a function that I made.
    int i = 0;
    while (i < 100)
    {
    newstream.WriteLine(rand.Next());
    ++i;
    }

    for this purpose for loop is much simpler:
    for(int i = 0; i < 100; i++)
    {
    newstream.WriteLine(rand.Next());
    }



  • file io causes exception