tags: ProgrammingLanguage

C#

ๆŽ’้™ค้‡่ฆ†่ณ‡ๆ–™

Hashtable ht = new Hashtable();

if (!ht.Contains(_selstchf.Table.Rows[i]["rank"].ToString())) //ๆŽ’้™ค้‡่ค‡่ณ‡ๆ–™
{
rankCount++;
ht.Add(_selstchf.Table.Rows[i]["rank"].ToString(), _selstchf.Table.Rows[i]["rank"].ToString());
}


 #region ๅˆ†ๅ‰ฒPDFๆช”
                                if (format == "pdfDownload")
                                {
                                    //string path = @"D:\Users\LIU\Source\Workspaces\FR_ReportServer\ReportNet\FastReportCore.MVC\wwwroot\temp";
                                    //็•ถๅ‰ๅฐˆๆกˆ่ทฏๅพ‘ 
                                    string prjPath = Directory.GetCurrentDirectory();
                                    //ๆœๅฐ‹wwwroot่ทฏๅพ‘
                                    string dir = Directory.GetDirectories(prjPath, "wwwroot", SearchOption.TopDirectoryOnly)[0] + "\\temp";
                                    //ๅญ˜ๆ”พๅˆ†ๅ‰ฒPDF่ณ‡ๆ–™ๅคพ่ทฏๅพ‘
                                    string spilt_PDF_dir = Directory.GetDirectories(prjPath, "wwwroot", SearchOption.TopDirectoryOnly)[0] + "\\SpiltPDF";
                                    //ๅญ˜ๆ”พzip่ณ‡ๆ–™ๅคพ
                                    string zip_dir = Directory.GetDirectories(prjPath, "wwwroot", SearchOption.TopDirectoryOnly)[0] + "\\zip";
                             
                                    if (!Directory.Exists(spilt_PDF_dir))
                                        Directory.CreateDirectory(spilt_PDF_dir);

                                    DirectoryInfo directoryInfo = new DirectoryInfo(dir);                                 
                                    string currentPDF = tempFile;
                                     
                                    if (Directory.Exists(spilt_PDF_dir))
                                    {     
                                        PdfReader reader = null;
                                        string sourcePdfPath = dir + "\\" + currentPDF; //่ฆๅˆ†ๅ‰ฒ็š„pdfๆช”
                                        try
                                        {
                                            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourcePdfPath);
                                            string outputPdfFolder = spilt_PDF_dir; //ๅŒฏๅ‡บ่ทฏๅพ‘
                                            reader = new PdfReader(sourcePdfPath);

                                            if(reader.NumberOfPages > 0)
                                            {
                                                for (int i = 1; i <= reader.NumberOfPages; i++)
                                                {
                                                    //่ฎ€ๅ–PDFๆช”ๅ…งๅฎนไธฆๆ“ทๅ–ๅญธ่™Ÿ็•ถๆช”ๆกˆๅ็จฑ
                                                    var pdfText = PdfTextExtractor.GetTextFromPage(reader, i, new LocationTextExtractionStrategy());

                                                    string strKey = "ๅญธ       ่™Ÿ๏ผš"; //Regex่ฆๅ‰‡
                                                    int keyIndex  = 0;
                                                    string str   = "";
                                                    string stuno = "";

                                                    MatchCollection matches = Regex.Matches(pdfText, strKey);

                                                    foreach (Match m in matches)                                                     
                                                        keyIndex = m.Index + m.Length;
                                                    
                                                    str = pdfText.Substring(keyIndex, 10).Trim();
                                                    string[] stunoArr = str.Split(' ');
                                                    stuno = stunoArr[0];

                                                    PdfCopy pdfCopyProvider      = null;
                                                    PdfImportedPage importedPage = null;
                                                    Document sourceDocument      = null;
                                                    string outputPdfPath = outputPdfFolder + "\\" + stuno  + ".pdf"; 
                                                    sourceDocument  = new Document(reader.GetPageSizeWithRotation(i)); 
                                                    pdfCopyProvider = new PdfCopy(sourceDocument, new FileStream(outputPdfPath, FileMode.Create));
                                                    sourceDocument.Open();
                                                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                                                    pdfCopyProvider.AddPage(importedPage);
                                                    sourceDocument.Close();
                                                }
                                                reader.Close();

                                                //ๅŒฏๅ‡บzipๆช”
                                                //ๅญ˜ๆ”พๅˆ†ๅ‰ฒPDF่ณ‡ๆ–™ๅคพ่ทฏๅพ‘                                                
                                                if(!Directory.Exists(zip_dir))
                                                    Directory.CreateDirectory(zip_dir);

                                                string startPath = outputPdfFolder;
                                                string zipPath = zip_dir + "\\" + strReport + "_" + g.ToString().Replace("-", "") + ".zip";                                                
                                                ZipFile.CreateFromDirectory(startPath, zipPath);

                                                //ๅŒฏๅ‡บZIPๆช”ๅพŒๅˆช้™คๆ‰€ๆœ‰ๆช”ๆกˆ
                                                new PublicFunction().DeleteDirectoryFile(outputPdfFolder);                                             
                                            }
                                        }
                                        catch (Exception ex) 
                                        { 
                                            throw ex; 
                                        }
                                    }
                                    return File("/zip/" + strReport + "_" + g.ToString().Replace("-", "") + ".zip", "application/x-zip-compressed", strReport + "_" + g.ToString().Replace("-", "") + ".zip");
                                }

