CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: help pls

  1. #1
    Join Date
    Jan 2012
    Posts
    8

    help pls

    Hello. Im a starter in C# and i need some help with some codes.

    What i need is to display the folder statistics of a particular folder. ( in this occasion i want C:\Windows). Display the following statistics about all the files in the current folder.

    Total files: 49
    Total size of all files: 7121424 bytes
    Largest file: explorer.exe, 2871808 bytes
    Average file length: 145335 bytes


    this is what i want to display. Please someone tell me the code for this things. Im a starter please something simple if it is possible. Please if it is possible include a full code with variables etc.

    Thanks in advance.

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: help pls

    Code:
    using System;
    using System.IO;
    
    namespace TestConsole
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                long fileCount = 0;
                long totalSizeOfAllFiles = 0;
                long largestFileSize = 0;
                long averageFileLength = 0;
                string largestFileName = String.Empty;
                string[] filePaths;
    
                Console.WriteLine("Processing Files...");
    
                try
                {
                    filePaths = Directory.GetFiles(@"C:\Windows", "*.*", SearchOption.AllDirectories);
    
                    foreach (string path in filePaths)
                    {
                        FileInfo info = new FileInfo(path);
    
                        fileCount++;
                        totalSizeOfAllFiles += info.Length;
                        if (info.Length > largestFileSize)
                        {
                            largestFileSize = info.Length;
                            largestFileName = info.Name;
                        }
                    }
                    averageFileLength = (totalSizeOfAllFiles / fileCount);
    
                    Console.WriteLine("File Count: {0}", fileCount);
                    Console.WriteLine("Total Size: {0} bytes", totalSizeOfAllFiles);
                    Console.WriteLine("Largest File: {0} ({1} bytes)", largestFileName, largestFileSize);
                    Console.WriteLine("Average File Size: {0} bytes", averageFileLength);
                    Console.WriteLine("Press any key to exit...");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    Console.ReadLine();
                }
            }
        }
    }
    I was getting an exception when the Directory.GetFiles() method tried to access the C:\Windows\...\RtBackup folder. It will probably be the same for you, so just keep that in mind. But it should work on just about any other folders on the system.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  3. #3
    Join Date
    Jan 2012
    Posts
    8

    Re: help pls

    Life saver it worked.

    I really really appreciate it my friend. Thanks a lot.

  4. #4
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: help pls

    Quote Originally Posted by blacksmoke40 View Post
    Life saver it worked.

    I really really appreciate it my friend. Thanks a lot.
    No problem. Little exercises like this help refresh me on concepts I haven't used in awhile, haha.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  5. #5
    Join Date
    Jan 2012
    Posts
    8

    Re: help pls

    i was wondering if you can help me with filtered File Listing. What i mean is i want to ask the user for a file filter and display only the files in the current folder that match that filter. An example of a filter might be "*.exe", which only shows the files ending with .exe

  6. #6
    Join Date
    Jan 2012
    Posts
    8

    Re: help pls

    i was wondering if you can help me with filtered File Listing. What i mean is i want to ask the user for a file filter and display only the files in the current folder that match that filter. An example of a filter might be "*.exe", which only shows the files ending with .exe also displaying the filter used

  7. #7
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: help pls

    Easy enough. You see where Directory.GetFiles() is called? You can specify the filter there. I used the filter "*.*" to get all files. But you could prompt the user for a filter by altering the code so it looks similar to

    Code:
    string filter;
    
    Console.Write("Please enter a file filter: ");   // prompt for the filter
    filter = Console.ReadLine();                           // get the filter
    
    filePaths = Directory.GetFiles(@"C:\Windows", filter, SearchOption.AllDirectories); // apply the filter and call GetFiles()
    This would prompt the user to input a filter and then pass that filter to the Directory.GetFiles() method.

    You would see something like

    Code:
    Please enter a file filter: *.exe
    Popup on the screen when you run it.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  8. #8
    Join Date
    Jan 2012
    Posts
    8

    Re: help pls

    im expiriencing some difficulties. its a very small program. if it is easy to you can i send it to you and help me with it ?

  9. #9
    Join Date
    Jan 2012
    Posts
    8

    Re: help pls

    its something like a menu and i cant return after i enter a choice. for example i can choose from 1 to 4. if i choose something and im done with it how can i return back to that menu ?

  10. #10
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: help pls

    Use a while loop and evaluate each input

    Code:
                bool exit = false;
    
                while (!exit)
                {
                    string optionStr;
                    int option;
    
                    Console.WriteLine("Please enter an option: ");
                    Console.WriteLine("1: <option 1>");
                    Console.WriteLine("2: <option 2>");
                    Console.WriteLine("3: <option 3>");
                    Console.WriteLine("4: Exit");
    
                    optionStr = Console.ReadLine();
                    option = Int32.Parse(optionStr);
    
                    switch (option)
                    {
                        case 1:
                            // execute option 1
                            break;
                        case 2:
                            // execute option 2
                            break;
                        case 3:
                            // execute option 3
                            break;
                        case 4:
                            exit = true;    // cause main loop to break
                            break;
                        default:
                            Console.WriteLine("Sorry, {0} is not a valid option.", option);
                            break;
                    }
                }
    I know it's a common pattern, but I don't remember the name for it... Once you get into command line loops in college and such you'll see it all the time.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  11. #11
    Join Date
    Jan 2012
    Posts
    8

    Re: help pls

    https://rapidshare.com/files/828697748/Menu.rar

    please help me complete it. I m struggling to finish it and i cant.

  12. #12
    Join Date
    Jan 2012
    Posts
    8

    Re: help pls

    Hi. I need help again pls respond you are a life saver :P.
    i have this situation.

    {
    DirectoryInfo folderInfo = new DirectoryInfo("C:\\Windows");
    FileInfo[] files = folderInfo.GetFiles();
    Console.WriteLine("Currently Displaying Files within C:\\Windows");
    Console.WriteLine();
    Console.WriteLine("Please Select one of these operations");
    Console.WriteLine();
    Console.WriteLine(" 1. Full File Listing");
    Console.WriteLine(" 2. Filtered File Listing");
    Console.WriteLine(" 3. Folder Statistics");
    Console.WriteLine(" 4. Quit");
    Console.WriteLine();
    string yesNo;
    int userInput;
    bool isNumber = int.TryParse(Console.ReadLine(), out userInput);
    while (isNumber == false || userInput <= 0 || userInput > 4)

    if (isNumber == false)
    {
    Console.WriteLine("Cannot enter letters. Please enter again only a number from 1 to 4");
    }
    else
    {
    Console.WriteLine("Please enter a number from 1 to 4: ");
    }

    isNumber = int.TryParse(Console.ReadLine(), out userInput);

    }

    while (isNumber == true)
    {
    if (userInput == 1) // Will display a Full File Listing
    {

    }

    else if (userInput == 2) // Will display a Filtered File Listing
    {

    }

    else if (userInput == 3) // Will display the Folder's Statistics
    {

    }

    else if (userInput == 4) // Will Quit the Program

    do
    {
    Console.Write("Are you sure you want to exit ? Enter y or n: ");
    yesNo = Console.ReadLine().ToLower();
    } while (yesNo != "y" && yesNo != "n");

    else
    {

    }



    i have used else if instead of case. there is a menu from 1 to 4. when the user selects something from 1-4 and is done with it i want when he press any button to return back to that main menu that he is prompted to select one of these options. Also on option 4 what is the code to stop the program for running ?

    Can you help me with it ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured