Beispiel #1
0
        /// <summary>
        /// Datatable to csv file
        /// </summary>
        /// <param name="dt">Datatable object</param>
        /// <param name="title">Csv file title</param>
        /// <returns>0:wrote successfully, 1:file already exist</returns>
        public void WriteFile(DataTable dt, string title    = ""
                              , LogHelperMode logHelperMode = LogHelperMode.Yearly)
        {
            string directory = GetDirectory(logHelperMode) + @"\";

            string fullPath = directory + title + ".csv";

            using (FileStream fs = File.Create(fullPath))
            {
                StringBuilder sb    = new StringBuilder();
                string        comma = "";

                foreach (DataColumn col in dt.Columns)
                {
                    sb.Append(comma + "\"" + col.ColumnName + "\"");
                    comma = ",";
                }

                foreach (DataRow row in dt.Rows)
                {
                    comma = "";
                    sb.Append(Environment.NewLine);
                    foreach (DataColumn col in dt.Columns)
                    {
                        sb.Append(comma + "\"" + row[col] + "\"");
                        comma = ",";
                    }
                }

                byte[] bytes = Encoding.Default.GetBytes(sb.ToString());
                fs.Write(bytes, 0, bytes.Length);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Get directory
        /// </summary>
        /// <param name="logHelperMode"></param>
        /// <returns></returns>
        private string GetDirectory(LogHelperMode logHelperMode)
        {
            string fullPath = Path;

            if (logHelperMode == LogHelperMode.Yearly)
            {
                fullPath += DateTime.Now.ToString("yyyy");
            }
            else if (logHelperMode == LogHelperMode.Monthly)
            {
                fullPath += DateTime.Now.ToString("yyyy")
                            + @"\" + DateTime.Now.ToString("MM");
            }
            else if (logHelperMode == LogHelperMode.Daily)
            {
                fullPath += DateTime.Now.ToString("yyyy")
                            + @"\" + DateTime.Now.ToString("MM")
                            + @"\" + DateTime.Now.ToString("dd");
            }

            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(fullPath);
            }

            return(fullPath);
        }
Beispiel #3
0
        /// <summary>
        /// Write log to file ( txt )
        /// </summary>
        /// <param name="str">Log file string</param>
        /// <param name="title">File name</param>
        /// <returns>0:wrote successfully, 1:file already exist</returns>
        public void WriteFile(string title, string body
                              , LogHelperMode logHelperMode = LogHelperMode.Yearly)
        {
            string directory = GetDirectory(logHelperMode) + @"\";

            string fullPath = directory + title + ".txt";

            using (FileStream fs = File.Create(fullPath))
            {
                fs.Write(Encoding.ASCII.GetBytes(body), 0, body.Length);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Write log text file ( each month each file )
        /// </summary>
        /// <param name="str">Log string</param>
        public void WriteText(string tag, string str
                              , LogHelperMode logHelperMode = LogHelperMode.Yearly)
        {
            FileStream fs;
            string     fullPath = GetDirectory(logHelperMode) + ".txt";

            if (!File.Exists(fullPath))
            {
                fs = File.Create(fullPath);
            }
            else
            {
                fs = new FileStream(fullPath, FileMode.Append);
            }

            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]\n")
                             + "<" + ProgramName + "-" + tag + ">\n" + str + "\n");
            }

            fs.Close();
        }