/// <summary>
        /// Html่ฝ‰PDFๅŒฏๅ‡บ
        /// </summary>
        /// <param name="html">html่ชžๆณ•</param>
        /// <param name="path">Temp.pdf่ทฏๅพ‘</param>
        public static void HtmlToPdf(string html, string path)
        {
            //Function Definition
            StringBuilder paramsBuilder = new StringBuilder();
            //Build command
            paramsBuilder.AppendFormat("\"-\" \"{0}\"", path);
            //process to execute above command
            using (Process process = new Process())
            {
                //path of wkhtmltopdf.exe file
                string filepath = @"wkhtmltopdf.exe";
                //specify path
                process.StartInfo.FileName = filepath;
                //set command by argument
                process.StartInfo.Arguments = paramsBuilder.ToString();
                //set credentials

                Console.InputEncoding = Encoding.UTF8;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardInput = true;

                //start process
                process.Start();
                using (var stream = process.StandardInput)
                {
                    byte[] buffer = Encoding.UTF8.GetBytes(html);
                    stream.BaseStream.Write(buffer, 0, buffer.Length);
                    stream.WriteLine();
                }
                process.Close();
            }
            Thread.Sleep(2000);
        }

    ๅŒฏๅ‡บtxt
   bResult = Encoding.Unicode.GetBytes(new PublicFunction().txtExport(sSql, program_no));

                return File(bResult, "text/plain", program_no + guid.ToString() + ".txt");

ๆ•ธๅญ—่ฝ‰่‹ฑๆ–‡
โ€‹public static string NumberToText(int number, bool isUK)
โ€‹โ€‹โ€‹โ€‹{
โ€‹โ€‹โ€‹โ€‹    if (number == 0) 
โ€‹โ€‹โ€‹โ€‹    	return "Zero";
โ€‹โ€‹โ€‹โ€‹    string and = isUK ? "and " : ""; // deals with UK or US numbering
โ€‹โ€‹โ€‹โ€‹    
โ€‹โ€‹โ€‹โ€‹    if (number == -2147483648) 
โ€‹โ€‹โ€‹โ€‹    	return "Minus Two Billion One Hundred " + and +
โ€‹โ€‹โ€‹โ€‹    "Forty Seven Million Four Hundred " + and + "Eighty Three Thousand " +
โ€‹โ€‹โ€‹โ€‹    "Six Hundred " + and + "Forty Eight";
โ€‹โ€‹โ€‹โ€‹    
โ€‹โ€‹โ€‹โ€‹    int[] num = new int[4];
โ€‹โ€‹โ€‹โ€‹    int first = 0;
โ€‹โ€‹โ€‹โ€‹    int u, h, t;
โ€‹โ€‹โ€‹โ€‹    System.Text.StringBuilder sb = new System.Text.StringBuilder();
โ€‹โ€‹โ€‹โ€‹    
โ€‹โ€‹โ€‹โ€‹    if (number < 0)
โ€‹โ€‹โ€‹โ€‹    {
โ€‹โ€‹โ€‹โ€‹        sb.Append("Minus ");
โ€‹โ€‹โ€‹โ€‹        number = -number;
โ€‹โ€‹โ€‹โ€‹    }
โ€‹โ€‹โ€‹โ€‹    string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };
โ€‹โ€‹โ€‹โ€‹    string[] words1 = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };
โ€‹โ€‹โ€‹โ€‹    string[] words2 = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };
โ€‹โ€‹โ€‹โ€‹    string[] words3 = { "Thousand ", "Million ", "Billion " };
โ€‹โ€‹โ€‹โ€‹    num[0] = number % 1000;           // units
โ€‹โ€‹โ€‹โ€‹    num[1] = number / 1000;
โ€‹โ€‹โ€‹โ€‹    num[2] = number / 1000000;
โ€‹โ€‹โ€‹โ€‹    num[1] = num[1] - 1000 * num[2];  // thousands
โ€‹โ€‹โ€‹โ€‹    num[3] = number / 1000000000;     // billions
โ€‹โ€‹โ€‹โ€‹    num[2] = num[2] - 1000 * num[3];  // millions
โ€‹โ€‹โ€‹โ€‹    
โ€‹โ€‹โ€‹โ€‹    for (int i = 3; i > 0; i--)
โ€‹โ€‹โ€‹โ€‹    {
โ€‹โ€‹โ€‹โ€‹        if (num[i] != 0)
โ€‹โ€‹โ€‹โ€‹        {
โ€‹โ€‹โ€‹โ€‹            first = i;
โ€‹โ€‹โ€‹โ€‹            break;
โ€‹โ€‹โ€‹โ€‹        }
โ€‹โ€‹โ€‹โ€‹    }
โ€‹โ€‹โ€‹โ€‹    
โ€‹โ€‹โ€‹โ€‹    for (int i = first; i >= 0; i--)
โ€‹โ€‹โ€‹โ€‹    {
โ€‹โ€‹โ€‹โ€‹        if (num[i] == 0) continue;
โ€‹โ€‹โ€‹โ€‹        u = num[i] % 10;              // ones
โ€‹โ€‹โ€‹โ€‹        t = num[i] / 10;
โ€‹โ€‹โ€‹โ€‹        h = num[i] / 100;             // hundreds
โ€‹โ€‹โ€‹โ€‹        t = t - 10 * h;               // tens
โ€‹โ€‹โ€‹โ€‹        if (h > 0) sb.Append(words0[h] + "Hundred ");
โ€‹โ€‹โ€‹โ€‹        if (u > 0 || t > 0)
โ€‹โ€‹โ€‹โ€‹        {
โ€‹โ€‹โ€‹โ€‹            if (h > 0 || i < first) sb.Append(and);
โ€‹โ€‹โ€‹โ€‹            if (t == 0)
โ€‹โ€‹โ€‹โ€‹                sb.Append(words0[u]);
โ€‹โ€‹โ€‹โ€‹            else if (t == 1)
โ€‹โ€‹โ€‹โ€‹                sb.Append(words1[u]);
โ€‹โ€‹โ€‹โ€‹            else
โ€‹โ€‹โ€‹โ€‹                sb.Append(words2[t - 2] + words0[u]);
โ€‹โ€‹โ€‹โ€‹        }
โ€‹โ€‹โ€‹โ€‹        if (i != 0) sb.Append(words3[i - 1]);
โ€‹โ€‹โ€‹โ€‹    }
โ€‹โ€‹โ€‹โ€‹    return sb.ToString().TrimEnd();
โ€‹โ€‹โ€‹โ€‹}

