Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. get files one by one in the lable

get files one by one in the lable

Scheduled Pinned Locked Moved C#
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tanweer
    wrote on last edited by
    #1

    hello every body i want to get the file list one by one in a lable giving path of the folder of the file using this code but it does not work for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); label2.Text = file[i].ToString(); } can any body explain me

    S 1 Reply Last reply
    0
    • T tanweer

      hello every body i want to get the file list one by one in a lable giving path of the folder of the file using this code but it does not work for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); label2.Text = file[i].ToString(); } can any body explain me

      S Offline
      S Offline
      sanforjackass
      wrote on last edited by
      #2

      as i understand you want to write all dir. in label, try this: for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); label2.Text += file[i].ToString()+"\n"; } or arraylist a=new arraylist(); for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); //label2.Text += file[i].ToString()+"\n"; a.add(file[i].ToString()); }

      T 1 Reply Last reply
      0
      • S sanforjackass

        as i understand you want to write all dir. in label, try this: for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); label2.Text += file[i].ToString()+"\n"; } or arraylist a=new arraylist(); for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); //label2.Text += file[i].ToString()+"\n"; a.add(file[i].ToString()); }

        T Offline
        T Offline
        tanweer
        wrote on last edited by
        #3

        thanks sir but exactly what i need is the files name of the dir. is just kike the anti virus file scane looks that is file name and the path is showing in the lable might be using timerin it private void button1_Click(object sender, EventArgs e) { timer1.Start(); timer1.Interval = 2000; } private void timer1_Tick(object sender, EventArgs e) { for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); label2.Text = file[i].ToString(); } timer1.Stop(); } whatis the problem in it this shows only the last file of the dir.

        S OriginalGriffO 2 Replies Last reply
        0
        • T tanweer

          thanks sir but exactly what i need is the files name of the dir. is just kike the anti virus file scane looks that is file name and the path is showing in the lable might be using timerin it private void button1_Click(object sender, EventArgs e) { timer1.Start(); timer1.Interval = 2000; } private void timer1_Tick(object sender, EventArgs e) { for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); label2.Text = file[i].ToString(); } timer1.Stop(); } whatis the problem in it this shows only the last file of the dir.

          S Offline
          S Offline
          sanforjackass
          wrote on last edited by
          #4

          sorry are you trying to make anti virus program

          T 1 Reply Last reply
          0
          • S sanforjackass

            sorry are you trying to make anti virus program

            T Offline
            T Offline
            tanweer
            wrote on last edited by
            #5

            No sir i just try to make a file counter in my program that it------------->>>>>>>>>>>>>>> can u hepl me..........>>>>>>>>>>>>>>>

            S 1 Reply Last reply
            0
            • T tanweer

              thanks sir but exactly what i need is the files name of the dir. is just kike the anti virus file scane looks that is file name and the path is showing in the lable might be using timerin it private void button1_Click(object sender, EventArgs e) { timer1.Start(); timer1.Interval = 2000; } private void timer1_Tick(object sender, EventArgs e) { for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); label2.Text = file[i].ToString(); } timer1.Stop(); } whatis the problem in it this shows only the last file of the dir.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              tanweer akhtar wrote:

              for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); label2.Text = file[i].ToString(); } timer1.Stop(); }

              Yes, it will show only the last file in the list. Try replacing:

              label2.Text = file[i].ToString();

              with

              label2.Text += file[i].ToString();

              as this means the new name will be appended to the existing text, rather than replacing it. You will also want to clear the label2.Text before entering the for loop, and add a newline to the end of the name (or they will all run together in one big line). Other improvements you may want to do: 1) Change the label for a ListBox - it is better designed for showing lists than a label. 2) Consider using foreach rather than your existing for loop:

                         DirectoryInfo dir = new DirectoryInfo(dirname);
                          foreach (FileInfo fi in dir.GetFiles())
                              {
                              string filename = fi.FullName;
                              ...
                              }
              

              All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • T tanweer

                No sir i just try to make a file counter in my program that it------------->>>>>>>>>>>>>>> can u hepl me..........>>>>>>>>>>>>>>>

                S Offline
                S Offline
                sanforjackass
                wrote on last edited by
                #7

                for (int i = 0; i < Directory.GetFiles(textBox1.Text).Length - 1; i++) { object[] file=Directory.GetFiles(textBox1.Text+@"\"); for(int x=0;x

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups