File reading problem

S

Sabin Finateanu

Hi I'm having problem reading a file from my program and I think it's from a
procedure I'm using but I don't see where I'm going wrong. Here is the code:

public bool AllowUsage()

{

OperatingSystem os = Environment.OSVersion;

AppDomain ad = Thread.GetDomain();

ad.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

WindowsPrincipal wp = (WindowsPrincipal)Thread.CurrentPrincipal;


if(os.Platform == System.PlatformID.Win32NT &&
wp.IsInRole(WindowsBuiltInRole.Administrator) == true)

{

return true;

}

else

{

return false;

}

}



Antd then I call it here:

void MainFormLoad(object sender, System.EventArgs e)

{

if(!this.AllowUsage())

{

MessageBox.Show("You are not an administrator on this system. Program usage
denied.", "ConfigBackup", MessageBoxButtons.OK, MessageBoxIcon.Stop);

this.Close();

}


// Read the Konfig.ini file and store the results

using(StreamReader FStream = new StreamReader("Konfig.ini"))

{

string[] confOptions = new String[9];

string Line;

int i = 0;


while((Line = FStream.ReadLine()) != null)

{

confOptions = Line;

i++;

}

this.textBox1.Text = confOptions[0];

this.textBox2.Text = confOptions[1];

this.textBox3.Text = confOptions[2];

this.textBox4.Text = confOptions[3];

this.textBox5.Text = confOptions[4];

this.textBox6.Text = confOptions[5];

this.textBox7.Text = confOptions[6];

this.textBox8.Text = confOptions[7];

this.textBox9.Text = confOptions[8];

}

this.label7.Text = "phpMyAdmin v" + this.textBox1.Text;

this.label8.Text = "Apache HTTP Server v" + this.textBox2.Text;

this.label9.Text = "MySQL Database Server v" + this.textBox3.Text;

this.label10.Text = "PHP v" + this.textBox4.Text;

}

Can anyone help me?
 
A

Adam Clauss

Well, first of all, try debugging in, see where it is failing.

There are a couple things that *could* be causing problems:If something else has changed the current directory of your program, this line may fail as it will not be looking in the right
folder for Konfig.ini.
string[] confOptions = new String[9];
string Line;
int i = 0;
while((Line = FStream.ReadLine()) != null)
{
confOptions = Line;
i++;
}

This will fail if your ini file is malformed. Since it is just reading as many lines as possible from the file, i has the potential
to go over (or under) 9. So you could either get null's left in your array, or get an index out of bounds exception.

A different way to it would be:
string contents = FStream.ReadToEnd();
string []confOptions = contents.Split("\r\n");
if (confOptions.Length != 9)
MessageBox.Show("Invalid Konfig.ini!");

This will read in the entire file and put each line of the file into confOptions, which I believe is your intent from the while
loop, but this safely does it such that no unhandled exceptions should occur.

Adam Clauss
(e-mail address removed)

-----Original Message-----
From: Sabin Finateanu [mailto:[email protected]]
Sent: Friday, July 23, 2004 5:30 PM
To: (e-mail address removed)
Subject: Re: File reading problem

The data from the file doesn't appear in my form. I have this
problem since
I've introduced the procedure.

Adam Clauss said:
Well, I don't see anything particularly wrong, what problem are you having?
I think it's
from awrong. Here is the
code:system. Program
usage
denied.", "ConfigBackup", MessageBoxButtons.OK, MessageBoxIcon.Stop);

this.Close();

}


// Read the Konfig.ini file and store the results

using(StreamReader FStream = new StreamReader("Konfig.ini"))

{

string[] confOptions = new String[9];

string Line;

int i = 0;


while((Line = FStream.ReadLine()) != null)

{

confOptions = Line;

i++;

}

this.textBox1.Text = confOptions[0];

this.textBox2.Text = confOptions[1];

this.textBox3.Text = confOptions[2];

this.textBox4.Text = confOptions[3];

this.textBox5.Text = confOptions[4];

this.textBox6.Text = confOptions[5];

this.textBox7.Text = confOptions[6];

this.textBox8.Text = confOptions[7];

this.textBox9.Text = confOptions[8];

}

this.label7.Text = "phpMyAdmin v" + this.textBox1.Text;

this.label8.Text = "Apache HTTP Server v" + this.textBox2.Text;

this.label9.Text = "MySQL Database Server v" + this.textBox3.Text;

this.label10.Text = "PHP v" + this.textBox4.Text;

}

Can anyone help me?


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top