Ghi log to file Text c#

0 thích 0 không thích
1 lượt xem
đã hỏi 11 Tháng 1 trong Lập trình C# bởi nguyenthao (9,000 điểm)
 public void WriteToFile(string Message)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
            if (!File.Exists(filepath))
            {
                using (StreamWriter sw = File.CreateText(filepath))
                {
                    sw.WriteLine(Message);
                }
            }
            else
            {
                using (StreamWriter sw = File.AppendText(filepath))
                {
                    sw.WriteLine(Message);
                }
            }
        }

    

1 câu trả lời

0 thích 0 không thích
đã trả lời 12 Tháng 1 bởi nguyenthao (9,000 điểm)
using System;

using System.Text.RegularExpressions;
using System.Collections.Generic;
                   
public class Program
{
    public static void Main()
    {
         string text = "Nguyễn Kim Tuấn - 0963009554";

        List<string> phoneNumbers = ExtractPhoneNumbers(text);

        foreach (string phoneNumber in phoneNumbers)
        {
            Console.WriteLine(phoneNumber);
        }
    }
     static List<string> ExtractPhoneNumbers(string input)
    {
        List<string> phoneNumbers = new List<string>();

        // Define a regular expression pattern for Vietnamese phone numbers
        string pattern = @"(\+?84|0)([1-9][0-9]{8})\b";

        // Create a regex object
        Regex regex = new Regex(pattern);

        // Find all matches
        MatchCollection matches = regex.Matches(input);

        // Extract phone numbers from matches
        foreach (Match match in matches)
        {
            phoneNumbers.Add(match.Value);
        }

        return phoneNumbers;
    }
}
...