記錄開發中遇到的和別人問的較多的問題....
目錄
IE10中LinkButton不可用
這應該是2013年初遇到的一個BUG,當使用Asp.Net開發Web Application時,頁面使用LinkButton按鈕;運行頁面用IE10打開,點擊LinkButton按鈕出現如下圖錯誤
沒有直接彈出如下圖錯誤,可以運行IE10開發人員工具(F12),在控制台中查看,也會輸出 “__doPostBack”未定義 錯誤信息
解決方法及參考:
下載並在服務器上安裝相應補丁程序即可,下載地址:
.NET Framework 2.0 : http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2600100&kbln=zh-cn
.NET Framework 4.0 : http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2600088&kbln=zh-cn
參考來自:
如何配置IIS才能通過鏈接下載文件
如果不進行任何的配置,通過url直接下載一個doc類型文件(例如:http://www.xxx.com/1.doc),頁面會返回如下錯誤:
HTTP 錯誤 404.3 - Not Found
由於擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程序。如果應下載文件,請添加 MIME 映射。
解決方法:
以IIS7.5為例
1.在IIS中找到相應的網站
2.在“功能視圖”中找到“MIME”類型,雙擊進入
3.添加,在“文件擴展名”內填入相應的擴展名,比如:.doc
4.在“MIME類型(M)”內填入相應的MIME類型,比如doc文件的MIME:application/msword
5.提交
操作完成后再次點擊url,瀏覽器會填出下載窗口!
不清楚相應類型文件對應的MIME類型可以在此網址查找:
測試發現應該不用每種文件類型指定確切的MIME類型,application/octet-stream 適用於多數文件類型...
如何配置IIS通過鏈接是下載而不是直接打開txt/圖片類型文件
當正確配置MIME類型后,下載文件大部分都可以成功,但是如txt或者是一些圖片格式的文件,瀏覽器不會填出下載窗口,而是會在當前頁打開並顯示其內容,如果需要直接下載這些類型的文件,還需要其它的IIS配置:
測試只適用於IE!
解決方法:
以IIS7.5為例
1.在IIS中找到相應的網站
2.在“功能視圖”中找到“HTTP 響應頭”類型,雙擊進入
3.添加,“名稱”內填入:Content-Disposition
4.在“值”內填入:attachment
5.提交
操作完成后,打開IE再次點擊url,瀏覽器會填出下載窗口!
C# 給虛擬目錄批量添加MIME示例

using System.DirectoryServices; //添加導出COM組件:Active DS IIS Namespace Provider static void Main(string[] args) { Console.WriteLine("正在添加已有的MIME類型..."); DirectoryEntry virDir_IKeyMgmt = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT/虛擬目錄名"); try { PropertyValueCollection mimeCollection = (PropertyValueCollection)virDir_IKeyMgmt.Properties["MimeMap"]; //獲取MIME類型 string[] extArr = new string[] {".tar", ".zip", ".gz", ".gds", ".v", ".sof", ".bit", ".stp", ".edf", ".sdc", ".ucf", ".fsdb", ".vcd", ".sess", ".rc", ".result", ".jpg", ".png", ".pdf", ".doc", ".txt", ".log", ".xrc", ".lvs", ".xls", ".elf", ".tr0"}; IISOle.MimeMapClass mimeType = null; foreach (string ext in extArr) { mimeType = new IISOle.MimeMapClass(); mimeType.Extension = ext; mimeType.MimeType = "application/octet-stream"; mimeCollection.Add(mimeType); } virDir_IKeyMgmt.CommitChanges();//更改目錄 Console.WriteLine("添加成功!"); } catch (Exception ex) { Console.WriteLine("添加失敗!"); Console.WriteLine("錯誤信息:" + ex.ToString()); } finally { Console.ReadLine(); } }
C# 獲取虛擬目錄的物理路徑示例

/// <summary> /// 獲取虛擬目錄的物理路徑 /// </summary> /// <param name="identifier">虛擬目錄所屬網站的標識符</param> /// <param name="name">虛擬目錄名稱</param> /// <returns></returns> private string GetWebVirtualDirectoryPath(string identifier, string name) { DirectoryEntry de = new DirectoryEntry("IIS://LOCALHOST/W3SVC/" + identifier + "/ROOT/" + name); string path = (string)de.Properties["Path"].Value; return path; } /// <summary>???????? /// 獲取網站的標識符 /// </summary> /// <param name="portNumber">端口號</param> ///<returns></returns> private string GetWebSiteIdentifier(string portNumber) { DirectoryEntry root = new DirectoryEntry("IIS://LOCALHOST/W3SVC"); foreach (DirectoryEntry e in root.Children) { if (e.SchemaClassName == "IIsWebServer") { foreach (object property in e.Properties["ServerBindings"]) { if (property.Equals(":" + portNumber + ":")) { return e.Name; } } } } //?默認為“默認網站”的標識符 return "1"; }
The name 'ScriptManager' does not exist in the current context
錯誤如下圖:
解決方式:
1.從.aspx頁移除ScriptManager控件
2.頁面的.cs文件添加using System.Web.UI;
3.再把ScriptManager控件添加到.aspx頁
4.重新生成
System.InvalidOperationException: 未在本地計算機上注冊“Microsoft.ACE.OLEDB.12.0”提供程序 Office 2010
可能出現此問題的情況有很多
1.連接字符串沒有配置正確,可對照相應的版本 http://www.connectionstrings.com/excel/
2.需要安裝Microsoft Access 2010 數據庫引擎可再發行程序包 http://www.microsoft.com/en-us/download/confirmation.aspx?id=23734
3.如果以上兩種情況都解決不了,可以試着修改項目的項目PlatForm為x86
Unable to find manifest signing certificate in the certificate store"
解決方法:
以VS2008為例
1.VS Solution Explorer中選擇項目
2.右鍵,點擊進入“properties”,選中“Signing”標簽頁
3.找到“Sign the ClickOnce manifests”選擇,去掉勾選並保存。
配置Session為StateServer的方式
配置方法:
1.修改注冊表項
找到 HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/aspnet_state/Parameters
把AllowRemoteConnection設為1
2.啟動ASP.NET State Service服務
把ASP.NET State Service服務設為自動,並且啟動它
如果對Session狀態的配置方法有疑問可以看看MSDN :
http://msdn.microsoft.com/zh-cn/library/h6bb9cz9(v=VS.85).aspx
如果對Session有哪幾種保存方式、分別有什么優劣這些都不是很了解,自已去看看MSDN 看看這篇文章,相信會對Session有更進一步的理解:
http://blog.csdn.net/cityhunter172/article/details/727743
C# 用WMI獲取網卡MAC地址示例代碼

using System.Management; //添加System。Manageement引用 static void Main(string[] args) { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { WriteLog(mo["MacAddress"].ToString()); } } Console.ReadLine(); }
使用Net User命名創建帳戶,如何讓密碼永不過期的問題
程序通過調用net user命令創建用戶,但是此命令未提供設置密碼永不過期的參數,如果想設置密碼永不過期,可以通過調用第三方Netuser.exe來完成。
下載地址: http://files.cnblogs.com/zhongweiv/NetUser.zip
將 netuser.exe 拷貝到 %systemroot%\system32 下。
Examples:
<Setting >:
/name: set a new name
/pwnexp:{y|n} set 'password never expires'
1. 更改用戶名
netuser Administrator /name:"Admin go"
更改Administrator名字為 Admin go
netuser "John Doe" /name:DoeJ
更改John Doe名字為DoeJ
2.是用戶用不過期
netuser admin /pwnexp:y
使admin 用戶永不過期
net user這些操作可以通過 Network Management Functions 的User相關的方法去實現!
C# 得到文件頭信息示例代碼

static void Main(string[] args) { string filePath = @"文件絕對路徑"; System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader r = new System.IO.BinaryReader(fs); string bx = ""; byte buffer; try { buffer = r.ReadByte(); bx = buffer.ToString(); buffer = r.ReadByte(); bx += buffer.ToString(); } catch (Exception exc) { Console.WriteLine(exc.Message); } r.Close(); fs.Close(); Console.WriteLine(bx); Console.ReadLine(); }
文件頭並不是確定文件類型的准確標准,但確實能判斷出一些文件,本示例其實也不能叫得到文件頭的信息,只是讀取了文件的前兩個字節,如果作為判斷文件的嚴謹依據,還是要根據具體文件去進去格式分析!
在應用程序級別之外使用注冊為 allowDefinition='MachineToApplication' 的節是錯誤的。如果在 IIS 中沒有將虛擬目錄配置為應用程序
以IIS6為例
解決方式:
1.在IIS中網站對應的虛擬目錄上右鍵,選屬性
2.應用程序名后點創建
C# 利用SharpZipLib對字符串進行壓縮
下載地址
https://github.com/icsharpcode/SharpZipLib

#region## 壓縮字符串 /// <summary> /// 功能:壓縮字符串 /// 創建人:Wilson /// 創建時間:2012-12-27 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string Compress(string str) { byte[] temp = System.Text.Encoding.Unicode.GetBytes(str); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); System.IO.Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(memStream); stream.Write(temp, 0, temp.Length); stream.Close(); byte[] compressedData = (byte[])memStream.ToArray(); return System.Convert.ToBase64String(compressedData, 0, compressedData.Length); } #endregion #region## 解壓縮字符串 /// <summary> /// 功能:解壓縮字符串 /// 創建人:Wilson /// 創建時間:2012-12-27 /// </summary> /// <param name="compressedStr"></param> /// <returns></returns> public static string UnCompress(string compressedStr) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); int totalLen = 0; byte[] temp = System.Convert.FromBase64String(compressedStr); ; byte[] writeTemp = new byte[4096]; System.IO.Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new System.IO.MemoryStream(temp)); while (true) { int size = stream.Read(writeTemp, 0, writeTemp.Length); if (size > 0) { totalLen += size; sb.Append(System.Text.Encoding.Unicode.GetString(writeTemp, 0, size)); } else { break; } } stream.Close(); return sb.ToString(); } #endregion
Assembly.Load (Byte[])方法 調用內存占用一直增大的問題
問題表現:動態調用WebSerivce時,因為反復調用使用了Assembly.Load (Byte[]),導致進程內存不斷升高
解決方法:
//方法外聲明 private static byte[] filedata = null; private static Assembly asm = null; //方法中使用 if (filedata == null) { filedata = File.ReadAllBytes(DLL_NAME); } if (asm == null) { asm = Assembly.Load(filedata); }
IIS7/7.5配置上傳大文件
在IIS7/7.5中要上傳在文件,不僅需要配置
<httpRuntime executionTimeout="3600" maxRequestLength="2097151"/>
還需要配置如下requestLimits節點
<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="2147483647"/> </requestFiltering> </security> </system.webServer>
PS:
executionTimeout單位:秒
maxRequestLength單位:KB
maxAllowedContentLength:bytes
maxRequestLength 表示Asp.Net允許上傳的大小 默認值:4096KB 最大值:2097151KB(2G-1K)
executionTimeout 表示允許執行的最大時間 默認值是90秒 (超時只有在compilation 節點設置為時才會生效)
httpRuntime 元素(ASP.NET 設置架構)
http://msdn.microsoft.com/zh-cn/library/e1f13641(v=vs.90).aspx
system.webServer節點是IIS7引入的
不要輕易修改上傳限制,以防上傳大文件攻擊服務器!
項目發布在IIS中圖片或CSS樣式無法正常顯示
很多時候在開發環境中頁面能正常顯示,但發布在IIS中后,顯示正常
最常見原因:
1.路徑不對正常
特別是發布為虛擬目錄時,一定要注意路徑問題
2.IIS安裝沒有勾選“靜態內容“選項
多數這種原因比較多,打開IIS配置窗口
Internet Information Services(Internet 信息服務)-->World Wild Web(萬維網服務)-->Common HTTP features(常見HTTP功能)-->選中staticcontent(靜態內容)
重新刷新頁面即可
Web.config文件中配置修改查詢超時時間
進行大數據查詢或者統計數據時,常出現查詢超時,通過配置Web.config連接字符串可以解決(MySQL)
Server=211.136.8.81;Port=3306;Database=lf;Uid=root;Pwd=admin123;CharSet=utf8;Pooling=True;default command timeout=3600;Connection Timeout=3600;
default command timeout和Connection Timeout從字面上就很好理解,就不解釋了
友情提示:3600這個值只是示例,具體還是要配置一個相對合理的時間,資源寶貴!