關於我

我的相片
用心思考、保持熱情,把工作上的經驗作分享與紀錄。希望能夠跟大家一起不斷的成長~

StopSubdirsMonitor

    public static void StopSubdirsMoniter()
    {
      PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
      object o = p.GetValue(null, null);
      FieldInfo f = o.GetType().GetField("_dirMonSubdirs", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
      object monitor = f.GetValue(o);
      MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic);
      m.Invoke(monitor, new object[] {});
    }

將數個檔案依指定名稱zip後,response回page讓client下載



protected void BtnSave_Click(object sender, EventArgs e)
{
MemoryStream ms = FEDIClientFacade.FEDISetupToExcel(this, DropDownList1.SelectedValue);
string txtFEDIName = "金融EDI交易量統計表" + (DateTime.Now.Year - 1911).ToString("000") + DateTime.Now.Month.ToString("00") + ".xls";

MemoryStream msCust = FEDIClientFacade.FEDISetupCustomerToExcel(this, DropDownList1.SelectedValue);
string txtFEDINameCust = "金融EDI客戶統計表" + (DateTime.Now.Year - 1911).ToString("000") + DateTime.Now.Month.ToString("00") + ".xls";

bool isSuccess = Utility.DoStreamZipPopUpDownload(this, new Stream[2] { ms, msCust },
new string[] { txtFEDIName, txtFEDINameCust }, "金融EDI統計表.zip");
if (!isSuccess)
ClientScript.RegisterStartupScript(this.GetType(), "NG", "<script>alert('報表產生失敗!')</script>");
}

//-----------------------------------------------------------------------------//

///
/// 將Stream依指定路徑與檔名存檔
///

/// Stream
/// 檔案完整路徑與檔名
public static void SaveStreamToFile(Stream stream, string filePathName)
{
string nPath = filePathName.Substring(0, filePathName.IndexOf(filePathName.Split('\\')[filePathName.Split('\\').Length - 1]));
if (!Directory.Exists(nPath))
Directory.CreateDirectory(nPath);
System.IO.File.WriteAllBytes(filePathName, StreamToByteArray(stream));
}

///
/// 將Stream轉為Byte[]
///

/// Stream
/// Byte[]
public static byte[] StreamToByteArray(Stream stream)
{
long FileSize = stream.Length;
byte[] Buffer = new byte[FileSize];
stream.Read(Buffer, 0, (int)FileSize);
stream.Close();
return Buffer;
}


///
/// 將數個檔案依指定名稱zip後,response回page讓client下載
///

/// popup之page
/// 各檔案完整路徑+名稱
/// zip檔案之名稱
///
public static bool DoZipPopUpDownload(System.Web.UI.Page page, string[] files, string zipname)
{
try
{
//建立暫存資料夾
string zipTempPath = "~/zipTemp/" + new Random().Next().ToString() + "/";
string zipTempPathOrg = zipTempPath + "org/";
if (!Directory.Exists(page.Server.MapPath(zipTempPathOrg)))
Directory.CreateDirectory(page.Server.MapPath(zipTempPathOrg));

//將檔案複製至暫存資料夾
foreach (string file in files)
{
string filename = file.Split('\\')[file.Split('\\').Length - 1];
//string filename = file.Substring(0, file.IndexOf(file.Split('\\')[file.Split('\\').Length - 1]));
File.Copy(file, page.Server.MapPath(zipTempPathOrg + filename));
}

//建立zip壓縮檔
ICSharpCode.SharpZipLib.Zip.FastZip fz = new ICSharpCode.SharpZipLib.Zip.FastZip();
fz.CreateZip(page.Server.MapPath(zipTempPath + zipname), page.Server.MapPath(zipTempPathOrg), true, "");

//Response下載zip檔
byte[] zip = File.ReadAllBytes(page.Server.MapPath(zipTempPath + zipname));
page.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", page.Server.UrlPathEncode(zipname)));
page.Response.BinaryWrite(zip);

//清空檔案與暫存檔
Directory.Delete(page.Server.MapPath(zipTempPath), true);
return true;
}
catch
{
return false;
}
}


