2019年10月8日 星期二

[C#]檢視文件編碼

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Threading;
using System.Text;

namespace my
{
    public partial class test : System.Web.UI.Page
    {
        private GetValue TempClass = new GetValue();
        private string TempRole = "";
        public string msg = "";


        protected void Page_Load(object sender, System.EventArgs e)
        {
          dir_seach(@"\\192.168.0.240\E_Portal1");
          dir_seach(@"D:\xml");
           
        }

        public void dir_seach(string dir)
        {
            getFile(dir);
            foreach (string f in Directory.GetDirectories(dir))
            {
                getFile(f);
                dir_seach(f);
            }

           
        }

        private void getFile(string FilePath)
        {
        string [] f = Directory.GetFiles(FilePath, "*.aspx.cs");

            foreach (string file in f)
            {
                FileInfo ff = new FileInfo(file);

           

                System.Text.Encoding x= TextEncodingType.GetFileEncodeType(file);

                string show = string.Format("{1}:{0}<br> ",x,file);
                Utility.log(show);
            }


        }

        public static class TextEncodingType
        {
            /// <summary>
            /// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
            /// </summary>
            /// <param name=“fileName“>文件路径</param>
            /// <returns>文件的编码类型</returns>
            public static System.Text.Encoding GetType(string fileName)
            {
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
               
                Encoding r = GetType(fs);
                    fs.Close();
                return r;
            }

            /// <summary>
            /// 通过给定的文件流,判断文件的编码类型
            /// </summary>
            /// <param name=“fs“>文件流</param>
            /// <returns>文件的编码类型</returns>
            public static System.Text.Encoding GetType(FileStream fs)
            {
                byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
                byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
                byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
                Encoding reVal = Encoding.Default;

                BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
                int i;
                int.TryParse(fs.Length.ToString(), out i);
                byte[] ss = r.ReadBytes(i);
                if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
                {
                    reVal = Encoding.UTF8;
                }
                else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
                {
                    reVal = Encoding.BigEndianUnicode;
                }
                else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
                {
                    reVal = Encoding.Unicode;
                }
                r.Close();
                return reVal;

            }

            /// <summary>
            /// 判断是否是不带 BOM 的 UTF8 格式
            /// </summary>
            /// <param name=“data“></param>
            /// <returns></returns>
            private static bool IsUTF8Bytes(byte[] data)
            {
                int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
                byte curByte; //当前分析的字节.
                for (int i = 0; i < data.Length; i++)
                {
                    curByte = data[i];
                    if (charByteCounter == 1)
                    {
                        if (curByte >= 0x80)
                        {
                            //判断当前
                            while (((curByte <<= 1) & 0x80) != 0)
                            {
                                charByteCounter++;
                            }
                            //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
                            if (charByteCounter == 1 || charByteCounter > 6)
                            {
                                return false;
                            }
                        }
                    }
                    else
                    {
                        //若是UTF-8 此时第一位必须为1
                        if ((curByte & 0xC0) != 0x80)
                        {
                            return false;
                        }
                        charByteCounter--;
                    }
                }
                if (charByteCounter > 1)
                {
                    throw new Exception("非预期的byte格式");
                }
                return true;
            }

            public static System.Text.Encoding GetFileEncodeType(string filename)
            {
                try
                {
                    System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                    Byte[] buffer = br.ReadBytes(2);
                    if (buffer[0] >= 0xEF)
                    {
                        if (buffer[0] == 0xEF && buffer[1] == 0xBB)
                        {
                            return System.Text.Encoding.UTF8;
                        }
                        else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
                        {
                            return System.Text.Encoding.BigEndianUnicode;
                        }
                        else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
                        {
                            return System.Text.Encoding.Unicode;
                        }
                        else
                        {
                            return System.Text.Encoding.Default;
                        }
                    }
                    else
                    {
                        return System.Text.Encoding.Default;
                    }
                }
                catch (Exception ex)
                {

                    throw new Exception(filename, ex);
                }

            }

        }
    }

}

2019年8月12日 星期一

[SQL]INSTALLSHAREDDIR參數指定目錄"c:..."無效。因為此目錄是壓縮目錄或位於壓縮目錄中,請指定一個未壓縮的目錄

因為重灌win10所以重裝資料庫,結果 從2017 裝到2014 再到2012 都是同一個問題
百思不得其解,改了機碼,刪了所有跟MS SQL相關的元還是不行
在快吐血之際……
大陸的網站
https://zhidao.baidu.com/question/1923735875421667707.html
有二個解答
第一個說是目錄被壓縮了
第二個說是目錄有空白

















我信了第二個繞了一個大圈
又看到了同一張圖片…腦中一閃…呀…

[win10]會自動幫忙壓縮檔案…當檔案夾上,有二個相對的藍色鍵頭
就表示win10自動壓縮了?????


只能說win10太雞婆了 沒事幹沒幫忙,造成問題源










2019年8月8日 星期四

[ASP.NET] [目前無法叫用中斷點,未載入這個文件的符號]

參考 http://notesfun.blogspot.com/2016/08/aspnet.html

[ASP.NET] [目前無法叫用中斷點,未載入這個文件的符號]


[目前無法叫用中斷點,未載入這個文件的符號] 我遇到的解法 有以下提示內容




1. 請使用偵錯組建組態,或停用 [啟用 Just My Code] 偵錯選項。

2. 檢查偵錯選項中的 [符號] 設定。




偵錯\選項\停用 [啟用 Just My Code] ,再重新除厝功能就正常囉。

2019年7月28日 星期日

[C#]反組譯DLL

參考https://www.bbsmax.com/A/n2d9Dl1w5D/
參考https://www.cnblogs.com/dudu/archive/2011/05/17/ildasm_ilasm_il.html

 ilasm 源文件.il /output=目标文件.dll /dll

========================= [ dll 成.il ] ======================================
工具 C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\ildasm.exe

傾印成.il後
移除不要的部份 ex:強名稱



=============================[ .il => dll]==================================
C:\Windows\Microsoft.NET\Framework\v4.0.30319>ILASM  D:\temp\dll\L\SmoothEnterprise.Web.Control.Enterprise.il /OUTPUT=D:
\temp\dll\L\SmoothEnterprise.Web.Control.Enterprise.dll


ilasm D:\temp\dll\L\SmoothEnterprise.Web.Control.Enterprise.il /output=D:\temp\dll\L\SmoothEnterprise.Web.Control.Enterprise.dll /dll  /RESOURCE=D:\temp\dll\L\SmoothEnterprise.Web.Control.Enterprise.res

2019年5月2日 星期四

[FileUpload] UpdatePanel+ FileUpload 在PostBack 後會失效

FileUpload 在上傳檔案後 在PostBack 後 就失效了,無法再上傳了
這是FileUpload 的缺點,在 https://dotblogs.com.tw/stanley14/2016/08/10/173019的文章中
發現 重新註冊trigger 可以解決 這個問題,

protected void Page_Load(object sender, EventArgs e) { Master.UpdatePanel1.Triggers.Add(new PostBackTrigger { ControlID = Button1.UniqueID }); Page.Form.Attributes.Add("enctype", "multipart/form-data"); ScriptManager.GetCurrent(Page).RegisterPostBackControl(Button1); }


//我在網頁上多個 UpdatePanel 的寫法

  foreach (Control ctl in Page.Master.FindControl("ContentPlaceHolder1").Controls)
        {
            //Utility.log(1, ctl.ID.ToString());
            if (ctl.GetType().Name == "UpdatePanel")
            {
                UpdatePanel p = (UpdatePanel)ctl;

                string name = p.ID.Replace("panel_", "btn_");
                string type = p.ID.Replace("panel_", "");

                Button btn = (Button)p.FindControl(name);

                btn.Text = getStr("upload_file");

                //因為fileUpload 在postback 後,上傳功能會鎖住 所以必需要重新註冊
                //https://dotblogs.com.tw/stanley14/2016/08/10/173019 參考這篇 重新註冊 trigger
                PostBackTrigger trigger = new PostBackTrigger();
                trigger.ControlID = btn.ID;
                p.Triggers.Add(trigger);
                ScriptManager.GetCurrent(Page).RegisterPostBackControl(btn);
//Page.Form.Attributes.Add("enctype", "multipart/form-data");經測試,這行其實不需要
                if (_attach.Rows.Count > 0)
                {
                    attach_List(type);
                }
            }
        }



2019年4月24日 星期三

[C#] Dispose 資源釋放

namespace Yield
{
     public class TsRun : IDisposable
    {
        private bool alreadydisposed = false;
        private System.Threading.Timer ThreadTimer;
         public TsRun()
        {
            if (ThreadTimer == null)
            {
                ThreadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(RFID_Read), null, 0, 1000);
            }
        }
        private void Stop()
        {
            if (ThreadTimer != null)
            {
                alreadydisposed = true;
                ThreadTimer.Dispose();
                ThreadTimer = null;
            }
        }
        #region dispose
        ~TsRun()
        {
            Dispose(false);
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!alreadydisposed)
            {
                return;
            }
            if (disposing)
            {
                //释放托管资源
                //託管資源:由CLR管理分配和釋放的資源,即由CLR裡new出來的物件;
                Stop();
            }
            //释放非托管资源
            alreadydisposed = true;
        }
        #endregion dispose
    }
}
參考: Effective C# — .NET資源管理

2019年2月14日 星期四

[asp.net]無效的回傳或回呼引數。已在組態中使用 ....

參考 KB-About Event Validation of ASP.NET 2.0

因在asp.net 2.0中使用ajax來更新網頁元件中的項目(DropDownList 的清單)
引發enableEventValidation驗證,

在黑大文中所述,因為網頁元件的資料非該網頁原生,導致資安疑慮,而讓asp.net中止網頁執行。

我因為要做多階式動態表單,DropDownList的資料由ajax更新,而引enableEventValidation驗證,為了解決這個問題 ,我將DropDownList 所屬的所有資料,在第一次進入網頁中,就載入網頁再使用ajax 去更新DropDownList 的資料,即可。