Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Общие вопросы по .NET и C# > GetFiles


Автор: Barabeo 4.12.2011, 17:25
Почему выводит только один файл?
Код

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace FormsGetFiles
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo("c:\\Windows");
            FileInfo[] fiArr = di.GetFiles();
            foreach (FileInfo fri in fiArr)
                textBox1.Text = fri.Name;
        }
    }
}


Автор: Fortnox 4.12.2011, 23:16
Цитата(Barabeo @  4.12.2011,  17:25 Найти цитируемый пост)
textBox1.Text = fri.Name;

Вы заносите в текстовое поле имя последнего файла.

Если надо вывести все файлы на строку:
Код

DirectoryInfo di = new DirectoryInfo("c:\\Windows");
            FileInfo[] fiArr = di.GetFiles();
            foreach (FileInfo fri in fiArr)
                textBox1.Text += string.Format("{0}{1}", fri.Name, Environment.NewLine);

Автор: Barabeo 5.12.2011, 23:09
Цитата(Fortnox @ 4.12.2011,  23:16)
Цитата(Barabeo @  4.12.2011,  17:25 Найти цитируемый пост)
textBox1.Text = fri.Name;

Вы заносите в текстовое поле имя последнего файла.

Если надо вывести все файлы на строку:
Код

DirectoryInfo di = new DirectoryInfo("c:\\Windows");
            FileInfo[] fiArr = di.GetFiles();
            foreach (FileInfo fri in fiArr)
                textBox1.Text += string.Format("{0}{1}", fri.Name, Environment.NewLine);

ОГромное спасибо!

Автор: Felixx 12.12.2011, 13:47
Вот ещо вариант.. кусок моего кода.. 

Код

            String path = "data\\items";

            if (Directory.Exists(path))
            {
                String[] allfiles = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories);
                for (int i = 0; i < allfiles.Length; i++)
                {
                    parse(allfiles[i]);
                }
            }
            else
            {
                CLogger.info("No Have Dir: " + path);
            }

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)