///
/// 將數個資料Stream依指定名稱zip後,response回page讓client下載
///

/// popup之page
/// 各檔案資料Stream
/// 對應各檔案Stream之檔案名稱
/// zip檔案之名稱
///
public static bool DoStreamZipPopUpDownload(System.Web.UI.Page page, Stream[] streams,string[] filenames, string zipname)
{
try
{
//建立暫存資料夾
string zipTempPath = "~/zipTemp/" + new Random().Next().ToString() + "/";
if (!Directory.Exists(page.Server.MapPath(zipTempPath)))
Directory.CreateDirectory(page.Server.MapPath(zipTempPath));

if (streams.Length == filenames.Length)
{
string[] files = new string[streams.Length];
//資料寫入暫存
for (int i = 0; i < streams.Length; i++)
{
SaveStreamToFile(streams[i], page.Server.MapPath(zipTempPath + filenames[i]));
files[i] = page.Server.MapPath(zipTempPath + filenames[i]);
}

//製作popup zip download
bool isSuccess = DoZipPopUpDownload(page, files, zipname);

//清空暫存
Directory.Delete(zipTempPath, true);
return isSuccess;
}
else
{
return false;
}

}
catch
{
return false;
}
}

使用單一使用者模式還原資料庫

alter database vatc4 set single_user with rollback immediate;
go;
RESTORE DATABASE [VATC4] FROM  DISK = N'D:\Tools\04Temp\20090324.bak' WITH  FILE = 1,  MOVE N'VATC4_Data' TO N'E:\DB\家樂福\VATC4.mdf',  MOVE N'VATC4_Data2' TO N'E:\DB\家樂福\VATC_1.mdf',  MOVE N'VATC4_Log' TO N'E:\DB\家樂福\VATC4.ldf',  NOUNLOAD,  REPLACE,  STATS = 10;
go;
ALTER DATABASE VATC4 SET multi_user;
go;

asp.net 1.1升級2.0後,整合其他2.0專案發生問題

asp.net1.1升級2.0後,整合其他2.0專案,發現原2.0專案之AJAX不會Work. 不管怎麼找config中設定,都確定該註冊之項目都已註冊,但AJAX就是無法正確Work. 後來終於發現,因專案是由1.1升級上來的,在web.config中發現有一項設定

就是此設定讓AJAX無法work,至msdn查詢後,才知道為什麼此設定會讓AJAX無法work,說明如下
指定 ASP.NET 應用程式的 XHTML 呈現模式。模式可以是在下表中列出的其中一個值。預設值為 Transitional (XHTML 1.0 Transitional)。
Legacy:為了一致性,會將所做的一些呈現變更還原成 v1.1 的呈現行為。
設定後,會輸出非XHTML模式,導致AJAX無法正常執行。

Sqlexception 判斷錯誤類型

use a try catch block like this: 

try
{
  ...
  ...
}
catch (SqlException ex)
{
  switch (ex.Number) 
    { 
        case 4060: // Invalid Database 
                  ....
                  break;
        case 18456: // Login Failed 
                  ....
                  break;
        case 547: // ForeignKey Violation 
                  ....
                  break;
        case 2627: // Unique Index/ Primary key Violation/ Constriant Violation 
                  ....
                  break;
        case 2601: // Unique Index/Constriant Violation 
                  ....
                  break;
        default: 
                  ....
                  break;    
       } 
}

如何強迫 .Net 應用程式輸出英文的例外訊息

System.Threading.Thread.CurrentThread.CurrentCulture = 
    new System.Globalization.CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentUICulture = 
    new System.Globalization.CultureInfo("en-US");
除了可以用在 Console Program 外,同樣的程式碼一樣也可以運用在 ASP.NET、Windows Form 或任何其他 .Net 應用程式中喔!

mssql 清除log檔

backup log 資料庫名稱 with truncate_only; 
dbcc shrinkdatabase(資料庫名稱);