//註冊表名稱 string sRegistryName = "Test.exe";//放入執行檔檔案名稱 //註冊表路徑 string sRegistryPath = ""; //判斷電腦位元數,不同位元放在不同註冊表當中 if (Environment.Is64BitOperatingSystem) {//64位元 sRegistryPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"; } else {//32位元 sRegistryPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"; } //開啟登錄檔 RegistryKey RegKey = Registry.LocalMachine.OpenSubKey(@"Software", true); //將程式名稱寫入註冊表 11000 代表 IE11 Registry.SetValue(sRegistryPath , sRegistryName , 11000, RegistryValueKind.DWord); RegKey.Close();
目前分類:程式筆記 (32)
- Apr 20 Thu 2017 15:58
[C#] WebBrowser 修改瀏覽器版本
- Feb 26 Fri 2016 16:14
C# WebBrowser 抓取原始碼
C# 抓取網頁原始碼,就可以擷取自己想要的網頁內容。 private void button1_Click(object sender, EventArgs e) { //webBrowser1連結Google網站 webBrowser1.Navigate("http://www.google.com"); //等待網站下載完成 Loading(); } //判斷網頁是否下載完成 private void Loading() { while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } } private void button8_Click(object sender, EventArgs e) { //抓取原始碼放到textBox1.text裡面 textBox1.Text = webBrowser1.Document.Body.OuterHtml.ToString(); }
程式畫面結果
- Feb 26 Fri 2016 15:59
C# WebBrowser 基礎使用(二)
webBrowser(首頁、上一頁、下一頁、重新整理、停止)
private void button1_Click(object sender, EventArgs e) { //webBrowser1連結Google網站 webBrowser1.Navigate("http://www.google.com"); //等待網站下載完成 Loading(); } //判斷網頁是否下載完成 private void Loading() { while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } } private void button2_Click(object sender, EventArgs e) { webBrowser1.GoHome();//回首頁 } private void button3_Click(object sender, EventArgs e) { webBrowser1.GoBack();//上一頁 } private void button4_Click(object sender, EventArgs e) { webBrowser1.GoForward();//下一頁 } private void button5_Click(object sender, EventArgs e) { webBrowser1.Refresh();//重新整理 } private void button6_Click(object sender, EventArgs e) { webBrowser1.Stop();//停止 } private void button7_Click(object sender, EventArgs e) { this.Close();//程式關閉 }
程式畫面結果
- Feb 26 Fri 2016 15:32
C# WebBrowser 基礎使用(一)
- Feb 26 Fri 2016 13:48
C# Excel資料轉成統計圖
C#Excel把資料轉成統計圖表,下方程式碼我都有一一註解,把需要的程式碼加進去就可以使用了!
private void button1_Click(object sender, EventArgs e) { Excel._Application myExcel = null; Excel._Workbook myBook = null; Excel._Worksheet mySheet = null; try { myExcel = new Excel.Application(); //開啟一個新的應用程式 myExcel.DisplayAlerts = false; //停用警告訊息 myBook = myExcel.Workbooks.Add(true); //新增活頁簿 mySheet = (Excel._Worksheet)myBook.Worksheets[1];//引用第一張工作表 myExcel.Visible = true; //顯示Excel程式 mySheet.Cells.Font.Name = "標楷體"; //設定Excel資料字體字型 mySheet.Cells.Font.Size = 20; //設定Excel資料字體大小 //Excel寫入資料 mySheet.Cells[1, 1] = "A"; mySheet.Cells[1, 2] = "5"; mySheet.Cells[1, 3] = "ㄅ"; mySheet.Cells[2, 1] = "B"; mySheet.Cells[2, 2] = "6"; mySheet.Cells[2, 3] = "ㄆ"; mySheet.Cells[3, 1] = "C"; mySheet.Cells[3, 2] = "7"; mySheet.Cells[3, 3] = "ㄇ"; mySheet.Cells[4, 1] = "D"; mySheet.Cells[4, 2] = "8"; mySheet.Cells[4, 3] = "ㄈ"; //在工作簿 新增一張 統計圖表,單獨放在一個分頁裡面 myBook.Charts.Add(Type.Missing, Type.Missing, 1, Type.Missing); //選擇 統計圖表 的 圖表種類 myBook.ActiveChart.ChartType = Excel.XlChartType.xlLineMarkers;//插入折線圖 //設定數據範圍 string strRange = "A1:B4"; //設定 統計圖表 的 數據範圍內容 myBook.ActiveChart.SetSourceData(mySheet.get_Range(strRange), Excel.XlRowCol.xlColumns); //將新增的統計圖表 插入到 指定位置(可以從單獨的分頁放到一個分頁裡面) myBook.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject, mySheet.Name); mySheet.Shapes.Item("Chart 1").Width = 450; //調整圖表寬度 mySheet.Shapes.Item("Chart 1").Height = 254; //調整圖表高度 mySheet.Shapes.Item("Chart 1").Top = 33; //調整圖表在分頁中的高度(上邊距) 位置 mySheet.Shapes.Item("Chart 1").Left = 240; //調整圖表在分頁中的左右(左邊距) 位置 //設定 繪圖區 的 背景顏色 myBook.ActiveChart.PlotArea.Interior.Color = ColorTranslator.ToOle(Color.LightGray); //設定 繪圖區 的 邊框線條樣式 myBook.ActiveChart.PlotArea.Border.LineStyle = Excel.XlLineStyle.xlDash; //設定 繪圖區 的 寬度 myBook.ActiveChart.PlotArea.Width = 420; //設定 繪圖區 的 高度 myBook.ActiveChart.PlotArea.Height = 230; //設定 繪圖區 在 圖表中的 高低位置(上邊距) myBook.ActiveChart.PlotArea.Top = 41; //設定 繪圖區 在 圖表中的 左右位置(左邊距) myBook.ActiveChart.PlotArea.Left = 10; //設定 繪圖區 的 x軸名稱下方 顯示y軸的 數據資料 myBook.ActiveChart.HasDataTable = false; //設定 圖表的 背景顏色__方法1 使用colorIndex(放上色彩索引) myBook.ActiveChart.ChartArea.Interior.ColorIndex = 10; //設定 圖表的 背景顏色__方法2 使用color(放入色彩名稱) myBook.ActiveChart.ChartArea.Interior.Color = ColorTranslator.ToOle(Color.LightGray); //設定 圖表的 邊框顏色__方法1 使用colorIndex(放上色彩索引) myBook.ActiveChart.ChartArea.Border.ColorIndex = 10; //設定 圖表的 邊框顏色__方法2 使用color(放入色彩名稱) myBook.ActiveChart.ChartArea.Border.Color = ColorTranslator.ToOle(Color.LightGreen); //設定 圖表的 邊框樣式 myBook.ActiveChart.ChartArea.Border.LineStyle = Excel.XlLineStyle.xlDash; //設置Legend圖例 myBook.ActiveChart.Legend.Top = 5; //設定 圖例 的 上邊距 myBook.ActiveChart.Legend.Left = 185; //設定 圖例 的 左邊距 //設定 圖例 的 背景色彩 myBook.ActiveChart.Legend.Interior.Color = ColorTranslator.ToOle(Color.LightGreen); myBook.ActiveChart.Legend.Width = 55; //設定 圖例 的 寬度 myBook.ActiveChart.Legend.Height = 20; //設定 圖例 的 高度 myBook.ActiveChart.Legend.Font.Size = 11; //設定 圖例 的 字體大小 myBook.ActiveChart.Legend.Font.Bold = true; //設定 圖例 的 字體樣式=粗體 myBook.ActiveChart.Legend.Font.Name = "細明體";//設定 圖例 的 字體字型=細明體 myBook.ActiveChart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionTop;//設訂 圖例 的 位置靠上 myBook.ActiveChart.Legend.Border.LineStyle = Excel.XlLineStyle.xlDash;//設定 圖例 的 邊框線條 //設定 圖表 x 軸 內容 //宣告 Excel.Axis xAxis = (Excel.Axis)myBook.ActiveChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary); //設定 圖表 x軸 橫向線條 線條樣式 xAxis.MajorGridlines.Border.LineStyle = Excel.XlLineStyle.xlDash; //設定 圖表 x軸 橫向線條顏色__方法1 xAxis.MajorGridlines.Border.ColorIndex = 8; //設定 圖表 x軸 橫向線條顏色__方法2 xAxis.MajorGridlines.Border.Color = ColorTranslator.ToOle(Color.LightGreen); xAxis.HasTitle = false; //設定 x軸 座標軸標題 = false(不顯示),不打就是不顯示 xAxis.MinimumScale = 1; //設定 x軸 數值 最小值 xAxis.MaximumScale = 10; //設定 x軸 數值 最大值 xAxis.TickLabels.Font.Name = "標楷體"; //設定 x軸 字體字型=標楷體 xAxis.TickLabels.Font.Size = 14; //設定 x軸 字體大小 //設定 圖表 y軸 內容 Excel.Axis yAxis = (Excel.Axis)myBook.ActiveChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary); yAxis.TickLabels.Font.Name = "標楷體"; //設定 y軸 字體字型=標楷體 yAxis.TickLabels.Font.Size = 14; //設定 y軸 字體大小 //設定 圖表 標題 顯示 = false(關閉) myBook.ActiveChart.HasTitle = false; //設定 圖表 標題 = 匯率 myBook.ActiveChart.ChartTitle.Text = "匯率"; //設定 圖表 標題 陰影 = false(關閉) myBook.ActiveChart.ChartTitle.Shadow = false; //設定 圖表 標題 邊框樣式 myBook.ActiveChart.ChartTitle.Border.LineStyle = Excel.XlLineStyle.xlDash; ////選擇統計圖表的 圖表種類=3D類型的統計圖表 Floor才可以使用 //myBook.ActiveChart.ChartType = Excel.XlChartType.xl3DColumn;//插入3D統計圖表 ////設定 圖表的 Floor顏色__方法1 使用colorIndex(放上色彩索引) //myBook.ActiveChart.Floor.Interior.ColorIndex = 1; ////設定 圖表的 Floor顏色__方法2 使用color(放入色彩名稱) //myBook.ActiveChart.Floor.Interior.Color = ColorTranslator.ToOle(Color.LightGreen); } catch (Exception) { myExcel.Visible = true; } finally { //把執行的Excel資源釋放 System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel); myExcel = null; myBook = null; mySheet = null; } }
- Feb 26 Fri 2016 13:43
C# 開啟Excel,設定框線、字體對齊
下面就是設定框線及字體對齊的語法
private void button1_Click(object sender, EventArgs e) { Excel._Application myExcel = null; Excel._Workbook myBook = null; Excel._Worksheet mySheet = null; try { myExcel = new Excel.Application(); //開啟一個新的應用程式 myExcel.DisplayAlerts = false; //停用警告訊息 myBook = myExcel.Workbooks.Add(true); //新增活頁簿 mySheet = (Excel._Worksheet)myBook.Worksheets[1];//引用第一張工作表 myExcel.Visible = true; //顯示Excel程式 mySheet.Cells.Font.Name = "標楷體"; //設定Excel資料字體字型 mySheet.Cells.Font.Size = 20; //設定Excel資料字體大小 //Excel寫入資料 mySheet.Cells[1, 1] = "1"; mySheet.Cells[1, 2] = "A"; mySheet.Cells[1, 3] = "ㄅ"; mySheet.Cells[2, 1] = "2"; mySheet.Cells[2, 2] = "B"; mySheet.Cells[2, 3] = "ㄆ"; mySheet.Cells[3, 1] = "3"; mySheet.Cells[3, 2] = "C"; mySheet.Cells[3, 3] = "ㄇ"; mySheet.Cells[4, 1] = "4"; mySheet.Cells[4, 2] = "D"; mySheet.Cells[4, 3] = "ㄈ"; //設定框線 string Range = "A1:C4"; //設定範圍 mySheet.get_Range(Range).Borders.LineStyle = Excel.XlLineStyle.xlContinuous; //設定對齊方式 //靠左 string RangeLeft = "A1:A4";//設定範圍 mySheet.get_Range(RangeLeft).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft; //靠右 string RangeRight = "B1:B4";//設定範圍 mySheet.get_Range(RangeRight).HorizontalAlignment = Excel.XlHAlign.xlHAlignRight; //置中 string RangeCenter = "C1:C4";//設定範圍 mySheet.get_Range(RangeCenter).HorizontalAlignment=Excel.XlHAlign.xlHAlignCenter; } catch (Exception) { myExcel.Visible = true; } finally { //把執行的Excel資源釋放 System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel); myExcel = null; myBook = null; mySheet = null; } }
- Feb 26 Fri 2016 13:36
C# Excel設定字型及大小並寫入資料
想要在C#程式中讓Excel寫入資料,就只要在在程式中間加入下列語法就可以了
private void button1_Click(object sender, EventArgs e) { Excel._Application myExcel = null; Excel._Workbook myBook = null; Excel._Worksheet mySheet = null; try { myExcel = new Excel.Application(); //開啟一個新的應用程式 myExcel.DisplayAlerts = false; //停用警告訊息 myBook = myExcel.Workbooks.Add(true); //新增活頁簿 mySheet = (Excel._Worksheet)myBook.Worksheets[1];//引用第一張工作表 myExcel.Visible = true; //顯示Excel程式 mySheet.Cells.Font.Name = "標楷體"; //設定Excel資料字體字型 mySheet.Cells.Font.Size = 20; //設定Excel資料字體大小 //Excel寫入資料 mySheet.Cells[1, 1] = "1"; mySheet.Cells[1, 2] = "A"; mySheet.Cells[2, 1] = "2"; mySheet.Cells[2, 2] = "B"; mySheet.Cells[3, 1] = "3"; mySheet.Cells[3, 2] = "C"; mySheet.Cells[4, 1] = "4"; mySheet.Cells[4, 2] = "D"; } catch (Exception) { myExcel.Visible = true; } finally { //把執行的Excel資源釋放 System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel); myExcel = null; myBook = null; mySheet = null; } }
Excel畫面結果
- Feb 26 Fri 2016 09:03
C# 用程式開啟Excel 試算表
- Dec 01 Tue 2015 10:53
[C#] 多執行緒
- Nov 30 Mon 2015 16:38
[C#] 輸出成Execl
- Nov 30 Mon 2015 09:26
[C#]DataTable的使用方式
//創建一個 DataTable 來存放資料庫
- Nov 27 Fri 2015 15:05
[C#] 計算程式執行時間
//last_run_min 程式開始執行 起始時間 System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Reset(); sw.Start(); //last_run_min 程式結束 結束時間 sw.Stop(); // 將時間儲存到Timing字串 string Timing = sw.Elapsed.TotalMinutes.ToString("0.00"); //輸出到TextBox1.text 顯示 textBox1.Text = Timing;
- Nov 03 Tue 2015 14:15
[C#] 傳入參數自定義 程式啟動模式
- Nov 02 Mon 2015 16:04
[C#] DataGirdView 轉 DataTable
//把DataGridView1 轉 DataTable DataTable dt = (DataTable)DataGridView1.DataSource;
- Sep 30 Wed 2015 17:01
[C#] WinRAR 壓縮
Process Process1 = new Process(); string RarFileName = @"C:\Users\jwwu\Desktop\安裝檔"; //要壓縮成什麼檔案名稱 string CompressSource = @"C:\Users\jwwu\Desktop\安裝檔"; //壓縮來源 Process1.StartInfo.FileName = "WinRAR.exe"; Process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; Process1.StartInfo.Arguments = string.Format("a -ep1 {0} {1}", RarFileName, CompressSource); Process1.Start();
- Sep 16 Wed 2015 16:29
[C#] WebBrowser抓取所有超連結,使用WebClient DownloadFile
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; //加入下列物件 using System.Net; using mshtml; namespace WebBrowser_Download { public partial class Form1 : Form { WebBrowser webUrl = new WebBrowser(); public Form1() { InitializeComponent(); this.btnAuto.Click += new EventHandler(btn_Click); this.btnManual.Click += new EventHandler(btn_Click); DownloadLink.Text = "www.yahoo.com.tw"; this.listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_MouseDoubleClick); webUrl.ScriptErrorsSuppressed = true; } public void loading() { while (!(webUrl.ReadyState == WebBrowserReadyState.Complete)) { //在網頁讀取的時候可以繼續其他事件 Application.DoEvents(); } } public void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) { //點選ListBox選項下載檔案 webUrl.Navigate(listBox1.SelectedItem.ToString()); } public void btn_Click(object sender, EventArgs e) { //連結到網站 webUrl.Navigate(DownloadLink.Text); //等待網頁下載完成 loading(); //使用HTMLDocument語法 HTMLDocument doc = webUrl.Document.Body.Document.DomDocument as HTMLDocument; //計算檔案次數 int CountA = 0; WebClient wc = new WebClient(); //暫存Url string strUrl = ""; //儲存路徑 string receivePath = @"C:\Users\jwwu\Desktop\WebBrowser_Download\DownLoad\"; progressBar1.Minimum = 0; //自動下載btn if (sender.Equals(btnAuto)) { //逐筆抓出有".doc"的網址 foreach (IHTMLElement doclink in doc.links) { strUrl = doclink.toString(); //Url文字包含".doc" if (strUrl.Contains(".doc")) { CountA++; progressBar1.Maximum=CountA; progressBar1.Value += 1; wc.DownloadFile(strUrl, receivePath + System.IO.Path.GetFileName(strUrl)); } } if (CountA == 0) { MessageBox.Show("沒有word檔案可供下載", "下載檔案", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("總共" + CountA + "筆", "下載檔案", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else if (sender.Equals(btnManual))//手動下載 { foreach (IHTMLElement doclink in doc.links) { strUrl = doclink.toString(); if (strUrl.Contains(".doc")) { CountA++; listBox1.Items.Add(strUrl); } } if (CountA == 0) { MessageBox.Show("沒有word檔案可供下載", "下載檔案", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } }
- Sep 16 Wed 2015 15:50
[C#] Stream DownLoad
// Stream DownLoad 直接下載檔案
- Sep 16 Wed 2015 14:22
[C#] 更改檔案名稱
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; //自行宣告 namespace WindowsFormsApplication4 { public partial class Form1 : Form { //宣告資料夾對話方塊類別 FolderBrowserDialog Fbd = new FolderBrowserDialog(); public Form1() { InitializeComponent(); textPath.Text = @"C:\Users\jwwu\Desktop\委任狀\"; this.btnS.Click += new EventHandler(Btn_Click); this.btnBr.Click += new EventHandler(Btn_Click); this.btnE.Click += new EventHandler(Btn_Click); this.label4.Text = ""; } public void Btn_Click(object sender, EventArgs e) { if (sender.Equals(btnE)) { this.Close(); } else if (sender.Equals(btnBr)) { //初始位置設定 Fbd.RootFolder = Environment.SpecialFolder.Desktop; //開啟方塊 Fbd.ShowDialog(); //取得選取資料夾路徑 textPath.Text = Fbd.SelectedPath; } else if (sender.Equals(btnS)) { if (!Directory.Exists(textPath.Text)) { MessageBox.Show("請輸入正確路徑", "瀏覽路徑"); return; } //找出目錄底下所有檔案,以及子目錄底下所有檔案 string[] filepath = System.IO.Directory.GetFiles(textPath.Text, "*", System.IO.SearchOption.AllDirectories); //設定初始值 this.label4.Text = ""; //設定初始值=0 this.progressBar1.Value = 0; //數值最小=0 this.progressBar1.Minimum = 0; //數值最大=總共檔案數目 this.progressBar1.Maximum = filepath.Length; this.progressBar1.Step = 1; foreach (var filename in filepath) { //從路徑抓出檔案名稱 string strname = filename.Substring(filename.LastIndexOf("\\") + 1); //從路徑抓出目錄路徑 string strname1 = filename.Substring(0, filename.LastIndexOf("\\")); string strSpace = ""; //判斷字串中是否有空白部分 if (strname.Contains(" ")) { //使用Replace轉換字串 strSpace = strname.Replace(" ", "_"); } else { //沒有空白則為原來檔案名稱 strSpace = strname; } //拆解檔案名稱,分出 檔名 及 副檔名 string strMa = ""; string strSe = ""; //檔名在副檔名"."之前 strMa = strSpace.Substring(0, strSpace.LastIndexOf(".")).ToString(); //副檔名在"."之後包括"."
strSe = strSpace.Substring(strSpace.LastIndexOf(".")).ToString(); string strDel = ""; //當檔案名稱裡面有"."的時候 while (strMa.Contains(".")) { //index從零開始,總長度-1= index if ((strMa.LastIndexOf(".")) == (strMa.Length - 1)) { //檔案索引與"."相同,減去一個字串 strDel = strMa.Substring(0, strMa.Length - 1).ToString(); strMa = strDel; } else { break; } } //foreach一個檔案就+1 progressBar1.Value += 1; //使用File.Move移動檔案,也可以更新檔案名稱 File.Move(filename, strname1 + "\\" + strMa + strSe);