3 Replies - 1280 Views - Last Post: 20 November 2012 - 06:58 AM Rate Topic: -----

#1 mel_ga  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 18-January 10

Unauthorized exception error

Posted 19 November 2012 - 03:35 PM

Hi I have the following program but when I run it I am getting an unauthorized exception error in the Read method. I don't know why. Here is the code:
public class JuniorStudents : Student
    {
        public int numofStudents;
        public Marks Mathematics;
        public Marks English;
        public Marks Maltese;
        public Marks ReligiousStudies;
        public Marks SocialStudies;

       
        public string filename = ("C:\\Users\\Me\\Desktop\\Students");
        public List<Object> studentList = new List<Object>();

        public int ReadAllRecords()
        {
            studentList.Clear();
            numofStudents = 0;
            try
            {
                System.IO.Stream s1 = System.IO.File.Open(filename, System.IO.FileMode.Open);
                System.IO.BinaryReader f1 = new System.IO.BinaryReader(s1);
                //string s;
              


                numofStudents = f1.ReadInt16();

                for (int i = 0; i < numofStudents; i++)
                {
                    Student stud = new Student();
                    stud.Name = f1.ReadString();
                    stud.Surname = f1.ReadString();
                    stud.DOB = f1.ReadString();
                    stud.Addr = f1.ReadString();
                    stud.Id = f1.ReadString();
                    Mathematics.HYEXAM = f1.ReadInt16();
                    Mathematics.ANEXAM = f1.ReadInt16();
                    English.HYEXAM = f1.ReadInt16();
                    English.ANEXAM = f1.ReadInt16();
                    Maltese.HYEXAM = f1.ReadInt16();
                    Maltese.ANEXAM = f1.ReadInt16();
                    ReligiousStudies.HYEXAM = f1.ReadInt16();
                    ReligiousStudies.ANEXAM = f1.ReadInt16();
                    SocialStudies.HYEXAM = f1.ReadInt16();
                    SocialStudies.ANEXAM = f1.ReadInt16();

                    //for (int j = 0; j < 5; j++) {
                    //    stud.Marks[j] = f1.ReadInt16();
                    //}
                    studentList.Add(stud);
                    studentList.Add(Mathematics);
                    studentList.Add(English);
                    studentList.Add(Maltese);
                    studentList.Add(ReligiousStudies);
                    studentList.Add(SocialStudies);

                }

                f1.Close();


            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.ToString());
            }
                return numofStudents;
            
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Unauthorized exception error

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

Re: Unauthorized exception error

Posted 19 November 2012 - 04:43 PM

public string filename = ("C:\\Users\\Me\\Desktop\\Students");


Is the name of your file "Students" or is that the directory name?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Unauthorized exception error

Posted 19 November 2012 - 05:38 PM

Just because you have your Windows Explorer set to the default setting of
[x] Hide extensions for known file types

Doesn't mean those files don't still have extensions.

Windows might be showing you "Students" when in reality the file is "Students.txt"

Also, is "me" really the name of the user? More importantly are you "me"?

If you are "bob" and you are trying to access files for "mary\\desktop" then you are indeed unauthorized.

You should not be building your paths like that.
There is an enumeration that will point to special folders specifically for this need.

http://msdn.microsof...cialfolder.aspx
Was This Post Helpful? 1
  • +
  • -

#4 mel_ga  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 18-January 10

Re: Unauthorized exception error

Posted 20 November 2012 - 06:58 AM

That is only to hide my name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1