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;

2010年8月30日 星期一

T-SQL 清除SQL 記憶體

網路上看到 暫記一下

DBCC FREESYSTEMCACHE ('ALL');
DBCC FREESESSIONCACHE;
DBCC FREEPROCCACHE;


連結網站

T-SQL 用TSQL查詢CPU 重的SQL語法

這可以用來查詢
常常耗費CPU的SQL語法
雖然沒有Profiler那麼多資訊
但可以不需多開Profiler耗費資源
也可以找出耗費CPU的語法加以優化


USE AdventureWorks2008R2; --改成自己的DB
GO
SELECT TOP 5 query_stats.query_hash AS "Query Hash",
SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS "Avg CPU Time",
MIN(query_stats.statement_text) AS "Statement Text"
FROM
(SELECT QS.*,
SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(ST.text)
ELSE QS.statement_end_offset END
- QS.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS QS
CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats
GROUP BY query_stats.query_hash
ORDER BY 2 DESC;
GO



參考網站

2010年8月26日 星期四