• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Cannot read text file

When I execute my read text file program, the
following message appeared:
An execption system security execption has
occured in project.
Can anyone tell me why?

len00
0
len00
Asked:
len00
1 Solution
 
BullsCommented:
Hi

Couold you post more info, maybe the code, but my guess is that you don't have sufficient permission.

Cheers
0
 
SunBowCommented:
If you get permission, then you can move on to next error. In meantime system has labeled you as violator. Best to only access your own files, IMO.
0
 
God_AresCommented:
start over and see if you have the same problem:

THIS IS A CONSOLE APPLICATION. just make a MyFile.txt in the same path as your app and fill it with text.

using System;
using System.IO;
public class TextFromFile
{
    private const string FILE_NAME = "MyFile.txt";
    public static void Main(String[] args)
    {
        if (!File.Exists(FILE_NAME))
        {
            Console.WriteLine("{0} does not exist.", FILE_NAME);
            return;
        }
        StreamReader sr = File.OpenText(FILE_NAME);
        String input;
        while ((input=sr.ReadLine())!=null)
        {
            Console.WriteLine(input);
        }
        Console.WriteLine ("The end of the stream has been reached.");
        sr.Close();
    }
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
len00Author Commented:
Thank you for your respone, but the problem still occured.
Is it because the network drive that I'm using? I'm not allowed to used the local drive, so I'm using the network drive that is allocated to me to do all my project. If the network drive is the problem, how can I overcome the problem so that I can execute my read text file program succesfully.

For the reference, here is my program:

using System;
using System.Collections;
using System.IO;

public class Read
{
    public static void Main()
    {    
     string fileContent;
     FileStream myStream = new FileStream
        ("P:\\TestInput.txt",FileMode.Open);
     StreamReader myStreamReader = new StreamReader
        (myStream);
     fileContent = myStreamReader.ReadToEnd();
     Console.WriteLine("{0}",fileContent);
     Console.ReadLine();
    }
}

len00
0
 
BullsCommented:
Hi

I tried your code and it works, but what happens when you add the 3rd parameter to new FileStream.

Ex:

FileStream fs = new FileStream("P:\\TestInput.txt", FileMode.Open, FileAccess.ReadWrite);

O yea, the file does exist, right?

Cheers
0
 
AvonWyssCommented:
Its probably the network share. Note that applications which are not run from a local disk do have less proviledges than ones run from a lokal drive. You may try to use the floppy disk drive (copy your app there and try it) to see if the problem gets solved by this.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now