0% found this document useful (0 votes)
36 views20 pages

C Shap Samples

Uploaded by

Eduh njoroge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views20 pages

C Shap Samples

Uploaded by

Eduh njoroge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Mdi parent Form

This is the project main menu.


[Link] output management
In the mdi parent form this is among the menu items and contains:
1. Files option- it implements file creation, file read and write, listing of drives and files
and viewing file information

Program Code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class IO_Manager : Form

{
string Foldername = @"F:\Csharp";
string winDir = [Link]("windir");
public IO_Manager()
{
InitializeComponent();
}
private void addListItem(string value)
{
[Link](value);
}

private void bunifuFlatButton2_Click(object sender, EventArgs e)


{//creates files and folder on csharp
string path = [Link];
if (![Link](path))
{
[Link](@"F:\Csharp");
[Link]("file created successfully...!");
}
else
{
[Link]("file already exists");
}
}

private void bunifuFlatButton1_Click(object sender, EventArgs e)


{
string path = [Link];
if (![Link](path))
{
[Link](@"F:\Csharp");
[Link]("SUCCESSFULLY CREATED...!");

}
else
{
[Link]("Directory already exists");
}

private void bunifuCustomTextbox1_TextChanged(object sender, EventArgs e)


{

private void bunifuCustomLabel1_Click(object sender, EventArgs e)


{

private void File_Manager_Load(object sender, EventArgs e)


{

private void bunifuFlatButton4_Click(object sender, EventArgs e)


{//displays drive and files
[Link]();
StreamReader reader = new StreamReader(winDir + "\\[Link]");
try
{
do
{
addListItem([Link]());
}
while ([Link]() != -1);
}
catch
{
addListItem("File is empty");
}
finally
{
[Link]();
}
}

private void bunifuFlatButton5_Click(object sender, EventArgs e)


{
StreamWriter writer = new StreamWriter("E:\\directory");
[Link]("File created using StreamWriter class.");
[Link]();
[Link]();
addListItem("File Written to C:\\[Link]");

private void bunifuFlatButton6_Click(object sender, EventArgs e)


{
[Link]();
FileInfo FileProps = new FileInfo(winDir + "\\[Link]");
addListItem("File Name = " + [Link]);
addListItem("Creation Time = " + [Link]);
addListItem("Last Access Time = " + [Link]);
addListItem("Last Write TIme = " + [Link]);
addListItem("Size = " + [Link]);
FileProps = null;

private void bunifuFlatButton7_Click(object sender, EventArgs e)


{
//Demonstrates how to obtain a list of disk drives.
[Link]();
string[] drives = [Link]();
foreach (string drive in drives)
{
addListItem(drive);
}

private void bunifuFlatButton8_Click(object sender, EventArgs e)


{
int count = 0;
//How to get a list of folders (example uses Windows folder).
[Link]();
//string[] dirs = [Link](winDir);
string[] dirs = [Link]("E:\\");
foreach (string dir in dirs)
{
count++;
addListItem(count + " " + dir);

private void bunifuFlatButton3_Click(object sender, EventArgs e)


{
//How to obtain list of files (example uses Windows folder).
[Link]();
// string[] files = [Link](winDir);
string[] files = [Link]("E:\\");
foreach (string i in files)
{
addListItem(i);
}

private void bunifuThinButton21_Click(object sender, EventArgs e)


{
[Link]();
}
}
}
[Link]-in this we implement a program that enables opening of usb ports and disabling the
provided you are an admin of the computer.

not
Program Code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using Microsoft.Win32;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class devices : Form
{

public devices()
{
InitializeComponent();
}
RegistryKey Regkey, RegKey2;
Int32 rValue, rsvalue, Gvalue, tvalue;
string Regpath = "System\\CurrentControlSet\\Services\\USBSTOR";
string ReadAndWriteRegPath2 = "System\\CurrentControlSet\\Control";
string ReadAndWriteRegPath = "System\\CurrentControlSet\\Control\\
StorageDevicePolicies";

private void devices_Load(object sender, EventArgs e)


{
isAdmin = IsUserAnAdmin();
if (isAdmin == false)
{
[Link]("You don't have proper privileges level to make changes,
administrators privileges are required", "Error", [Link],
[Link]);
//Close();
}
else
{
Regkey = [Link](Regpath, true);
Gvalue = Convert.ToInt32([Link]("Start"));
//check the current state of the usb/whether is enabled or disabled
if (Gvalue == 3)
{
[Link] = true;
}
else if (Gvalue == 4)
{
[Link] = true;
}
RegKey2 = [Link](ReadAndWriteRegPath, true);
try
{
tvalue = Convert.ToInt32([Link]("WriteProtect"));
if (tvalue == 1)
{
[Link] = true;
}
else if (tvalue == 0)
{
[Link] = true;
}
}
catch (NullReferenceException) { }
}
}

private void bunifuFlatButton1_Click(object sender, EventArgs e)


{ }

private void bunifuFlatButton2_Click(object sender, EventArgs e)


{

private void bunifuFlatButton3_Click(object sender, EventArgs e)


{ }

private void bunifuThinButton25_Click(object sender, EventArgs e)


{
[Link]();
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)


{
[Link] = true;
rValue = 3;
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)


{
[Link] = true;
rValue = 4;
}

private void radioButton3_CheckedChanged(object sender, EventArgs e)


{
rsvalue = 1;
}

private void bunifuThinButton21_Click(object sender, EventArgs e)


{
try
{
Regkey = [Link](Regpath, true);
[Link]("Start", rValue);
if ([Link] == true)
{
RegKey2 = [Link](ReadAndWriteRegPath2, true);
[Link]("StorageDevicePolicies");
RegKey2 = [Link](ReadAndWriteRegPath, true);
[Link]("WriteProtect", rsvalue);
}
}
catch (Exception ex)
{ }
if ((rValue == 3) && (rsvalue == 1))
{
[Link]("USB Port were enable and Read only is enabled");
}
else if ((rValue == 3) && (rsvalue == 0))
{
[Link]("USB Port were enable and Read and write is enabled");
}
else
{
[Link]("USB Port were disable");
}

private void radioButton4_CheckedChanged(object sender, EventArgs e)


{
rsvalue = 0;
}

private void bunifuThinButton22_Click(object sender, EventArgs e)


{
Close();
}
bool isAdmin;
[DllImport("shell32")]
static extern bool IsUserAnAdmin();

}
}

[Link] options—The program restarts, shutdowns, sign user out of the computer and puts it
to sleep mode.

Program code.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class power : Form
{
[DllImport("user32")]
public static extern void LockWorkStation();

public power()
{
InitializeComponent();
}
[DllImport("user32")]
public static extern bool ExitWindowsEx(GraphicsUnit uFlags, uint DwReason);

private void bunifuThinButton21_Click(object sender, EventArgs e)


{
[Link]();
}

private void bunifuThinButton22_Click(object sender, EventArgs e)


{//restarts computer
[Link]("[Link]", "-r -t 00");
}

private void bunifuThinButton24_Click(object sender, EventArgs e)

{//shurts computer
[Link]("[Link]", "-s -t 00");
}

private void bunifuThinButton25_Click(object sender, EventArgs e)


{
ExitWindowsEx(0, 0);
}

private void bunifuThinButton23_Click(object sender, EventArgs e)


{//sleep mode
LockWorkStation();
}

private void power_Load(object sender, EventArgs e)


{

}
}
}

[Link] Management
It is another mdiparent form menu item which has the processes that is it can start apps such
as word excel notepad and others. At the same time the started apps can be killed, I
implemented using the combo box whereby we select the program you want to kill.
Program code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class Processmgn : Form
{
public Processmgn()
{
InitializeComponent();
}

private void Processmgn_Load(object sender, EventArgs e)


{

private void bunifuThinButton21_Click(object sender, EventArgs e)


{//starts word
[Link]("winword");
}

private void bunifuThinButton22_Click(object sender, EventArgs e)


{//starts calculator
[Link]("calc");
}

private void bunifuThinButton23_Click(object sender, EventArgs e)


{//starts excel
[Link]("excel");
}

private void bunifuThinButton24_Click(object sender, EventArgs e)


{//starts notepad
[Link]("notepad");
}

private void bunifuFlatButton1_Click(object sender, EventArgs e)


{//kills the selected process
foreach (var process in [Link]([Link]))
{
[Link]();
}
}

private void bunifuThinButton25_Click(object sender, EventArgs e)


{
[Link]();
}
}
}

Listing running processes

Program code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class frmprocesses : Form

public ExpandoObject GetProcessExtraInformation(int processId)


{
// Query the Win32_Process
// string query = "Select * From Win32_Process Where ProcessID = " + processId;
string query = "Select * From Win32_Process Where ProcessID = " + processId;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = [Link]();

// Create a dynamic object to store some properties on it


dynamic response = new ExpandoObject();
[Link] = "";
[Link] = "Unknown";

foreach (ManagementObject obj in processList)


{
// Retrieve username
string[] argList = new string[] { [Link], [Link] };
int returnVal = Convert.ToInt32([Link]("GetOwner", argList));
if (returnVal == 0)
{
// return Username
[Link] = argList[0];

// You can return the domain too like (PCDesktop-123123\Username using


instead
//[Link] = argList[1] + "\\" + argList[0];
}

// Retrieve process description if exists


if (obj["ExecutablePath"] != null)
{
try
{
FileVersionInfo info =
[Link](obj["ExecutablePath"].ToString());
[Link] = [Link];
}
catch { }
}
}

return response;
}
public string BytesToReadableValue(long number)
{
List<string> suffixes = new List<string> { " B", " KB", " MB", " GB", " TB", "
PB" };

for (int i = 0; i < [Link]; i++)


{
long temp = number / (int)[Link](1024, i + 1);

if (temp == 0)
{
return (number / (int)[Link](1024, i)) + suffixes[i];
}
}

return [Link]();
}

public frmprocesses()
{
InitializeComponent();
}

private void frmprocesses_Load(object sender, EventArgs e)


{

private void bunifuThinButton21_Click(object sender, EventArgs e)


{

// Create an array to store the processes


Process[] processList = [Link]();

// Create an Imagelist that will store the icons of every process


ImageList Imagelist = new ImageList();
// Loop through the array of processes to show information of every process in
your console
foreach (Process process in processList)
{
// Define the status from a boolean to a simple string
string status = ([Link] == true ? "Responding" : "Not
responding");

// Retrieve the object of extra information of the process (to retrieve


Username and Description)
dynamic extraProcessInfo = GetProcessExtraInformation([Link]);

// Create an array of string that will store the information to display in


our
string[] row = {
// 1 Process name
[Link],
// 2 Process ID
[Link](),
// 3 Process status
status,
// 4 Username that started the process
[Link],
// 5 Memory usage
BytesToReadableValue(process.PrivateMemorySize64),
// 6 Description of the process
[Link]
};

//
// As not every process has an icon then, prevent the app from crash
try
{
[Link](
// Add an unique Key as identifier for the icon (same as the ID of
the process)
[Link](),
// Add Icon to the List
[Link]([Link]).ToBitmap()
);
}
catch { }

// Create a new Item to add into the list view that expects the row of
information as first argument
ListViewItem item = new ListViewItem(row)
{
// Set the ImageIndex of the item as the same defined in the previous
try-catch
ImageIndex = [Link]([Link]())
};

// Add the Item


[Link](item);
}
}

private void bunifuThinButton22_Click(object sender, EventArgs e)


{
[Link]();
}

}
}

[Link] Management
It is menu item number three in the mdi parent form. In this the program lists all the disk
partitions, the space used and space available for each partition.
Program code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class Memory : Form
{
public Memory()
{
InitializeComponent();
getdrive();

}
private void getdrive()
{

{ }

}
private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
[Link](" COMPUTER HARDWARE INFORMATION");
int NoOfDisk = 0;
foreach (DriveInfo drinfo in [Link]())
{
[Link]("DriveName" + [Link] + "\n"
+ "DriveLabel :" + [Link] + "\n"
+ "Total Space:" + [Link] / 1073741824 + " GB\n"
+ "Total Free Space:" + [Link] / 1073741824 + " GB\n"
+ "DriveType :" + [Link]
);
}
[Link]("Total Disk:" + (NoOfDisk - 1).ToString());
}

private void bunifuThinButton25_Click(object sender, EventArgs e)


{
[Link]();
}

private void Memory_Load(object sender, EventArgs e)


{

}
}
}

[Link] Management
In communication management I implemented sockets, signals, Rpc and Serial
communication. Serial communication enables communication between ports (COM3,
COM4, COM5). Rpc implements remote communication where commands are typed in the
command prompt. Signals implement the use of timers for an event to occur.

 Serial Communication

Program Code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class communication : Form
{
public communication()
{
InitializeComponent();
}

private void communication_Load(object sender, EventArgs e)


{
string[] ports = [Link]();
[Link](ports);
[Link] = 0;
[Link] = false;
}

private void bunifuThinButton21_Click(object sender, EventArgs e)


{
[Link] = true;
[Link] = false;
try
{
[Link] = [Link];
[Link]();
}
catch (Exception ex) {

[Link]([Link], "Message", [Link],


[Link]);
}

private void bunifuThinButton23_Click(object sender, EventArgs e)


{

try
{
if ([Link])
{
[Link]([Link]+[Link]);
[Link]();
}
}
catch (Exception ex)
{

[Link]([Link], "Message", [Link],


[Link]);
}
}

private void bunifuThinButton22_Click(object sender, EventArgs e)


{
[Link] = false;
[Link] = true;
try
{

[Link]();
}
catch (Exception ex)
{

[Link]([Link], "Message", [Link],


[Link]);
}
}

private void bunifuThinButton24_Click(object sender, EventArgs e)


{
try
{
if ([Link])
{
[Link] = [Link]();
}
}
catch (Exception ex)
{
[Link]([Link], "Message", [Link],
[Link]);
}
}

private void communication_FormClosing(object sender, FormClosingEventArgs e)


{
if ([Link])
[Link]();
}

private void bunifuThinButton25_Click(object sender, EventArgs e)


{
[Link]();
}
}
}

 RPC

Program code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class Rpc : Form
{
public Rpc()
{
InitializeComponent();
}

private void Rpc_Load(object sender, EventArgs e)


{
string[] passedInAgs = [Link]();
if (([Link]("/h") || [Link]("/H")))
{
[Link]("You called Help, you got helP!");
}
else
{
foreach (string s in passedInAgs)
{
[Link](s);
}

}
}
}
}

 Signal

Program Code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
[Link] = true;
[Link] = false;
[Link] = false;
}

private void pictureBox1_Click(object sender, EventArgs e)


{

private void timer1_Tick(object sender, EventArgs e)


{
if ([Link] == true)
{
[Link] = false;
[Link] = false;
[Link] = true;
}
else if([Link] == true)
{
[Link] = false;
[Link] = true;
[Link] = false;
}
else if ([Link] == true)
{
[Link] = true;
[Link] = false;
[Link] = false;
}

private void bunifuThinButton21_Click(object sender, EventArgs e)


{
[Link] = true;
}

private void bunifuThinButton22_Click(object sender, EventArgs e)


{
[Link] = false;
}

private void Form1_Load(object sender, EventArgs e)


{

}
}
}

 Socket

Program Code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace monday
{
public partial class socket : Form
{
Socket sck;
EndPoint epLocal, epRemote;
byte[] buffer;
public socket()
{
InitializeComponent();
}

private void button3_Click(object sender, EventArgs e)


{
[Link]();
}

private void socket_Load(object sender, EventArgs e)


{
sck = new Socket([Link],[Link],[Link]);
[Link]([Link],[Link],true);
[Link] = GetLocalIP();
[Link] = GetLocalIP();

}
private string GetLocalIP()
{
IPHostEntry host;
host= [Link]([Link]());
foreach (IPAddress ip in [Link]) {
if ([Link] == [Link])
return [Link]();
}
return "[Link]";
}

private void buttonconect_Click(object sender, EventArgs e)


{

epLocal = new IPEndPoint([Link]([Link]),


Convert.ToInt32([Link]));
[Link](epLocal);
//epremote = new
IPEndPoint([Link]([Link] ),Convert.ToInt32(textremoteport));
epRemote = new IPEndPoint([Link]([Link]),
Convert.ToInt32([Link]));
[Link](epRemote);
buffer= new byte[1500];
[Link](buffer,0,[Link],[Link],ref epRemote,new
AsyncCallback(MessageCallBack),buffer);
if (textRemotePort == textLocalPort)
{
[Link](" connection failure");
}
else { [Link]("Connection established"); }
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
byte[] ReceavedData = new byte[1500];
ReceavedData = (byte[])[Link];
ASCIIEncoding aEncoding = new ASCIIEncoding();
string ReceavedMessage = [Link](ReceavedData);

[Link]("receaver:" + ReceavedMessage);
buffer = new byte[1500];
[Link](buffer, 0, [Link], [Link], ref
epRemote, new AsyncCallback(MessageCallBack), buffer);
}
catch (Exception ex) { [Link]([Link]()); }
}

private void buttonsend_Click(object sender, EventArgs e)


{
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendingMessage = new byte[1500];
sendingMessage = [Link]([Link]);
[Link](sendingMessage);
[Link]("sender: " +[Link]);
[Link]("sender: " + [Link]);
[Link] = " ";

private void button1_Click(object sender, EventArgs e)


{
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendingMessage = new byte[1500];
sendingMessage = [Link]([Link]);
[Link](sendingMessage);
[Link]("receaver: " + [Link]);
[Link]("receaver: " + [Link]);
[Link] = " ";
}

private void listmesage_SelectedIndexChanged(object sender, EventArgs e)


{

}
}

[Link] Management
This gives the device name and the user logged in. It Displays the Pc Ip address.

Program Code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace WindowsFormsApplication1
{
public partial class frminformation : Form
{
public frminformation()
{
InitializeComponent();
}

private void frminformation_Load(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
string comname = [Link];
string username = [Link];
[Link] = comname;
[Link] = username;
}

private void button3_Click(object sender, EventArgs e)


{
IPAddress[] localIP = [Link]([Link]());
foreach (IPAddress address in localIP)
{ [Link] = [Link](); }
}
}
}

You might also like