本篇利用 PropertyItem 讀取 圖片 內的 EXIF 資訊,此功能為 .Net 內建方式,不須匯入任何套件。 讀取 EXIF 資訊範例:
Image image = Image.FromFile("圖片路徑");
foreach (PropertyItem p in image.PropertyItems)
{
Console.WriteLine(p.Id);
Console.WriteLine(p.Type);
Console.WriteLine(p.Len);
Console.WriteLine(p.Value);
}
image.Dispose();複製 EXIF 資訊 a 檔案到 b 檔案:
Image image_F = Image.FromFile("a.jpg");
Image image_T = Image.FromFile("b.jpg");
foreach (PropertyItem p in image_F.PropertyItems)
{
image_T.SetPropertyItem(p);
}
image_T.Save("b.jpg");
image_T.Dispose();
image_F.Dispose();本篇利用 ExifLibrary 讀取 圖片 內的 EXIF 資訊。
官方網站下載:http://www.codeproject.com/Articles/43665/ExifLibrary-for-NET
本地下載:ExifLibrary
嘗試了很多方式想要清除 Temporary Internet Files,但因為安全性無法列出所有檔案,所以無法清除!
其實只需利用 RunDll32.exe 即可達成刪除 快取 及 Cookie。
Temporary Internet Files
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
Cookies
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
History
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
Form Data
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
Passwords
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
Delete All
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255
Delete All – “Also delete files and settings stored by add-ons”
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351
網頁中常常會出現明碼的信箱位址,時常會被機器人爬取,做為廣告發送目標。
本篇我利用 .Net 產生 JavaScript,防止機器人直接抓取原始碼中的電子信箱位址。
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace Test
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(MailToJS("[email protected]"));
}
/// <summary>
/// 產生指定範圍的亂數
/// </summary>
/// <param name="Min"></param>
/// <param name="Max"></param>
/// <returns></returns>
public int RandomNext(int Min, int Max)
{
RNGCryptoServiceProvider rngp = new RNGCryptoServiceProvider();
byte[] rb = new byte[4];
rngp.GetBytes(rb);
int value = BitConverter.ToInt32(rb, 0);
value = (value % (Max - Min + 1));
if (value < 0) value = -value;
value += Min;
return value;
}
/// <summary>
/// 將信箱轉為JS
/// </summary>
/// <param name="Mail"></param>
/// <returns></returns>
protected string MailToJS(string Mail)
{
List<string> CList = new List<string>();
for (int i = 32; i <= 64; i++) CList.Add(Convert.ToChar(i).ToString());
for (int i = 91; i <= 126; i++) CList.Add(Convert.ToChar(i).ToString());
List<string> NList = new List<string>();
while (CList.Count > 0)
{
int R = RandomNext(0, CList.Count - 1);
NList.Add(CList[R]);
CList.RemoveAt(R);
}
List<int> EList = new List<int>();
Mail = Mail.ToLower();
Mail = "<a href=\"mailto:" + Mail + "\" hidefocus=\"true\">" + Mail + "</a>";
foreach (char c in Mail) EList.Add(NList.IndexOf(c.ToString()));
for (int i = 0; i < NList.Count; i++)
{
if (NList[i] == "'")
NList[i] = "'" + NList[i].Replace("'", "\\'") + "'";
else if (NList[i] == "\\")
NList[i] = "'" + NList[i].Replace("\\", "\\\\") + "'";
else
NList[i] = "'" + NList[i] + "'";
}
StringBuilder SB = new StringBuilder();
SB.AppendLine("<script type=\"text/javascript\">");
SB.AppendLine("<!--");
SB.AppendLine("\t(function(){");
SB.AppendLine("\t\tvar ek = [" + String.Join(",", NList.ToArray()) + "];");
SB.AppendLine("\t\tvar en = [" + String.Join(",", EList.ToArray()) + "];");
SB.AppendLine("\t\tvar ds = \"\";");
SB.AppendLine("\t\tfor(var i in en) ds += ek[en[i]];");
SB.AppendLine("\t\tdocument.write(ds);");
SB.AppendLine("\t})();");
SB.AppendLine("// -->");
SB.AppendLine("</script>");
return SB.ToString();
}
}
}因為修改 Web.config、Bin及回收都會造成 Session 遺失,只需開起 ASP.NET State Service 即可解決此問題。
到控制台 設定 > 服務 > ASP.NET State Service :
利用 NPOI 指定欄位儲存格格式:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("工作表名稱");
HSSFRow Row = (HSSFRow)sheet.CreateRow(0);
//欄位為數值 注意不可為字串
Row.CreateCell(0).SetCellValue(Convert.ToDouble("123"));
HSSFCellStyle cs = (HSSFCellStyle)workbook.CreateCellStyle();
//Format格式為數字
cs.DataFormat = HSSFDataFormat.GetBuiltinFormat("0");
//Format格式為百分比小數兩位
cs.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
Row.GetCell(0).CellStyle = cs;利用 NPOI 公式指令:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("工作表名稱");
HSSFRow Row = (HSSFRow)sheet.CreateRow(0);
Row.CreateCell(0).CellFormula = "SUM(A1:B1)";
Row.CreateCell(1).CellFormula = "A1-B1";
//更新有公式的欄位
sheet.ForceFormulaRecalculation = true;利用 NPOI 變更字體尺寸及樣式:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("工作表名稱");
HSSFRow Row = (HSSFRow)sheet.CreateRow(0);
Row.CreateCell(0).SetCellValue("測試文字");
HSSFCellStyle cs = (HSSFCellStyle)workbook.CreateCellStyle();
//啟動多行文字
cs.WrapText = true;
//文字置中
cs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER;
cs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
//框線樣式及顏色
cs.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOUBLE;
cs.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
cs.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
cs.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
cs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
cs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
cs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
cs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
//背景顏色
cs.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_25_PERCENT.index;
cs.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;
HSSFFont font1 = (HSSFFont)workbook.CreateFont();
//字體顏色
font1.Color = NPOI.HSSF.Util.HSSFColor.DARK_BLUE.index;
//字體粗體
font1.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
//字體尺寸
font1.FontHeightInPoints = 25;
cs.SetFont(font1);
Row.GetCell(0).CellStyle = cs;
//欄位寬度
sheet.SetColumnWidth(column.Ordinal, 5000);利用 NPOI 即可將 DataSet 匯出到 Excel 檔案。
官方網站:http://npoi.codeplex.com/
本地DLL下載:NPOI v1.25 .NET3.5 (已修改版可以超過4000條style限制)
程式碼範例:
DataSet DS = new DataSet();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("工作表名稱");
//顯示 Table 0 的所有欄位名稱
HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
foreach (DataColumn column in DS.Tables[0].Columns)
{
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
}
//顯示 所有資料列
int rowIndex = 1;
foreach (DataRow row in DS.Tables[0].Rows)
{
HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
foreach (DataColumn column in DS.Tables[0].Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}
dataRow = null;
rowIndex++;
}
Response.Clear();
// 產生 Excel 資料流
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
headerRow = null;
sheet = null;
workbook = null;
// 設定強制下載標頭
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=Download.xls"));
// 輸出檔案
Response.BinaryWrite(ms.ToArray());
ms.Close();
ms.Dispose();
Response.End();利用 .Net 抓取電腦的網路流量,程式碼如下:
using System;
using System.Net.NetworkInformation;
namespace Test
{
class Program
{
static void Main(string[] args)
{
while (true)
{
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics();
Console.Clear();
Console.WriteLine("接收封包數:" + ipstat.ReceivedPackets);
Console.WriteLine("轉發封包數:" + ipstat.ReceivedPacketsForwarded);
Console.WriteLine("傳送封包數:" + ipstat.ReceivedPacketsDelivered);
Console.WriteLine("丟棄封包數:" + ipstat.ReceivedPacketsDiscarded);
System.Threading.Thread.Sleep(1000);
}
}
}
}