-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
readresourceex1.cs
93 lines (90 loc) · 3.24 KB
/
readresourceex1.cs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// <Snippet6>
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Runtime.Versioning;
public class Example6
{
[SupportedOSPlatform("windows")]
public static void Run()
{
ResourceReader rdr = new ResourceReader(@".\ContactResources.resources");
IDictionaryEnumerator dict = rdr.GetEnumerator();
while (dict.MoveNext())
{
Console.WriteLine("Resource Name: {0}", dict.Key);
try
{
Console.WriteLine(" Value: {0}", dict.Value);
}
catch (FileNotFoundException)
{
Console.WriteLine(" Exception: A file cannot be found.");
DisplayResourceInfo(rdr, (string)dict.Key, false);
}
catch (FormatException)
{
Console.WriteLine(" Exception: Corrupted data.");
DisplayResourceInfo(rdr, (string)dict.Key, true);
}
catch (TypeLoadException)
{
Console.WriteLine(" Exception: Cannot load the data type.");
DisplayResourceInfo(rdr, (string)dict.Key, false);
}
}
}
[SupportedOSPlatform("windows")]
private static void DisplayResourceInfo(ResourceReader rr,
string key, bool loaded)
{
string dataType = null;
byte[] data = null;
rr.GetResourceData(key, out dataType, out data);
// Display the data type.
Console.WriteLine(" Data Type: {0}", dataType);
// Display the bytes that form the available data.
Console.Write(" Data: ");
int lines = 0;
foreach (var dataItem in data)
{
lines++;
Console.Write("{0:X2} ", dataItem);
if (lines % 25 == 0)
Console.Write("\n ");
}
Console.WriteLine();
// Try to recreate current state of data.
// Do: Bitmap, DateTimeTZI
switch (dataType)
{
// Handle internally serialized string data (ResourceTypeCode members).
case "ResourceTypeCode.String":
BinaryReader reader = new BinaryReader(new MemoryStream(data));
string binData = reader.ReadString();
Console.WriteLine(" Recreated Value: {0}", binData);
break;
case "ResourceTypeCode.Int32":
Console.WriteLine(" Recreated Value: {0}",
BitConverter.ToInt32(data, 0));
break;
case "ResourceTypeCode.Boolean":
Console.WriteLine(" Recreated Value: {0}",
BitConverter.ToBoolean(data, 0));
break;
// .jpeg image stored as a stream.
case "ResourceTypeCode.Stream":
const int OFFSET = 4;
int size = BitConverter.ToInt32(data, 0);
Bitmap value1 = new Bitmap(new MemoryStream(data, OFFSET, size));
Console.WriteLine(" Recreated Value: {0}", value1);
break;
default:
break;
}
Console.WriteLine();
}
}
// </Snippet6>