Click here to Skip to main content
15,892,059 members
Articles / Web Development / ASP.NET

Checksum Verification

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Sep 2011CPOL3 min read 52.5K   1.4K   17  
Loop through all files in a folder/subfolders and run a checksum that is stored in a database. E-mail descrepancies and results.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using ChecksumFiles.Utilities;

namespace ChecksumFiles.Utilities
{
    class FileGenerator
    {

        public static string m_results = Settings.Default.ResultFile.ToString();
        public static string m_errors = Settings.Default.ErrorFile.ToString();


        public static string CreateFile(DataTable dt, string strFilePath)
        {
            try
            {
                // StreamWriter and StringBuilder are used because the email body and
                // the file contents have slightly different data.  This method allows for 
                // fewer database calls and a single loop
                StreamWriter sw = new StreamWriter(strFilePath, false);
                StringBuilder sb = new StringBuilder();

                // First we will write the headers.
                int iColCount = dt.Columns.Count;

                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(dt.Columns[i]);
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }

                sw.Write(sw.NewLine);
                sb.AppendLine();

                // Now write all the rows.
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < iColCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            sw.Write(dr[i].ToString());

                        }

                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }
                    }
                    sw.Write(sw.NewLine);

                    sb.Append(dr["FILEPATH"].ToString());
                    sb.AppendLine();

                }

                sw.Close();

                return sb.ToString();

            }

            catch (Exception ex)
            {
                using (StreamWriter swerr = File.AppendText(m_errors))
                {
                    Logger.LogMessage(ex.Message.ToString(), swerr);
                    swerr.Close();
                }

                return "Unable to create file or email content.";

            }
        }

        public static string CreateFile(DataTable dt)
        {
            try
            {
                // This method creates email content only.  
                StringBuilder sb = new StringBuilder();

                sb.AppendLine();

                // Now write all the rows.
                foreach (DataRow dr in dt.Rows)
                {
                    sb.Append(dr["FILEPATH"].ToString());
                    sb.AppendLine();
                }

                return sb.ToString();
            }
            catch (Exception ex)
            {
                using (StreamWriter swerr = File.AppendText(m_errors))
                {
                    Logger.LogMessage(ex.Message.ToString(), swerr);
                    swerr.Close();
                }

                return "Unable to create email content.";

            }

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Database Developer
United States United States
I am an MBA with a bunch of MS certifications. Technically, I am a DBA, but I do a good deal of sys admin work and web development using .NET. I like to focus on business intelligence, database design, messaging architectures, and web services.

Comments and Discussions