2010年9月14日 星期二

C# Linq To Entity Framework

最近因為Linq To Sql 要倒了
所以在找 Linq To Entity Framework的方法
結果 哈哈哈 差不多東西阿 換湯不換藥
使用起來還是很簡單的說 XD

只是一開始是做Linq To Sql 變成做Entity Data Model
呼叫方式變成

LinqDataContext linqDB = new LinqDataContext();
Entities1 entity1 = new Entities1();

接下來就都一樣囉 >_^

2010年9月9日 星期四

C# Textbox 浮水印 WaterMark

再應用程式內的浮水印
利用參考網站大大分享的CODE所弄的

要進去改Designer的元件

//this.textBox1 = new System.Windows.Forms.TextBox(); //這個改成下面那個
this.textBox1 = new wmgCMS.WaterMarkTextBox(); //改成這個
//---------元件設定部份
this.textBox1 .WaterMarkColor = System.Drawing.Color.Gray;
this.textBox1 .WaterMarkText = "浮水印要顯示的字";
//---------最下面
//private System.Windows.Forms.TextBox textBox1; //這個改成下面那個
private wmgCMS.WaterMarkTextBox textBox1; //改成這個



參考網站

C# 選擇資料夾,選擇檔案

選擇資料夾

FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
textBox1.Text = path.SelectedPath;


選擇檔案

OpenFileDialog file = new OpenFileDialog();
file.ShowDialog();
textBox1.Text = file.SafeFileName;

2010年9月8日 星期三

C# 控制遠端電腦

就是用WMI來做壞事 阿 不是啦 做控制
就是利用WMI可以做遠端電腦的控制
如 關機..重開..一些資訊擷取等

關機 重開

ConnectionOptions options = new ConnectionOptions();
options.Username = "Username"; // 使用者名稱
options.Password = "Password"; // 使用者密碼
options.Authentication = AuthenticationLevel.Default; // 認證模式設定 (採用預設)
options.Impersonation = ImpersonationLevel.Impersonate; // 設定 COM 模擬等級
options.EnablePrivileges = true; // 參考 **(特一)
try
{

ManagementScope MS_Conn = new ManagementScope("\\\\" + "IP" + "\\root\\cimv2", options);
MS_Conn.Connect();


ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");


ManagementObjectSearcher mos1 = new ManagementObjectSearcher(MS_Conn, oq);

ManagementObjectCollection moc1 = mos1.Get();


foreach (ManagementObject mo in moc1)
{

mo.InvokeMethod("Reboot", null);
}
}
catch (Exception err1)
{
textBox1.Text= err1.Message;
} 



參考網站1 參考網站2

2010年9月7日 星期二

C# 檔案,資料夾 Move Copy Del Insert

檔案複製

string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
System.IO.File.Copy(sourceFile, destFile, true);


檔案搬移

System.IO.File.Move(sourceFile, destinationFile);


檔案刪除

if(System.IO.File.Exists(@"C:\Users\Public\DeleteTest\test.txt"))
{
// Use a try block to catch IOExceptions, to
// handle the case of the file already being
// opened by another process.
try
{
System.IO.File.Delete(@"C:\Users\Public\DeleteTest\test.txt");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}


建立資料夾

if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}



複製資料夾

if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);

// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}


搬移資料夾

System.IO.Directory.Move(@"C:\Users\Public\public\test\", @"C:\Users\Public\private");


刪除資料夾

if(System.IO.Directory.Exists(@"C:\Users\Public\DeleteTest"))
{
try
{
System.IO.Directory.Delete(@"C:\Users\Public\DeleteTest", true);
}

catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}


參考網站

2010年9月5日 星期日

C# textbox 驗證

可以用keyPress做驗證
這樣在KEY入就檢查各個單字是否可輸入

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int ascii = Convert.ToInt16(e.KeyChar);
if ((ascii >= 97 && ascii <= 122) || (ascii == 8))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}

或是用Validating來做驗證

private void textBox1_Validating(object sender, CancelEventArgs e)
{
try
{
int numberEntered = int.Parse(textBox1.Text);
if (numberEntered < 1 || numberEntered > 10)
{
e.Cancel = true;
MessageBox.Show("You have to enter a number between 1 and 10");
}
}
catch (FormatException)
{
e.Cancel = true;
MessageBox.Show("You need to enter an integer");
}
}

第一個是一輸入的時候就檢查
第二個是整個輸入結束執行時檢查

2010年9月2日 星期四

C# DataTable TO DataGridView Image 問題

最近試了DataGridView要Show圖
可是怎都怎錯
後來發現...
原來我DataTable沒設屬性
所以才會不能顯示

DataTable1.Columns.Add("ImageValue", typeof(Image));
Image OnlineImage = Image.FromFile("online.png");
DataTable1.Rows[i]["ImageValue"] = OnlineImage;
dataGridView1.DataSource = DataTable1;