-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathListing05.15.PassingCommandLineArgumentsToMain.cs
More file actions
42 lines (41 loc) · 1.22 KB
/
Listing05.15.PassingCommandLineArgumentsToMain.cs
File metadata and controls
42 lines (41 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_15;
#region INCLUDE
public class Program
{
#region HIGHLIGHT
public static int Main(string[] args)
#endregion HIGHLIGHT
{
#region HIGHLIGHT
int result;
if(args?.Length != 2 )
#endregion HIGHLIGHT
{
// Exactly two arguments must be specified; give an error
Console.WriteLine(
"ERROR: You must specify the "
+ "URL and the file name");
Console.WriteLine(
"Usage: Downloader.exe <URL> <TargetFileName>");
result = 1;
}
else
{
#region HIGHLIGHT
string urlString = args[0];
string fileName = args[1];
#endregion HIGHLIGHT
HttpClient client = new();
byte[] response =
client.GetByteArrayAsync(urlString).Result;
client.Dispose();
File.WriteAllBytes(fileName, response);
Console.WriteLine($"Downloaded '{ fileName }' from '{ urlString }'.");
result = 0;
}
#region HIGHLIGHT
return result;
#endregion HIGHLIGHT
}
}
#endregion INCLUDE