Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi there guys. I have a little problem here. I got an application which sends a list of files name through email notifying the user that those files has exceeded a certain size limit. But now I want it to not only send the files name but also the files size. I manage to attach the file size to the email. But my problem is the way it displayed. I want it to displayed like :

THE FILES :

filename1 = filesize1
filename2 = filesize2
filename3 = filesize3
..
..
..

HAS REACHED ITS LIMITS!

But my current display format in the email is like :

THE FILES :

filename1
filename2
filename3
filesize1
filesize2
filesize3

HAS REACHED ITS LIMITS!

And now I don't know how to change the display format like the first one. Any help would be greatly appreciated. Here is my source :

C#
//My size checking and store it into a list.
 if ((info.Length / 1024) > 1500000)
{
     files.Add(sysInfo.Name.ToString());
     files1.Add(info.Length.ToString());
     arr = string.Join("<br/>", files.ToArray());
     arr1 = string.Join("<br/>", files1.ToArray());
}


C#
//attaching the list to an email body
mailMessage.Body = "THE FILES : <br/><br/>" + arr + arr1 + "<br/><br/> HAS REACH ITS SIZE LIMIT!!";
Posted
Comments
[no name] 19-Apr-12 22:12pm    
Okay... so what have you tried? Seems relatively simple to loop through your arrays and add the strings to the message body.
nizam15 19-Apr-12 22:18pm    
Well the only one I've tried is like I've shown in my question. I tried to use attaching the array string to the message body of both the files name and size. But it came out like the output I've show in my question.
[no name] 19-Apr-12 22:51pm    
And so? Judging by your past questions and the answers that you have been given, it should be well within your knowledge to write a for loop. Even a foreach. Instead of waiting for someone to write your code for you, you would learn a lot more by actually trying to do something for yourself.
Mohammad A Rahman 19-Apr-12 22:53pm    
Thanks :)
[no name] 19-Apr-12 22:55pm    
For?

Please have a look below,

C#
using System;
using System.IO;
using System.Linq;
using System.Text;

namespace Chapter_5
{
    class Program
    {
        static void Main()
        {
            string directoryName = @"C:\\textfiles";
            Console.WriteLine(FindFileswhichReachedLimit(directoryName));
        }

        static string FindFileswhichReachedLimit(string directoryName)
        {
            StringBuilder builder = new StringBuilder();
            Directory.GetFiles(directoryName)
                .Where(file =>
                {
                    return IsReachedMax(Path.Combine(directoryName, file));
                })
                .Select(file =>
                {
                    return string.Format("{0,40}\t{1}{2}",
                        Path.GetFileName(file),
                        GetFileSize(Path.Combine(directoryName, file)),
                        Environment.NewLine);
                }).ToList().ForEach(fileInfo => builder.Append(fileInfo));
            return builder.ToString();

        }

        static bool IsReachedMax(string fileName)
        {
            // I modified the condition to test So please updated as yours
            return GetFileSize(fileName) / 1024 < 1500000;
        }

        static long GetFileSize(string fileName)
        {
            return (new FileInfo(fileName)).Length;
        }
    }
}


Hope it helps :)

2nd Version:

C#
class Program
{
    static void Main()
    {
        string directoryName = @"C:\\textfiles";
        Console.WriteLine(FindFileswhichReachedLimit(directoryName));
    }

    static string FindFileswhichReachedLimit(string directoryName)
    {
        StringBuilder builder = new StringBuilder();
        IList<string> files = Directory.GetFiles(directoryName);
        foreach (string file in files)
        {
            if (IsReachedMax(Path.Combine(directoryName, file)))
            {
                string fileInfo = string.Format("{0,40}\t{1}{2}", 
                    Path.GetFileName(file), 
                    GetFileSize(Path.Combine(directoryName, file)), 
                    Environment.NewLine);
                builder.Append(fileInfo);
            }

        }
        return builder.ToString();
    }

    static bool IsReachedMax(string fileName)
    {
        // I modified the condition to test So please updated as yours
        return GetFileSize(fileName) / 1024 < 1500000;
    }

    static long GetFileSize(string fileName)
    {
        return (new FileInfo(fileName)).Length;
    }
}
 
Share this answer
 
v2
Comments
nizam15 19-Apr-12 22:27pm    
I really appreciate your answer suggestion but its quite complex to me. Sorry I'm quite new in programming. Is there any way I can modified my current code to get the result??sorry.
Mohammad A Rahman 19-Apr-12 22:49pm    
Plz see the updated one :)
nizam15 19-Apr-12 23:21pm    
Thanks Mohammad for the suggestion.!:)
Mohammad A Rahman 20-Apr-12 0:06am    
Not a problem :)
Nobody is going to be able to really help you with this problem since we do not know what class sysInfo derives from. Normally file info is provided by the FileInfo class. The FileInfo has properties for length, file name, etc.

If you have some way to get the size of the file you are outputting then the following will work:

if ((info.Length / 1024) > 1500000)
{
     files.Add("{0,40}\t{1}", sysInfo.Name, info.Length);
}
arr = string.Join("<br/>", files.ToArray());
arr1 = string.Join("<br/>", files1.ToArray());
 
Share this answer
 
Comments
nizam15 19-Apr-12 23:25pm    
Thanks Clifford!Yes it is provided by FileInfo class. Sorry for not putting my full code here. I manage to solved it. But out of curiosity, can you explain to me the "{0,40}\t{1}" part of your suggestion. I don't quite understand how that part works.sorry.
Clifford Nelson 19-Apr-12 23:35pm    
In the string.Format you the first number in the curly brases is the reference to the arguments in the Format method after the string with the format information (in this case "{0,40}\t{1}"), the next is the number of spaces that are to be used for the field, This is how you effectively get a table like output. Otherwise everything would be jagged. In this case I am telling the system to use 40 spaces for the sysInfo.Name. See http://idunno.org/archive/2004/07/14/122.aspx
nizam15 19-Apr-12 23:40pm    
Understood.Thanks a lot for the lesson Clifford and thanks for the reference!Looks like I've still got a lot more to learn.:)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900