Hi community,
We have a new site "technic meets art"
Freaks Of Developer
Tuesday, June 17, 2014
Saturday, June 14, 2014
Friday, June 13, 2014
List Example
What is a List?
A list is a collection of data.Example
- Create a new list
//initialize an empty list for objects with type of INTEGERList<int> numbers = new List<int>();
//initialize an empty list for objects with type of STRING
List<string> names = new List<string>();
//initialize a list with objects type of INTEGER
List<int> numbers = new List<int>() {1,2,3,4}; //List with 4 integer elements
//initialize a list with objects type of STRINGList<string> names = new List<string>() {"Dad","Mum","Child"}; //List with 3 string elements
- Add elements to a list
//add one element at the end numbers.Add(5);
//add a collection of new elements at the end
numbers.AddRange(new List<int>() { 6, 7, 8 });
//add one element at the end
names.Add("Child2");
//add a collection of new elements at the end
names.AddRange(new List<string>() { "Child3", "Child4" });
//add one element at a defined position
numbers.Insert(8, 9);
//add one element at a defined position
names.Insert(6, "Child5");
//add a collection of new elements starting at a defined position
numbers.InsertRange(9, new List<int>() { 10, 11, 12 });
//add a collection of new elements starting at a defined position
names.InsertRange(7, new List<string>() { "Child6", "Child7" });
- Remove elements from a list
//remove the object with the value 8 from the list numbers.Remove(8);
//remove the opbject with the value Child from the list
names.Remove("Child");
//remove object at position 3
numbers.RemoveAt(3);
//remove 2 elements, starting at position 4
numbers.RemoveRange(4, 2);
CodeProject Foxload
Friday, June 6, 2014
Network Client Monitor C# -- Session 1
Now I will show you an example of a Network Client Monitor.
I will split this topic in 4 sessions:
Session 1:
- Create the Shell form with a menu strip and a status strip
- Create a static container (will be the computer)
- Ping to the computer IP address and set the state (success = green / fail == red)
First we start with an empty windows forms project.
Add a status strip item and a menu strip item to the empty form.
In our example we call it ShellStatusStrip and ShellMenuStrip.
Next we will add a new menu item to the ShellMenuStrip. We call it "Edit"
Also to the status strip we will add a new label called "Status"
Now add a groupbox and inside the groupbox we will add a label.
We set the back color to yellow.
Now we will implement the ping command in the Form_Load event.
We will use our local IP address (127.0.0.1) for this test case.
If we now run the program, we will see that the label will get the color GREEN if the connection(ping) was successful or RED if the connection was fail.
For Session 2:
- Create a separate thread for the ping command
- Use MethodInvoker to update the form
- Implement a XmlSerializer for the configuration
Kind regards,
FreaksOfDeveloper
CodeProject Foxload
I will split this topic in 4 sessions:
Session 1:
- Create the Shell form with a menu strip and a status strip
- Create a static container (will be the computer)
- Ping to the computer IP address and set the state (success = green / fail == red)
First we start with an empty windows forms project.
Add a status strip item and a menu strip item to the empty form.
In our example we call it ShellStatusStrip and ShellMenuStrip.
Next we will add a new menu item to the ShellMenuStrip. We call it "Edit"
For the Edit Menu we will create two new menu items called "Modify Computer" and "Add new Computer".
Also to the status strip we will add a new label called "Status"
Now add a groupbox and inside the groupbox we will add a label.
We set the back color to yellow.
Now we will implement the ping command in the Form_Load event.
We will use our local IP address (127.0.0.1) for this test case.
If we now run the program, we will see that the label will get the color GREEN if the connection(ping) was successful or RED if the connection was fail.
For Session 2:
- Create a separate thread for the ping command
- Use MethodInvoker to update the form
- Implement a XmlSerializer for the configuration
Kind regards,
FreaksOfDeveloper
CodeProject Foxload
FileSystemWatcher C#
Short example how to implement the FileSystemWatcher class.
In my example I´ve used a empty windows forms application.
First stepp, add a textbox and a button to the form.
Next we will implement the FileSystemWatcher class.
I´ve used in this example the Start button to initialize the FileSystemWatcher class.
I´ve created the FileSystemWatcher class to monitor a local path.
With watcher.Created += watcherEvent, watcher.Deleted += watcherEvent and watcher.Renamed += watcherEvent we will create three events.
The first event will happen if a new file is created in this folder. (watcher.Created += watcherEvent)
The second event will happen if a file is deleted in this folder. (watcher.Deleted += watcherEvent)
The third event will happen if a file is renamed in this folder. (watcher.Renamed += watcherEvent)
As next step we have to create the watcherEvent function.
Within this function we will handle all three events.
In my example, I will just write the event name to the text box.
Therefore we have to implement a delegate.
This is necessary because the FileSystemWatcher is running in the background as separate thread and it is not possible to update a object in the form from a new sub thread.
To handle this, I´ve created a function called "UpdateWatcherOutput" and also the delegate UpdateWatcherOutputCallback.
In this function we can update the text box with the event name.
In the event function from the FileSystemWatcher we will check if a Invoke is required. (WatcherOutputTextBox.InvokeRequired)
If YES, we will create a new delegate UpdateWatcherOutputCallback and will invoke it.
Now you can start the application and with the Start button the FileSystemWatcher will start with his work.
When you now create, delete or rename a file in the defined path, you will see the event name in the text box.
Have fun :-)
Kind regards,
FreaksOfDeveloper
CodeProject Foxload
In my example I´ve used a empty windows forms application.
First stepp, add a textbox and a button to the form.
Next we will implement the FileSystemWatcher class.
I´ve used in this example the Start button to initialize the FileSystemWatcher class.
I´ve created the FileSystemWatcher class to monitor a local path.
With watcher.Created += watcherEvent, watcher.Deleted += watcherEvent and watcher.Renamed += watcherEvent we will create three events.
The first event will happen if a new file is created in this folder. (watcher.Created += watcherEvent)
The second event will happen if a file is deleted in this folder. (watcher.Deleted += watcherEvent)
The third event will happen if a file is renamed in this folder. (watcher.Renamed += watcherEvent)
As next step we have to create the watcherEvent function.
Within this function we will handle all three events.
In my example, I will just write the event name to the text box.
Therefore we have to implement a delegate.
This is necessary because the FileSystemWatcher is running in the background as separate thread and it is not possible to update a object in the form from a new sub thread.
To handle this, I´ve created a function called "UpdateWatcherOutput" and also the delegate UpdateWatcherOutputCallback.
In this function we can update the text box with the event name.
In the event function from the FileSystemWatcher we will check if a Invoke is required. (WatcherOutputTextBox.InvokeRequired)
If YES, we will create a new delegate UpdateWatcherOutputCallback and will invoke it.
Now you can start the application and with the Start button the FileSystemWatcher will start with his work.
When you now create, delete or rename a file in the defined path, you will see the event name in the text box.
Have fun :-)
Kind regards,
FreaksOfDeveloper
CodeProject Foxload
Monday, June 2, 2014
Impersonator C#
I think everybody was already at this point... A program should handle file transfers or modifications but the access to the target path is not given to everybody.
How to handle this situation? :-(
One answer could be, to use one common account (inside the program) to do these actions.
I have tried to solve this situation like following:
1) Create a new class "Impersonator"
using System;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace Test.Impersonator
{
public class Impersonator : IDisposable
{
public Impersonator(string userName, string domainName, string password)
{
ImpersonateValidUser(userName, domainName, password);
}
public void Dispose()
{
UndoImpersonation();
}
[DllImport("advapi32.dll", SetLastError = true)]
private static extern int LogonUser(string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool CloseHandle(IntPtr handle);
private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_PROVIDER_DEFAULT = 0;
private void ImpersonateValidUser(string userName, string domain, string password)
{
WindowsIdentity tempWindowsIdentity = null;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
try
{
if (RevertToSelf())
{
if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
finally
{
if (token != IntPtr.Zero)
{
CloseHandle(token);
}
if (tokenDuplicate != IntPtr.Zero)
{
CloseHandle(tokenDuplicate);
}
}
}
private void UndoImpersonation()
{
if (impersonationContext != null)
{
impersonationContext.Undo();
}
}
private WindowsImpersonationContext impersonationContext = null;
}
}
2) Use the Impersonator class in you main program around the function which should run under a different user.
using...
using Test.Impersonator;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void StartDataTransfer()
{
//Do something as User1
using (new Impersonator("UserName", "Domain", "Password"))
{
//Do something as User2
}
}
}
}
Feel free to ask/feedback :-)
Kind regards,
FreaksOfDeveloper
CodeProject Foxload
Subscribe to:
Posts (Atom)