Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
using System;
using System.Reflection;

public class Example
{
public static void Main()
{
// <Snippet1>
// Windows DLL (non-.NET assembly)
string filePath = Environment.ExpandEnvironmentVariables("%windir%");
if (!filePath.Trim().EndsWith(@"\"))
filePath += @"\";
filePath += @"System32\Kernel32.dll";
public static void Main()
{
// <Snippet1>
// Windows DLL (non-.NET assembly)
string filePath = Environment.ExpandEnvironmentVariables("%windir%");
if (!filePath.Trim().EndsWith(@"\"))
filePath += @"\";
filePath += @"System32\Kernel32.dll";

try {
Assembly assem = Assembly.LoadFile(filePath);
}
catch (BadImageFormatException e) {
Console.WriteLine("Unable to load {0}.", filePath);
Console.WriteLine(e.Message.Substring(0,
e.Message.IndexOf(".") + 1));
}
// The example displays an error message like the following:
// Unable to load C:\WINDOWS\System32\Kernel32.dll.
// The module was expected to contain an assembly manifest.
// </Snippet1>
}
try
{
Assembly assem = Assembly.LoadFile(filePath);
}
catch (BadImageFormatException e)
{
Console.WriteLine($"Unable to load {filePath}.");
Console.WriteLine(e.Message.Substring(0,
e.Message.IndexOf(".") + 1));
}
// The example displays an error message like the following:
// Unable to load C:\WINDOWS\System32\Kernel32.dll.
// The module was expected to contain an assembly manifest.
// </Snippet1>
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
// <Snippet2>
// <Snippet2>
using System;

public class StringLib
{
private string[] exceptionList = { "a", "an", "the", "in", "on", "of" };
private char[] separators = { ' ' };
private string[] exceptionList = { "a", "an", "the", "in", "on", "of" };
private char[] separators = { ' ' };

public string ToProperCase(string title)
{
bool isException = false;
public string ToProperCase(string title)
{
bool isException = false;

string[] words = title.Split( separators, StringSplitOptions.RemoveEmptyEntries);
string[] newWords = new string[words.Length];

for (int ctr = 0; ctr <= words.Length - 1; ctr++)
{
isException = false;
string[] words = title.Split(separators, StringSplitOptions.RemoveEmptyEntries);
string[] newWords = new string[words.Length];

foreach (string exception in exceptionList)
{
if (words[ctr].Equals(exception) && ctr > 0)
for (int ctr = 0; ctr <= words.Length - 1; ctr++)
{
isException = false;

foreach (string exception in exceptionList)
{
isException = true;
break;
if (words[ctr].Equals(exception) && ctr > 0)
{
isException = true;
break;
}
}
}

if (!isException)
newWords[ctr] = words[ctr].Substring(0, 1).ToUpper() + words[ctr].Substring(1);
else
newWords[ctr] = words[ctr];
}
return string.Join(" ", newWords);
}
if (!isException)
newWords[ctr] = words[ctr].Substring(0, 1).ToUpper() + words[ctr].Substring(1);
else
newWords[ctr] = words[ctr];
}
return string.Join(" ", newWords);
}
}
// Attempting to load the StringLib.dll assembly produces the following output:
// Unhandled Exception: System.BadImageFormatException:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// <Snippet4>
using System;
using System.IO;
Expand All @@ -5,39 +5,45 @@

public class Example
{
public static void Main()
{
String[] args = Environment.GetCommandLineArgs();
if (args.Length == 1) {
Console.WriteLine("\nSyntax: PlatformInfo <filename>\n");
return;
}
Console.WriteLine();
public static void Main()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length == 1)
{
Console.WriteLine("\nSyntax: PlatformInfo <filename>\n");
return;
}
Console.WriteLine();

// Loop through files and display information about their platform.
for (int ctr = 1; ctr < args.Length; ctr++) {
string fn = args[ctr];
if (!File.Exists(fn)) {
Console.WriteLine("File: {0}", fn);
Console.WriteLine("The file does not exist.\n");
}
else {
try {
AssemblyName an = AssemblyName.GetAssemblyName(fn);
Console.WriteLine("Assembly: {0}", an.Name);
if (an.ProcessorArchitecture == ProcessorArchitecture.MSIL)
Console.WriteLine("Architecture: AnyCPU");
else
Console.WriteLine("Architecture: {0}", an.ProcessorArchitecture);

Console.WriteLine();
// Loop through files and display information about their platform.
for (int ctr = 1; ctr < args.Length; ctr++)
{
string fn = args[ctr];
if (!File.Exists(fn))
{
Console.WriteLine($"File: {fn}");
Console.WriteLine("The file does not exist.\n");
}
catch (BadImageFormatException) {
Console.WriteLine("File: {0}", fn);
Console.WriteLine("Not a valid assembly.\n");
else
{
try
{
AssemblyName an = AssemblyName.GetAssemblyName(fn);
Console.WriteLine($"Assembly: {an.Name}");
if (an.ProcessorArchitecture == ProcessorArchitecture.MSIL)
Console.WriteLine("Architecture: AnyCPU");
else
Console.WriteLine($"Architecture: {an.ProcessorArchitecture}");

Console.WriteLine();
}
catch (BadImageFormatException)
{
Console.WriteLine($"File: {fn}");
Console.WriteLine("Not a valid assembly.\n");
}
}
}
}
}
}
}
}
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
// <Snippet2>
using System;

public class Example
{
public static void Main()
{
// Define an array of 20 elements and display it.
int[] arr = new int[20];
int value = 1;
for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++) {
arr[ctr] = value;
value = value * 2 + 1;
}
DisplayArray(arr);
public static void Main()
{
// Define an array of 20 elements and display it.
int[] arr = new int[20];
int value = 1;
for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++)
{
arr[ctr] = value;
value = value * 2 + 1;
}
DisplayArray(arr);

// Convert the array of integers to a byte array.
byte[] bytes = new byte[arr.Length * 4];
for (int ctr = 0; ctr < arr.Length; ctr++) {
Array.Copy(BitConverter.GetBytes(arr[ctr]), 0,
bytes, ctr * 4, 4);
}
// Convert the array of integers to a byte array.
byte[] bytes = new byte[arr.Length * 4];
for (int ctr = 0; ctr < arr.Length; ctr++)
{
Array.Copy(BitConverter.GetBytes(arr[ctr]), 0,
bytes, ctr * 4, 4);
}

// Encode the byte array using Base64 encoding
String base64 = Convert.ToBase64String(bytes);
Console.WriteLine("The encoded string: ");
for (int ctr = 0; ctr <= base64.Length / 50; ctr++)
Console.WriteLine(base64.Substring(ctr * 50,
ctr * 50 + 50 <= base64.Length
? 50 : base64.Length - ctr * 50));
Console.WriteLine();
// Encode the byte array using Base64 encoding
string base64 = Convert.ToBase64String(bytes);
Console.WriteLine("The encoded string: ");
for (int ctr = 0; ctr <= base64.Length / 50; ctr++)
Console.WriteLine(base64.Substring(ctr * 50,
ctr * 50 + 50 <= base64.Length
? 50 : base64.Length - ctr * 50));
Console.WriteLine();

// Convert the string back to a byte array.
byte[] newBytes = Convert.FromBase64String(base64);
// Convert the string back to a byte array.
byte[] newBytes = Convert.FromBase64String(base64);

// Convert the byte array back to an integer array.
int[] newArr = new int[newBytes.Length/4];
for (int ctr = 0; ctr < newBytes.Length / 4; ctr ++)
newArr[ctr] = BitConverter.ToInt32(newBytes, ctr * 4);
// Convert the byte array back to an integer array.
int[] newArr = new int[newBytes.Length / 4];
for (int ctr = 0; ctr < newBytes.Length / 4; ctr++)
newArr[ctr] = BitConverter.ToInt32(newBytes, ctr * 4);

DisplayArray(newArr);
}
DisplayArray(newArr);
}

private static void DisplayArray(Array arr)
{
Console.WriteLine("The array:");
Console.Write("{ ");
for (int ctr = 0; ctr < arr.GetUpperBound(0); ctr++) {
Console.Write("{0}, ", arr.GetValue(ctr));
if ((ctr + 1) % 10 == 0)
Console.Write("\n ");
}
Console.WriteLine("{0} {1}", arr.GetValue(arr.GetUpperBound(0)), "}");
Console.WriteLine();
}
private static void DisplayArray(Array arr)
{
Console.WriteLine("The array:");
Console.Write("{ ");
for (int ctr = 0; ctr < arr.GetUpperBound(0); ctr++)
{
Console.Write($"{arr.GetValue(ctr)}, ");
if ((ctr + 1) % 10 == 0)
Console.Write("\n ");
}
Console.WriteLine($"{arr.GetValue(arr.GetUpperBound(0))} }}");
Console.WriteLine();
}
}
// The example displays the following output:
// The array:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// <Snippet1>
using System;

public class Example
{
public static void Main()
{
// Define a byte array.
byte[] bytes = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
Console.WriteLine("The byte array: ");
Console.WriteLine(" {0}\n", BitConverter.ToString(bytes));
public static void Main()
{
// Define a byte array.
byte[] bytes = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
Console.WriteLine("The byte array: ");
Console.WriteLine($" {BitConverter.ToString(bytes)}\n");

// Convert the array to a base 64 string.
string s = Convert.ToBase64String(bytes);
Console.WriteLine("The base 64 string:\n {0}\n", s);
// Convert the array to a base 64 string.
string s = Convert.ToBase64String(bytes);
Console.WriteLine($"The base 64 string:\n {s}\n");

// Restore the byte array.
byte[] newBytes = Convert.FromBase64String(s);
Console.WriteLine("The restored byte array: ");
Console.WriteLine(" {0}\n", BitConverter.ToString(newBytes));
}
// Restore the byte array.
byte[] newBytes = Convert.FromBase64String(s);
Console.WriteLine("The restored byte array: ");
Console.WriteLine($" {BitConverter.ToString(newBytes)}\n");
}
}
// The example displays the following output:
// The byte array:
Expand Down
Loading
Loading