|
Luc Pattyn wrote: Which book one prefers is subjective.
...just avoid anything with "...In 7 Days!" in the title!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Hi Luc,
I'm watching/doing the video series on www.learnvisualstudio.net
Have you watched these? I learn better seeing while reading ... but maybe the book is worth it also? Let me know what you think, thanks.
|
|
|
|
|
I think nothing beats a book, for a couple of reasons:
1. The quality tends to be much higher than anything on the web.
2. You can process material on your own pace, anything that moves may end up being watched as a TV show.
3. You can make little notes and apply highlights in a book (assuming you own it).
I do a quick video if I need to get some insight in new stuff, not to study it thoroughly.
|
|
|
|
|
Books are very good. Considering everything I know about C# was gained through library books and books bought at used book stores (no formal training) they are a priceless source of information.
As a side note, args[0] sounds like the argument passed to all Console applications in the Main method. So args[0] would be the first command line argument for the application e.g. ping.exe 255.255.255.255 . This being a hypothetical situation from the fact that replacing it with a variable called host (assuming it is the IP Adress to ping) made it work.
Anyway. Books are good.
|
|
|
|
|
|
That is not an introduction or a tutorial, that is reference material. Which comes in handy once you start programming in some language, but first you need the intro, the fundamentals.
|
|
|
|
|
|
So your first port of call is to find out just what args[0] is. Google can help: Google args[0] c#[^]
This second result explains it, and gives examples: MSDN on command line arguments[^]
This is your second (or first-and-a-half) port of call: MSDN. It knows everything (hah!) about C# and .NET
And it's free.
Impenetrable at times, but free. And very often worth tagging into a Google search: "args[0] c# MSDN" would give the article above as the first hit.
So, args[0] is the first command line argument to your console app. (or Winforms or whatever, but don't worry about that yet). This explains why it gave an ArgumentOutOfRange error when you tried to use it: you haven't supplied any command line arguments!
So, change your code to:
string host = "192.168.0.1";
if (args.Count > 0)
{
host = args[0];
} And it would work with either a default value - my router - or the IP address you specify as a command line argument.
This means you can run your console app with:
myPing
(which would ping your router)
or
myPing 92.27.41.80
(which would ping me, for an hour or so until I turn the router off.)
Now do you see what I mean about it being important to work out why something isn't working, rather than just "fixing it"?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Thank you.
Even though I have some reading to do now I will try and implement this and get it working.
|
|
|
|
|
Here is what I have, did I do something wrong because it is still telling me I am outside of the bounds of the array
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
public static void Main(string [] args)
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
string host = "servername";
if (args.Length > 0)
{
host = args[0];
}
options.DontFragment = true;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(args[0], timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}", reply.Address.ToString());
Console.WriteLine("Bytes {0}", reply.Buffer.Length);
Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
Console.ReadLine();
}
}
}
}
|
|
|
|
|
Yes! You are still using args[0]
PingReply reply = pingSender.Send(args[0], timeout, buffer, options);
Try:
PingReply reply = pingSender.Send(host, timeout, buffer, options);
Also change the comment near the top:
It may be accurate, but it isn't very helpfull! How about:
That describes better what you are trying to do, rather than how you are achieving them.
However, you get a 5 just for putting comments in your first program.
Not a bad effort - keep it up!
Later, you may want to put in a loop, so you can use a number of addresses in the command line. See foreach for details.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Thanks and be proud, I touched it up to this, this morning and it actually works!!!
Next on the list is to import a text file through streamreader? or textreader?
Hopefully I can find some more code like this basic ping tool on MSDN or something and then I'll be able to import each server with my foreach loop?
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.IO;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
public static void Main(string [] args)
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
string[] arr = new string[] { "server1", "server2", "server3" };
foreach (string i in arr)
{
int pingloop = 0;
Console.WriteLine(i);
do
{
PingReply reply = pingSender.Send(i, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}", reply.Address.ToString() + ": bytes=" + reply.Buffer.Length + " time=" + reply.RoundtripTime + "ms" + " TTL=" + reply.Options.Ttl);
pingloop = pingloop +1;
}
}
while (pingloop < 4);
Console.WriteLine();
}
Console.ReadLine();
}
}
|
|
|
|
|
Well done!
I think you are getting there.
A couple of style things, that make it easier to read when you go back to it:
string[] arr = new string[] { "server1", "server2", "server3" };
foreach (string i in arr)
Don't call it "i" - there are a couple of reasons (one of which is that old programmers assume "i" is always an int). Always use descriptive names for variables, it makes it a lot easier to work out what you are trying to do. Since Visual Studio list possible names for you, it isn't a lot of extra typing, and it does make it more readable. Try using "server" instead:
string[] servers = new string[] { "server1", "server2", "server3" };
foreach (string server in servers)
{
...
}
And try using a for loop instead of a do...while - it makes it more obvious by keeping the whole thing together:
int pingloop = 0;
do
{
PingReply reply = pingSender.Send(i, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}", reply.Address.ToString() + ": bytes=" + reply.Buffer.Length + " time=" + reply.RoundtripTime + "ms" + " TTL=" + reply.Options.Ttl);
pingloop = pingloop +1;
}
}
while (pingloop < 4);
becomes
for (int retries = 0; retries < 4; retries++)
{
PingReply reply = pingSender.Send(i, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}", reply.Address.ToString() + ": bytes=" + reply.Buffer.Length + " time=" + reply.RoundtripTime + "ms" + " TTL=" + reply.Options.Ttl);
break;
}
}
It's all about keeping thing together, and making it obvious what is going on. Doing it your way means that "pingloop" is available after the loop, implying that it will be relevant and probably used after the loop. With the for loop, "retries" is not available outside the loop at all.
I also added the "break" to prevent repeated pinging once it has succeeded.
Good effort though, well done!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Here is my latest code, thanks to all that helped
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
public static void Main(string[] args)
{
const string f = @"c:\temp\servernames.txt";
List<string> servers = new List<string>();
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
string line = null;
PingReply reply = null;
options.DontFragment = true;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 250;
using (StreamReader r = new StreamReader(f))
{
while ((line = r.ReadLine()) != null)
{
if (string.IsNullOrEmpty(line))
{
}
else
{
servers.Add(line);
}
}
}
foreach (string i in servers)
{
Console.WriteLine(i);
for (int retries = 0; retries < 4; retries++)
{
try
{
reply = pingSender.Send(i, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}", reply.Address.ToString() + ": bytes=" + reply.Buffer.Length + " time=" + reply.RoundtripTime + "ms" + " TTL=" + reply.Options.Ttl);
}
}
catch (PingException e)
{
Console.WriteLine("Ping failed: " + e.Message);
}
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
|
|
|
|
|
More internet reading for you.
http://dotnetperls.com/main[^]
OriginalGriff is right on the money, understand what you are doing, and Luc Pattyn said the correct way to do just that, read. When you understand the basic underlying constructs and how data is used and passed around within the computer, it gets easier. (That's what they told me 27 years ago, but I'm still waiting for it to get easier... )
It was broke, so I fixed it.
|
|
|
|
|
A ping app is probably not the best choice for your first C# application. You may want to get yourself familiar with the language syntax and semantics first. Although, if you have prior C++ or Java experience, this approach should still work fine for you.
|
|
|
|
|
Hope this example can help you:
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
if (p.Start())
{
p.StandardInput.WriteLine("net use " + strFileName + " " + strPwd + " /user:" + strDomain + "\\" + strId);
p.StandardInput.WriteLine("exit");
p.WaitForExit();
string ReturnText = p.StandardOutput.ReadToEnd();
if (ReturnText.IndexOf("成功") >= 0)
Dts.TaskResult = (int)ScriptResults.Success;
else
throw new Exception("fail");
}
else
throw new Exception("fail");
Reference: http://www.programlive.tk
|
|
|
|
|
Hi,
I am using iTextSharp to read & write the data to pdf-forms.It works good for reading from pdf-forms.
But when i write data to pdf-forms(programmatically) it fills data to pdf-forms successfully but when we try to
add data(manually) to pdf-forms, it don’t saves data to pdf-forms & gives error as
THIS DOCUMENT ENABLED EXTENDED FEATURES IN ADOBE READER.THE DOCUMNETHAS BEEN CHANGED
SINCE IT WAS CREATED AND USE OF EXTENDED FEATURES IS NO LONGER AVAILABLE.PLEASE CONTACT THE
ORIGINAL VERSION OF THIS DOCUMENT.
|
|
|
|
|
Who marked this as a Good Question?
Also, if you are using Adobe Reader, bypassing the reader extensions requirement is a violation of the Adobe License which is surprisingly very strict.
|
|
|
|
|
Hi All!
I need to parse a txt file. It would be no problem but txt file updated dynamically, i.e. lines are added in some intervals and i need to detect and parse a new line. How can i do it?
|
|
|
|
|
Message Closed
modified 23-Nov-14 5:53am.
|
|
|
|
|
Thanks a lot!Looks very interesting!
|
|
|
|
|
You might want to check out this article series for info on FileSystemWatcher:
FileSystemWatcher - Pure Chaos (Part 1 of 2)[^]
Part 1 of the article discusses the actual code, and Part 2 shows some interesting results using the sample application that is provided in Part 1.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Maintain a record of where you left off and pick up from there.
That's what I do.
|
|
|
|
|
Has anyone used this "Range.BorderAround2" when making the border line around the cell range? I believe that it caused crash with this error message: "Exception from HRESULT: 0x80020003" which is "DISP_E_MEMBERNOTFOUND". I'm not quite sure if this is due to the other pc that has office 2003? I've included the v14 dll file in the executable directory.
I used the 3 lines below and it caused the error exception mentioned above.
I'm using the Microsoft.Office.Interop.Excel v14 (for MS Office 2010).
cellRange = oSheet.get_Range(oSheet.Cells[3, 10], oSheet.Cells[15, 11]);
cellRange.BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, 1, 1);
I had to use the lines as shown below and it works fine...
cellRange = oSheet.get_Range(oSheet.Cells[3, 10], oSheet.Cells[15, 11]);
cellRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
cellRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
|
|
|
|
|