-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsample.cs
135 lines (129 loc) · 5.39 KB
/
sample.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Net.NetworkInformation;
using System.Net;
using System.Xml;
using System.IO;
namespace lStore
{
class userInfo
{
/**
* #todo - this has been hard coded for now
* find a method to find correct default gateway
*/
public static string defaultGateway = "192.168.100.1";
public static string ipaddress; //ip address of the system
public static string baseaddress; //base addres of the system
public static string macAddress; // mac address of the device
public static string username = Environment.UserName; // device username
public static string networkname = Environment.MachineName; //network username
//public static string sysManufacturer ;
/* #todo: need to get code to fetch this */
public static string ram; //total user ram
public static int cores = Environment.ProcessorCount; //total no of logical cores
//public static string processors;
/* #todo - need to get code to fetch this */
public static string resolution; //computer screen resolution in W x H
public static string osInfo; //computer's os detail
public static string rating; //user's rating @from XML
public static string files_shared = "100"; //count of files shared by user @from XML
public static string location; //user's location code @from XML
public static string hash; //user's pvt hash code @from XML
public static string primaryFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +@"\lStore";
/**
* function to get
* all the data which cannot be retrieved from environment variables
* must be called before the values are used
*/
public static void getAllData()
{
ram = new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory.ToString();
osInfo = new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName.ToString();
resolution = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Right.ToString() + " X " + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Bottom.ToString();
try
{
macAddress = GetMacAddress().ToString();
}
catch (Exception ex)
{
macAddress = "NA";
}
baseaddress = getBaseAddress(defaultGateway); //method to get the baseaddress
getIpAddress(); //method to get ip address
rating = getDataFromXML("rating");
location = getDataFromXML("location");
if (location.Length == 0) location = "-NA-";
else location = "[" + location + "]";
hash = getDataFromXML("hash");
files_shared = getDataFromXML("files");
}
/*
* function to get the mac address of the device
*/
public static PhysicalAddress GetMacAddress()
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
// Only consider Ethernet network interfaces
if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
nic.OperationalStatus == OperationalStatus.Up)
{
return nic.GetPhysicalAddress();
}
}
return null;
}
/*
* a function to get base address out of default parameter ip
* @param: string ip: any ip address
*/
public static string getBaseAddress(string ip)
{
string[] parts = ip.Split('.');
return parts[0] + '.' + parts[1] + '.' + parts[2];
}
/*
* function to get ip address of the system
*/
public static void getIpAddress()
{
string hostName = Dns.GetHostName();
IPHostEntry myself = Dns.GetHostByName(hostName);
foreach (IPAddress address in myself.AddressList)
{
if (getBaseAddress(address.ToString()) == baseaddress)
{
ipaddress = address.ToString();
}
}
}
/*
* function to get the data in node @param:node from
* savedfile.xml in lstore folder
*/
public static string getDataFromXML(string node)
{
string output = null;
string xmlString;
try
{
xmlString = File.ReadAllText(primaryFolder + @"\savedfile.xml");
}
catch (Exception ex)
{
return "-NA-";
}
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
reader.ReadToFollowing(node);
output = reader.ReadElementContentAsString();
}
return output;
}
}
}