locked
finding all file sizes in a directory in c# RRS feed

  • Question

  • I have a string array with different file names (with path) and I want to find out the size of each one. How can I find the size of each file using C#?

    Please suggest!!

    • Moved by Calvin_Gao Tuesday, March 22, 2011 2:59 AM (From:Visual Basic Language)
    Sunday, March 20, 2011 7:46 PM

Answers

  • You can directly use this:

    System.IO.FileInfo f = new FileInfo("aaa.txt"); 
    Console.WriteLine(f.Length.ToString());



    May this code snippet also give some ideas to your requirement:
    Converted from the VB code of maxter.
    See the Code Converter here:http://www.carlosag.net/Tools/CodeTranslator/.

       foreach (string item in filelist)
    
       {
    
        ((int)(blen)) = My.Computer.FileSystem.GetFileInfo(item).Length;
    
        int len = 0;
    
        bool donext = true;
    
        if ((blen <= 1024))
    
        {
    
         donext = false;
    
        }
    
        while ((donext == true))
    
        {
    
         blen -= 1024;
    
         len++;
    
         if ((blen <= 1024))
    
         {
    
          donext = false;
    
         }
    
        }
    
        MsgBox(("File Size(Kilobytes) for "
    
         + (item + (": " + len.ToString))));
    
       }
    
    

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 23, 2011 5:08 AM

All replies

  • Bytes:

    For Each item As String In filelist
    MsgBox("File Size(Bytes) for " + item + ": "+My.Computer.FileSystem.GetFileInfo(item).Length.ToString)
    Next
    Kilobytes:
    For Each item As String In filelist
    Dim blen As Integer = +My.Computer.FileSystem.GetFileInfo(item).Length Dim len As Integer = 0 Dim donext As Boolean = True If blen <= 1024 Then donext = False End If While donext = True blen -= 1024 len += 1 If blen <= 1024 Then donext = False End If End While MsgBox("File Size(Kilobytes) for " + item + ": "+len.ToString)
    Next

    If you want it in C#, Google "Convert VB.NET to C#".

    Also, Replace filelist in the For statements as the list in which the file paths are.

    • Proposed as answer by maxter1999 Sunday, March 20, 2011 8:01 PM
    Sunday, March 20, 2011 8:01 PM
  • I have a string array with different file names (with path) and I want to find out the size of each one. How can I find the size of each file using C#?

    Please suggest!!

    Hi,

    Visual C# forums are here:>>

    http://social.msdn.microsoft.com/Forums/en/category/visualcsharp

     

    and the Express Edition Visual C# forum is here:>>

    http://social.msdn.microsoft.com/Forums/en/Vsexpressvcs/threads



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Sunday, March 20, 2011 10:25 PM
  • You can directly use this:

    System.IO.FileInfo f = new FileInfo("aaa.txt"); 
    Console.WriteLine(f.Length.ToString());



    May this code snippet also give some ideas to your requirement:
    Converted from the VB code of maxter.
    See the Code Converter here:http://www.carlosag.net/Tools/CodeTranslator/.

       foreach (string item in filelist)
    
       {
    
        ((int)(blen)) = My.Computer.FileSystem.GetFileInfo(item).Length;
    
        int len = 0;
    
        bool donext = true;
    
        if ((blen <= 1024))
    
        {
    
         donext = false;
    
        }
    
        while ((donext == true))
    
        {
    
         blen -= 1024;
    
         len++;
    
         if ((blen <= 1024))
    
         {
    
          donext = false;
    
         }
    
        }
    
        MsgBox(("File Size(Kilobytes) for "
    
         + (item + (": " + len.ToString))));
    
       }
    
    

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 23, 2011 5:08 AM