Sie sind auf Seite 1von 8

August 31, 2012

[WINFORM 02]

Form main

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace SendMail
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FrmEmail frm = new FrmEmail();
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
this.Hide();
frm.Show();
}
void frm_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show();
}
private void button2_Click(object sender, EventArgs e)
{
FrmWeb frm = new FrmWeb();
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
this.Hide();
frm.Show();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

CUSC

Page 1

August 31, 2012

using
using
using
using
using
using
using
using
using
using
using

[WINFORM 02]

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Net.Mail;
System.IO;
System.Net;

namespace SendMail
{
public partial class FrmEmail : Form
{
public FrmEmail()
{
InitializeComponent();
IPHostEntry IP = Dns.Resolve("smtp.gmail.com");
lblDNS.Text = "smtp.gmail.com";
foreach (IPAddress var in IP.AddressList)
{
lblDNS.Text = lblDNS.Text + " - " + var.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.Text = "mail sending .....";

CUSC

Page 2

August 31, 2012

[WINFORM 02]

toolStripProgressBar1.Visible = true;
SmtpClient mail = new SmtpClient("smtp.gmail.com", 587);
mail.EnableSsl = true;
System.Net.NetworkCredential cert = new
System.Net.NetworkCredential("ngotuongdan04", "ngotuongdan");
mail.Credentials = cert;
mail.SendCompleted += new
SendCompletedEventHandler(mail_SendCompleted);
MailMessage message = new MailMessage(txtF.Text, txtT.Text);
message.Sender = new MailAddress("ngotuongdan04@gmail.com");
message.Subject = txtS.Text;
message.Body = rtContent.Text;
message.CC.Add(new MailAddress("ngotuongdan01@gmail.com"));
message.Bcc.Add(new MailAddress("ntdan@ctu.edu.vn"));
if (File.Exists(textBox1.Text))
message.Attachments.Add(new Attachment(textBox1.Text));
mail.SendAsync(message, this);
btnSend.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
toolStripProgressBar1.Visible = false;
}
}
void mail_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
MessageBox.Show("Send completed");
else
MessageBox.Show(e.Error.ToString());
btnSend.Enabled = true;
toolStripProgressBar1.Visible = false;
this.Text = "mail sent";
}
private void textBox1_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.InitialDirectory = "d:\\";
if (op.ShowDialog() == DialogResult.OK)
{
textBox1.Text = op.FileName;
}
}
}
}

CUSC

Page 3

August 31, 2012

using
using
using
using
using
using
using
using
using
using

[WINFORM 02]

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Media;
System.Net;

namespace SendMail
{
public partial class FrmWeb : Form
{
public FrmWeb()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(new Uri(textBox1.Text));
WebClient c = new WebClient();
c.DownloadFile(new Uri(textBox1.Text), "web.html");
}
private void button2_Click(object sender, EventArgs e)

CUSC

Page 4

August 31, 2012

[WINFORM 02]

{
webBrowser1.GoBack();
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
SoundPlayer mobj = new SoundPlayer();
mobj.SoundLocation = "";
mobj.Play();
}
}
}

M ha v chng thc
using
using
using
using
using
using

System;
System.Security;
System.Security.Principal;
System.Security.Permissions;
System.IO;
System.Threading;

namespace T2
{
public class Program
{
//[PrincipalPermissionAttribute(SecurityAction.Demand, Name =
"admin", Role = "Administrators")]
[PrincipalPermissionAttribute(SecurityAction.Demand, Role =
"Administrators")]
public static void PrivateInfo()
{
Console.WriteLine("You have access to the private data!");
}
public static void Main()
{

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
try
{
Program.PrivateInfo();
Console.ReadLine();
}
catch (SecurityException ex)
{
Console.Write(ex.ToString());
Console.ReadLine();
}
//try
//{
//
Principalidentity.Display();

CUSC

Page 5

August 31, 2012

[WINFORM 02]

//}
//catch (SecurityException exception)
//{
//
Console.WriteLine("Security exception caught(" +
exception.Message + ")");
//
Console.WriteLine("The current principal must be in the
local users group");
//}
Cls_Cryptogrphy.Demo();
Console.ReadLine();
}
}
public class Principalidentity
{
[PrincipalPermissionAttribute(SecurityAction.Demand, Role =
"BUILTIN\\Users")]
public static void Display()
{
Console.WriteLine("The current principal is logged in locally");
WindowsPrincipal MyPrincipal =
(WindowsPrincipal)Thread.CurrentPrincipal;
if (MyPrincipal.Identity.Name.Equals(@"MYDOMAIN\myuser") &&
MyPrincipal.Identity.IsAuthenticated.Equals(true))
Console.WriteLine("You are authenticated !");
else
Console.WriteLine("You are not authenticated!");
}
}
}

using
using
using
using

System;
System.Collections.Generic;
System.Text;
System.Security.Cryptography;

namespace T2
{
class Cls_Cryptogrphy
{
public static string Encrypt(string toEncrypt, string key, bool
useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new
MD5CryptoServiceProvider();
keyArray =
hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));

CUSC

Page 6

August 31, 2012

[WINFORM 02]

}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new
TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray =
cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0,
resultArray.Length);
}
public static string Decrypt(string toDecrypt, string key, bool
useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new
MD5CryptoServiceProvider();
keyArray =
hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new
TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray =
cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
public static void Demo()
{
string ClearText = "This-is-text";
string key = "security";
string str = Encrypt(ClearText, key,true);
Console.Write("\n {0} \n is encrypt of \n{1} ", str, ClearText);
Console.Write("\n\n {0} \n is descrypt of \n{1}", Decrypt(str,
key,true), str);

CUSC

Page 7

August 31, 2012

[WINFORM 02]

Console.ReadLine();
}
}
}

using System;
using System.IO;
using System.Security.Permissions;
using System.Security;
namespace T2
{
class TestApp
{
public TestApp()
{
FileIOPermission p1 = new
FileIOPermission(FileIOPermissionAccess.AllAccess, "d:\\XFile");
p1.Deny();
AttemptAccess("Deny permissions", "d:\\XFile");
CodeAccessPermission.RevertDeny();
AttemptAccess("Revert Deny permissions","d:\\XFile");
}
public void AttemptAccess(String s, string filename)
{
FileStream fs = null;
try
{
Console.Write(s);
fs = new FileStream(filename, FileMode.OpenOrCreate);
Console.WriteLine(" " + ((fs != null) ? "FileOpen" :
"FileNOTOpened"));
fs.Close();
File.Delete(filename);
Console.WriteLine("File," + filename + "Deleted");
}
catch (Exception)
{
Console.WriteLine("Error : " + ((fs != null) ?
"FileOpen" : "FileNOTOpened"));
if (fs != null) fs.Close();
}
}
}
}

CUSC

Page 8

Das könnte Ihnen auch gefallen