.Net公用代码

创建txt文本文件
 #region 创建txt文本文件
        /// <summary>
        /// 创建txt文本文件
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="filename">文件名</param>
        public static void CreateTxtFile(string content, string filename)
        {
            string directory = System.Environment.CurrentDirectory + "\\Export";
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            string path = string.Format("{0}\\{1}--{2}.txt", directory, filename, DateTime.Now.ToString("yyyy-MM-dd"));
            if (!File.Exists(path))
            {
                File.Create(path).Dispose();
            }
            else
            {
                File.Delete(path);
            }
            using (StreamWriter sw = System.IO.File.AppendText(path))
            {
                sw.WriteLine(content);
            }
        }
        #endregion
View Code
创建csv文件
#region 创建csv文件到bin目录下的Export文件夹
        /// <summary>
        /// 创建csv文件
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="filename">文件名</param>
        public static void CreateCsvFile(string content, string filename, System.Text.Encoding encoding)
        {
            string directory = System.Environment.CurrentDirectory + "\\Export";
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            string path = string.Format("{0}\\{1}--{2}.csv", directory, filename, DateTime.Now.ToString("yyyy-MM-dd"));
            if (!File.Exists(path))
            {
                File.Create(path).Dispose();
            }
            else
            {
                File.Delete(path);
            }
            System.IO.File.AppendAllText(path, content, encoding);
        }
        #endregion
View Code

读取文本文件

 string[] first = File.ReadAllLines(path, Encoding.UTF8);
Split函数
string[] second = item1.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);

 写日志

public void WriteLog(string UName)
        {
            string logDirPath = System.AppDomain.CurrentDomain.BaseDirectory + @"Log\";//日志目录
            string logFilePath = logDirPath + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";//日志文件全路径
                                                                                                  //检查是否有该路径  没有就创建
            if (!System.IO.Directory.Exists(logDirPath))
            {
                System.IO.Directory.CreateDirectory(logDirPath);//创建文件目录
            }

            using (StreamWriter sw = System.IO.File.AppendText(logFilePath))
            {
                sw.WriteLine(UName);
            }
        }
View Code

 处理标签

 private void button1_Click(object sender, EventArgs e)
        {
            const string strSql = @"
SELECT UserCode, UserName, UserAccount, DepartmentId, DepartmentName, IsActive,PostTitle,Name
FROM dbo.v_UserInfo
";

            var dt = SqlHelper.ExecuteDataset(Database.PurConnectionStrings, CommandType.Text, strSql)?.Tables[0];

            int count = dt.Rows.Count;

            if (dt != null && count > 0)
            {
                int iseek = 0;
                for (int i = 0; i < count; i++)
                {
                    iseek++;

                    lblStatus.Text = string.Format("处理标签,共 :{0},已处理:{1} )", count.ToString(), iseek.ToString());
                    Application.DoEvents();
                }
            }

        }
View Code

 

posted @ 2018-03-22 16:50  ~沐风  阅读(208)  评论(0编辑  收藏  举报