1.download iTextSharp.dll,Please find it by yourself
At VS2012 or other version:
2.import iTextSharp to reference
3.using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
4.At Button Event:
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
//openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
string filename = Path.GetFileName(openFileDialog1.FileName);
if ((myStream = openFileDialog1.OpenFile()) != null)
{
textBox1.Text = filename;
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string InputFile = Path.Combine(WorkingFolder, "xx.pdf");
string OutputFile = Path.Combine(WorkingFolder, "xx_enc.pdf");
using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, "secret", "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
2013年11月22日 星期五
2013年11月14日 星期四
Download file from FTP Server
try
{
string FileNameToDownload = "filename.txt"; //檔名
string DownloadFilePath = @"D://"; //存檔路徑
FileStream outputStream = new FileStream(DownloadFilePath + "\\" + FileNameToDownload, FileMode.Create);
string FTPip = "192.168.100.100"; //FTP Server IP
FtpWebRequest repFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"+FTPip+"/path/"+FileNameToDownload));
repFTP.Method = WebRequestMethods.Ftp.DownloadFile;
repFTP.UseBinary = true;
repFTP.Proxy = null;
string ftpUserID = "user";
string ftpPassword = "password";
repFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)repFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int buffersize = 2048;
int readCount;
byte[] buffer = new byte[buffersize];
readCount = ftpStream.Read(buffer, 0, buffersize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, buffersize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
MessageBox.Show("Complete!");
}
catch (WebException ex)
{
String status = ((FtpWebResponse)ex.Response).StatusDescription;
MessageBox.Show(status);
}
{
string FileNameToDownload = "filename.txt"; //檔名
string DownloadFilePath = @"D://"; //存檔路徑
FileStream outputStream = new FileStream(DownloadFilePath + "\\" + FileNameToDownload, FileMode.Create);
string FTPip = "192.168.100.100"; //FTP Server IP
FtpWebRequest repFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"+FTPip+"/path/"+FileNameToDownload));
repFTP.Method = WebRequestMethods.Ftp.DownloadFile;
repFTP.UseBinary = true;
repFTP.Proxy = null;
string ftpUserID = "user";
string ftpPassword = "password";
repFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)repFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int buffersize = 2048;
int readCount;
byte[] buffer = new byte[buffersize];
readCount = ftpStream.Read(buffer, 0, buffersize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, buffersize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
MessageBox.Show("Complete!");
}
catch (WebException ex)
{
String status = ((FtpWebResponse)ex.Response).StatusDescription;
MessageBox.Show(status);
}
2013年8月20日 星期二
2013年8月8日 星期四
Which RadioButton selected inside GroupBox or Panel
foreach (RadioButton rb in groupBox5.Controls.OfType<RadioButton>())
{
if(rb.Checked)
MessageBox.Show(rb.text);
}
{
if(rb.Checked)
MessageBox.Show(rb.text);
}
在Windows Form上面放一個Table,動態產生XY軸及控制項
我不會將Code鉅細靡遺通通放上來,基本操作是programer的基本功,應該自己想辦法弄懂。
1.依指定的XY數量產生表格:先放一個TableLayoutPanel元件,建立一個Function:
//定益表格參數
int y = 3;
int x = 2;
tableLayoutPanel1.Visible = false;
tableLayoutPanel1.BackColor = Color.FromArgb(192, 255, 192);
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.Refresh();
tableLayoutPanel1.RowCount = y;
tableLayoutPanel1.ColumnCount = x;
for (int co = 0; co < x; co++)
{
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / x);
tableLayoutPanel1.ColumnStyles.Add(cs);
}
for (int ro = 0; ro < y; ro++)
{
RowStyle rs = new RowStyle(SizeType.Percent, 100 / y);
tableLayoutPanel1.RowStyles.Add(rs);
}
2.以下範例,可在產生表格後、分別產生第一列及第一欄的Label
//產生第一欄Label
for (int co = 0; co < x; co++)
{
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / x);
tableLayoutPanel4.ColumnStyles.Add(cs);
if (co == 0)
{
for (int r = 0; r < y; r++)
{
if (r != 0)
{
Label LblName;
LblName = new Label();
LblName.Dock = DockStyle.Fill;//填滿表格
LblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;//文字置中
LblName.BackColor = Color.FromArgb(255, 255, 192);//背景顏色
LblName.Font = new Font(LblName.Font, FontStyle.Bold);//粗體字
LblName.ForeColor = Color.Red;//字體紅色
if(x >=16)
LblName.Font = new Font("標楷體", 7);
LblName.Name = "R" + Convert.ToInt16(r);
LblName.Text = "R" + Convert.ToInt16(r);
tableLayoutPanel1.Controls.Add(LblName, 0, r);
}
}
}
}
//產生第一列Label
for (int ro = 0; ro < y; ro++)
{
RowStyle rs = new RowStyle(SizeType.Percent, 100 / y);
tableLayoutPanel4.RowStyles.Add(rs);
if (ro == 0)
{
for (int c = 0; c < x;c++)
{
if (c != 0)
{
Label LblName;
LblName = new Label();
LblName.Dock = DockStyle.Fill;//填滿表格
LblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;//文字置中
LblName.BackColor = Color.FromArgb(255, 255, 192);//背景顏色
//LblName.Font = new Font(LblName.Font, FontStyle.Bold);//粗體字
LblName.ForeColor = Color.Red;//字體紅色
if (x >= 17)
LblName.Font = new Font("標楷體", 8);
if (x >= 20)
LblName.Font = new Font("標楷體", 7);
LblName.Name = "C" + Convert.ToInt16(c);
LblName.Text = "C" + Convert.ToInt16(c);
tableLayoutPanel1.Controls.Add(LblName, c, 0);
}
}
}
}
3.點選TableLayoutPanel時,動態產生Label:從TableLayoutPanel的MouseClick事件
int row = 0;
int verticalOffset = 0;
tableLayoutPanel1.Visible = false;//產生Label的過程,畫面會跑來跑去,所以先隱藏掉
foreach (int h in tableLayoutPanel1.GetRowHeights())
{
int column = 0;
int horizontalOffset = 0;
foreach (int w in tableLayoutPanel1.GetColumnWidths())
{
Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h);
if (rectangle.Contains(e.Location))
{
//產生Label
Label LblName;
LblName = new Label();
LblName.Dock = DockStyle.Fill;//填滿表格
LblName.ForeColor = Color.Red;//字體紅色
LblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;//文字置中
LblName.Font = new Font(LblName.Font, FontStyle.Bold);//粗體字
LblName.Font = new Font("標楷體", 10);
LblName.Name = "A1";
LblName.Text = "A1";
tableLayoutPanel1.Controls.Add(LblName, column, row);
LblName.Click += new System.EventHandler(LblName_Click);//動態事件
tableLayoutPanel4.Visible = true;
return;
}
horizontalOffset += w;
column++;
}
verticalOffset += h;
row++;
}
4.宣告點擊動態產生的Label
private void LblName_Click(object sender, System.EventArgs e)
{
dosomething;
}
1.依指定的XY數量產生表格:先放一個TableLayoutPanel元件,建立一個Function:
//定益表格參數
int y = 3;
int x = 2;
tableLayoutPanel1.Visible = false;
tableLayoutPanel1.BackColor = Color.FromArgb(192, 255, 192);
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.Refresh();
tableLayoutPanel1.RowCount = y;
tableLayoutPanel1.ColumnCount = x;
for (int co = 0; co < x; co++)
{
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / x);
tableLayoutPanel1.ColumnStyles.Add(cs);
}
for (int ro = 0; ro < y; ro++)
{
RowStyle rs = new RowStyle(SizeType.Percent, 100 / y);
tableLayoutPanel1.RowStyles.Add(rs);
}
2.以下範例,可在產生表格後、分別產生第一列及第一欄的Label
//產生第一欄Label
for (int co = 0; co < x; co++)
{
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / x);
tableLayoutPanel4.ColumnStyles.Add(cs);
if (co == 0)
{
for (int r = 0; r < y; r++)
{
if (r != 0)
{
Label LblName;
LblName = new Label();
LblName.Dock = DockStyle.Fill;//填滿表格
LblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;//文字置中
LblName.BackColor = Color.FromArgb(255, 255, 192);//背景顏色
LblName.Font = new Font(LblName.Font, FontStyle.Bold);//粗體字
LblName.ForeColor = Color.Red;//字體紅色
if(x >=16)
LblName.Font = new Font("標楷體", 7);
LblName.Name = "R" + Convert.ToInt16(r);
LblName.Text = "R" + Convert.ToInt16(r);
tableLayoutPanel1.Controls.Add(LblName, 0, r);
}
}
}
}
//產生第一列Label
for (int ro = 0; ro < y; ro++)
{
RowStyle rs = new RowStyle(SizeType.Percent, 100 / y);
tableLayoutPanel4.RowStyles.Add(rs);
if (ro == 0)
{
for (int c = 0; c < x;c++)
{
if (c != 0)
{
Label LblName;
LblName = new Label();
LblName.Dock = DockStyle.Fill;//填滿表格
LblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;//文字置中
LblName.BackColor = Color.FromArgb(255, 255, 192);//背景顏色
//LblName.Font = new Font(LblName.Font, FontStyle.Bold);//粗體字
LblName.ForeColor = Color.Red;//字體紅色
if (x >= 17)
LblName.Font = new Font("標楷體", 8);
if (x >= 20)
LblName.Font = new Font("標楷體", 7);
LblName.Name = "C" + Convert.ToInt16(c);
LblName.Text = "C" + Convert.ToInt16(c);
tableLayoutPanel1.Controls.Add(LblName, c, 0);
}
}
}
}
3.點選TableLayoutPanel時,動態產生Label:從TableLayoutPanel的MouseClick事件
int row = 0;
int verticalOffset = 0;
tableLayoutPanel1.Visible = false;//產生Label的過程,畫面會跑來跑去,所以先隱藏掉
foreach (int h in tableLayoutPanel1.GetRowHeights())
{
int column = 0;
int horizontalOffset = 0;
foreach (int w in tableLayoutPanel1.GetColumnWidths())
{
Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h);
if (rectangle.Contains(e.Location))
{
//產生Label
Label LblName;
LblName = new Label();
LblName.Dock = DockStyle.Fill;//填滿表格
LblName.ForeColor = Color.Red;//字體紅色
LblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;//文字置中
LblName.Font = new Font(LblName.Font, FontStyle.Bold);//粗體字
LblName.Font = new Font("標楷體", 10);
LblName.Name = "A1";
LblName.Text = "A1";
tableLayoutPanel1.Controls.Add(LblName, column, row);
LblName.Click += new System.EventHandler(LblName_Click);//動態事件
tableLayoutPanel4.Visible = true;
return;
}
horizontalOffset += w;
column++;
}
verticalOffset += h;
row++;
}
4.宣告點擊動態產生的Label
private void LblName_Click(object sender, System.EventArgs e)
{
dosomething;
}
2013年6月25日 星期二
用System.DirectoryServices.AccountManagement管理AD帳號
http://www.codeproject.com/Articles/38344/Using-System-DirectoryServices-AccountManagement
根據已上網址資料,改以中文方式簡要說明:
1.System.DirectoryServices.AccountManagement 這是.NET Framework 3.5以後才有的,
.NET Framework 2.0 則是使用 DirectoryEntry,
2者相較,System.DirectoryServices.AccountManagement效能較佳。
2.相關物件說明:
PrincipalContext:與AD的連接物件
Principal:提供AD單元儲存、刪除、新增
GroupPrincipal:由Principal衍生而來,對Group單元做儲存、刪除、新增
UserPricipal:由Principal衍生而來,對User單元做儲存、刪除、新增
3.程式範例:
新增帳號
using (var pc = new PrincipalContext(ContextType.Domain,"domain.com", "DC=domain,DC=com"))
{
using (var up = new UserPrincipal(pc))
{
UserPrincipal usr = UserPrincipal.FindByIdentity(pc, "NewUser");
if (usr != null)
{
MessageBox.Show("NewUser,此帳號已經存在!");
return;
}
else
{
try
{
up.SamAccountName = textBox1.Text;
up.SetPassword("P@ssw0rd");
up.Enabled = true;
//up.PasswordNeverExpires = true;//密碼永久有效
up.ExpirePasswordNow();//下次登入時更改密碼
up.Save();
}
catch (Exception ex)
{
MessageBox.Show("Fail!:" + ex);
Application.Exit();
}
}
}
}
將新增的帳號加入群組
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "domain.com", DC=com");
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx,"Domain Users");
if (grp != null)
{
grp.Members.Add(ctx,IdentityType.Name,"NewUser");
grp.Save();
grp.Dispose();
}
ctx.Dispose();
根據已上網址資料,改以中文方式簡要說明:
1.System.DirectoryServices.AccountManagement 這是.NET Framework 3.5以後才有的,
.NET Framework 2.0 則是使用 DirectoryEntry,
2者相較,System.DirectoryServices.AccountManagement效能較佳。
2.相關物件說明:
PrincipalContext:與AD的連接物件
Principal:提供AD單元儲存、刪除、新增
GroupPrincipal:由Principal衍生而來,對Group單元做儲存、刪除、新增
UserPricipal:由Principal衍生而來,對User單元做儲存、刪除、新增
3.程式範例:
新增帳號
using (var pc = new PrincipalContext(ContextType.Domain,"domain.com", "DC=domain,DC=com"))
{
using (var up = new UserPrincipal(pc))
{
UserPrincipal usr = UserPrincipal.FindByIdentity(pc, "NewUser");
if (usr != null)
{
MessageBox.Show("NewUser,此帳號已經存在!");
return;
}
else
{
try
{
up.SamAccountName = textBox1.Text;
up.SetPassword("P@ssw0rd");
up.Enabled = true;
//up.PasswordNeverExpires = true;//密碼永久有效
up.ExpirePasswordNow();//下次登入時更改密碼
up.Save();
}
catch (Exception ex)
{
MessageBox.Show("Fail!:" + ex);
Application.Exit();
}
}
}
}
將新增的帳號加入群組
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "domain.com", DC=com");
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx,"Domain Users");
if (grp != null)
{
grp.Members.Add(ctx,IdentityType.Name,"NewUser");
grp.Save();
grp.Dispose();
}
ctx.Dispose();
2013年3月27日 星期三
Delete、Truncate、Drop Table的差別
資料來源:
月神的咖啡館
三、使用Drop Table:
月神的咖啡館
許多情況下,我們需要刪除整個資料表內的資料,重新輸入新資料,有三種方法可以參考使用,分別是使用Delete、Trancate與Drop,那使這三種方式的差異為何呢?
一、使用Delete :
使用Delete語法,不加任何Where條件下,是可以移除整個Table的資料,這個方式簡單易懂,但是卻會耗用大量資源,因為Delete語法:
1.Delete屬DML命令,不加任何Where條件下,會一筆一筆的移除資料列,且在交易記錄中每筆刪除的資料,都會記錄成一個項目,當整個Table的資料移除完時,已耗用許多時間。
2.若Table存在刪除的觸發程序(Trigger)時,將會引發觸發程序。
3.如果該Table包含識別欄位,使用Delete陳述式刪除資料表中所有資料時,並不會讓識別編號重置,所以後續再新增的資料,仍會從之前最後一個編號之後繼續編號下去
二、使用Truncate :
使用Truncate Table 會移除資料表中的所有資料列,但會保留資料表結構及其欄位、條件約束、索引等。
如果該Table包含識別欄位,這個識別編號會重設為原本定義的初始值。如果未定義任何初始值,就會使用預設值 1。
Trance Table 與Delete 比較的優點為:
1.使用的交易記錄空間較少:Truncate Table會取消配置用來儲存資料表資料的資料頁,以移除資料,所以交易記錄只會記錄頁面的取消配置。
2.通常會使用較少鎖定:Delete會鎖定每一筆資料,以便執行刪除,但Truncate Table只鎖定資料表和頁面,不會鎖定每個資料列。
3.速度較快:綜合以上兩點,Truncate Table會比Delete快!
Truncate Table 語法範例:
TRUNCATE TABLE MyTestTable
三、使用Drop Table:
使用Drop Tablec會移除一或多個資料表定義及這些資料表的所有資料、索引、觸發程序、條件約束和權限規格,也就是完全刪除一個資料表。
當卸除資料表時,資料表的規則或預設值會失去它們的繫結,資料表的任何相關條件約束或觸發程序也都會自動卸除。如果重新建立資料表,您必須重新繫結適當的規則和預設值、重新建立任何觸發程序,以及加入所有必要的條件約束。
所以使用Drop Table後,若你要重新使用被刪除的資料表,就只好重新Create Table了,但是要注意,Drop Table 和 Create Table 不得在相同批次的相同資料表上執行。否則,系統可能會發生非預期的錯誤。
Drop Table 語法範例:
DROP TABLE MyTestTable
訂閱:
文章 (Atom)