Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Checksum Verification

, 22 Sep 2011
Loop through all files in a folder/subfolders and run a checksum that is stored in a database. E-mail descrepancies and results.
ChecksumFiles.zip
ChecksumFiles
bin
obj
Properties
Settings.settings
Utilities
DRMC.zip
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)

Share

About the Author

smoore4
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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 23 Sep 2011
Article Copyright 2011 by smoore4
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid