By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

File reading problem

P: n/a
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.WindowsPrinc ipal);

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[i] = 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?
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Well, I don't see anything particularly wrong, what problem are you having?

--
Adam Clauss
ca*****@tamu.edu

"Sabin Finateanu" <fs****@rdslink.ro> wrote in message news:e9*************@TK2MSFTNGP12.phx.gbl...
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.WindowsPrinc ipal);

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[i] = 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?

Nov 16 '05 #2

P: n/a
Well, first of all, try debugging in, see where it is failing.

There are a couple things that *could* be causing problems:
using(StreamReader FStream = new StreamReader("Konfig.ini")) 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[i] = 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
ca*****@tamu.edu

-----Original Message-----
From: Sabin Finateanu [mailto:fs****@rdslink.ro]
Sent: Friday, July 23, 2004 5:30 PM
To: ca*****@tamu.edu
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" <ca*****@tamu.edu> wrote in message
news:<Ov*************@TK2MSFTNGP12.phx.gbl>...
Well, I don't see anything particularly wrong, what problem are you

having?

--
Adam Clauss
ca*****@tamu.edu

"Sabin Finateanu" <fs****@rdslink.ro> wrote in message

news:e9*************@TK2MSFTNGP12.phx.gbl... 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.WindowsPrinc ipal);

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[i] = 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?




Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.