Click here to Skip to main content
15,884,933 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(@"i:\tyj.txt");
            {
                textBox1.AppendText(sr.ReadToEnd());
                {
                    if (this.textBox1.Text == " ")
                    {
                        MessageBox.Show("Wrong", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Thnx", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

it refers to this message - "Could not find a part of the path 'i:\tyj.txt'." but why it doesn't show the messagebox ?
Posted
Comments
Orcun Iyigun 26-Jan-12 13:54pm    
Is the "I" drive a flash memory drive or device that you connect from USB hub?
Ryszard50 28-Jan-12 11:40am    
it's flash drive, but I solved it.

Quote:


StreamReader sr = new StreamReader(@"i:\tyj.txt");


This line is causing an error to be thrown. This stops your code before you get to the line where your message box is.

-- add--

It could not find the file. Either the drive I:\ wasn't found, or the file wasn't there on the root of I:\. When you throw an error, think of it as crashing. Your code crashed, and never made it to the message boxes you were intending to display.

------------


When you hit an error in code, it exits there and "throws an exception". This means, the rest of your code does not get executed. You can do try catch, or surround it in logic to make sure you protect against the error.

You can wrap it in a try catch

try
{
  StreamReader sr = new StreamReader("...");
}
catch(FileNotFoundException e)
{
   //handle file not existing here
}


or you can do it much easier with:

if(File.Exists("..."))
{
   //your code here
}
else
{
  //message box maybe? File doesn't exist, or path is incorrect
}
 
Share this answer
 
v2
Comments
Ryszard50 26-Jan-12 14:13pm    
Thanx for help but I still dont get it ...
loctrice 26-Jan-12 14:18pm    
I tried to update my answer a little bit. You code tried to open a file that did not exist. It stops running when this happens, and dies. On it's way out it tries to tell you what happened, which is the message you saw.
Ryszard50 26-Jan-12 14:26pm    
it's a bit better it doesnt wannna read if I do like this:


if (File.Exists(@"i:\tyj.txt"))
{
StreamReader sr = new StreamReader(@"i:\tyj.txt");
sr.ReadLine();
MessageBox.Show("Thnx", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Dupa", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Ryszard50 26-Jan-12 14:31pm    
THANX I've just done :)
Espen Harlinn 26-Jan-12 16:50pm    
5'ed!
There are no situations where the hard-coded path name of the file can be useful. What, you want to develop code which can work only on one computer which has the disk "i", a file on it, etc? It will stop working even on your computer after the slightest change in hardware, maybe even if you insert another removable drive or something like that.

—SA
 
Share this answer
 
Comments
loctrice 26-Jan-12 14:57pm    
This is true. Hard coding drive paths is usually not good. When I first started learning this stuff in college, that was how we were taught to do it. After we had the basics, then the hard coding went away.
Sergey Alexandrovich Kryukov 26-Jan-12 15:20pm    
Thank you for this comment (and probably the vote).
I only insist that is not "usually" not good, it is never useful, except, perhaps, temporary code in the very beginning of the development, just for an initial test, to be removed later. As you say, "after we had the basics" -- well said.
--SA
Espen Harlinn 26-Jan-12 16:51pm    
Good point :)
Sergey Alexandrovich Kryukov 26-Jan-12 17:03pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900