Jody Nichols

I have this chunk of code in the end of my program. The program reads a list of hostnames from a text file to a array of strings(proven successful) and then is supposed to write out the unavailable hostnames to another file (results.txt). For some reason it will create this file, but not write to it. Does it have something to do with it being in a catch

Thank you.


foreach (string hostname in Hostnames)
{
try
{
reply = pinger.Send(hostname);
}
catch (PingException e)
{
Console.WriteLine(hostname);
tw.WriteLine(hostname);
}
}



Re: Visual C# General New to C# - Quick Question about StreamWriter

mwalts

Hello,

No, it being in a catch isn't the problem, the problem is that an unreachable server does not result in a PingException, but rather a PingResult.Status that is not equal to IPStatus.Success
so, after your ping, more like

if(reply.Status != IPStatus.Success)
{
tw.WriteLine(hostname);
}

hope that helps

-mwalts





Re: Visual C# General New to C# - Quick Question about StreamWriter

Jody Nichols

mwalts wrote:
Hello,

No, it being in a catch isn't the problem, the problem is that an unreachable server does not result in a PingException, but rather a PingResult.Status that is not equal to IPStatus.Success
so, after your ping, more like

if(reply.Status != IPStatus.Success)
{
tw.WriteLine(hostname);
}

hope that helps

-mwalts



That was actually the first thing I tried and for some reason it did generate the pingexception. We are on the same page.




Re: Visual C# General New to C# - Quick Question about StreamWriter

mwalts

Oh, and a great way to handle that type of problem on your own is by using MSDN as a resource.

http://msdn2.microsoft.com/en-us/library/7hzczzed.aspx

shows exactly what you should do, and explains the return type in the Return Value section, and further in the Remarks section.

Admittedly, that only helps if you suspect that the Ping class might be your problem, but having methods return flags based on failure is a fairly common practice in all programming languages.

Don't mean to sound like a jerk, I know I used to get annoyed when people would just tell me to RTFM (Read The Fairlygood(*cough*) Manual), but it is an excellent resource.

Good luck,

-mwalts




Re: Visual C# General New to C# - Quick Question about StreamWriter

Jody Nichols

mwalts wrote:
Oh, and a great way to handle that type of problem on your own is by using MSDN as a resource.

http://msdn2.microsoft.com/en-us/library/7hzczzed.aspx

shows exactly what you should do, and explains the return type in the Return Value section, and further in the Remarks section.

Admittedly, that only helps if you suspect that the Ping class might be your problem, but having methods return flags based on failure is a fairly common practice in all programming languages.

Don't mean to sound like a jerk, I know I used to get annoyed when people would just tell me to RTFM (Read The Fairlygood(*cough*) Manual), but it is an excellent resource.

Good luck,

-mwalts


No worries. That section of MSDN is where I found how to do the initial ping. However, I don't see how the *ping* itself could kill my stream. The actual dump of the exception shows that it in a pingexception and that is why I caught it. In theory this should work because the only time I want to write to the results file is when the hostname will not resolve and ping.

Thanks for your continued help.


EDIT: It might also be helpful to know I can put a
Console.WriteLine(hostname);
directly adjacent to the StreamWrite and it will print my desired results to the screen.




Re: Visual C# General New to C# - Quick Question about StreamWriter

mwalts

Alright, but that exception is not coming from an unreachable server.

Could you look through the debugger to tell what the internal exception is PingException just means an unhandled excpetion was thrown by one of the methods used by Ping.Send, so that really doesn't tell you much. But based on the documentation, it does tell us that something other then the host being unreachable is happening.

As for why your StreamWriter isn't working there, can we see where you instantiated it is it still open What method did you use to open it

Thanks,

-mwalts