MessageBoxButtons

                    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                    DialogResult result;
                    // Displays the MessageBox.
                		result = MessageBox.Show("่ญฆๅ‘Š,่ถ…้Ž่ฉฒ่ชฒ็จ‹้กžๅˆฅๆœ€ๅคšๅฏ้ธไธŠ็ง‘็›ฎๆ•ธ,ๆ˜ฏๅฆๅฏ้ธ่ชฒ๏ผŸ(Y/N)", "่ญฆๅ‘Š", buttons, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                        if (result == DialogResult.No)
                            return null;
่ฅฟๅ…ƒๆฐ‘ๅœ‹่ฝ‰ๆ›
/// <summary>
/// ่ฅฟๅ…ƒๅนด่ฝ‰ๆฐ‘ๅœ‹ๅนด
/// </summary>
/// <param name="dateTime"></param>
/// <returns>ๆฐ‘ๅœ‹ๅนดtw</returns>
public static string CE_TO_TW(string datetime)
{
    DateTime dt = DateTime.Parse(datetime);
    CultureInfo culture = new CultureInfo("zh-TW");
    culture.DateTimeFormat.Calendar = new TaiwanCalendar();
    return dt.ToString("yyy/MM/dd", culture);
}

/// <summary>
/// ๆฐ‘ๅœ‹ๅนด่ฝ‰่ฅฟๅ…ƒๅนด
/// </summary>
/// <param name="dateTime"></param>
/// <returns>่ฅฟๅ…ƒๅนดCE</returns>
public static string TW_TO_CE(string datetime)
{
    CultureInfo culture = new CultureInfo("zh-TW");
    culture.DateTimeFormat.Calendar = new TaiwanCalendar();
    return DateTime.Parse(datetime, culture).ToString();
}

ๅง”ๆดพ็ฏ„ไพ‹
public delegate void NotifyDelegate(string message);

class Program
{
    static void Main(string[] args)
    {
        NotifyDelegate notify = null;

        // ็ถๅฎšๅคšๅ€‹ๆ–นๆณ•
        notify += SendEmail;
        notify += SendSMS;
        notify += LogToConsole;

        // ่ชฟ็”จๅง”ๆดพ๏ผŒๅŸท่กŒๆ‰€ๆœ‰็ถๅฎšๆ–นๆณ•
        notify?.Invoke("System alert!");

        // ็งป้™คไธ€ๅ€‹ๆ–นๆณ•
        notify -= SendSMS;
        Console.WriteLine("\nAfter removing SendSMS:");
        notify?.Invoke("System alert again!");
    }

    static void SendEmail(string message)
    {
        Console.WriteLine($"Email: {message}");
    }

    static void SendSMS(string message)
    {
        Console.WriteLine($"SMS: {message}");
    }

    static void LogToConsole(string message)
    {
        Console.WriteLine($"Console: {message}");
    }
}