开发者

C# listbox to file listing

What I have is a listbox being populated by a textbox.

I want to search in a specific directory for all files that match the listbox criteria. I want do do this for each listing in the listbox, then I want to copy out all the matching files to another directory.

So Listbox contains: Apple Orange Fruit

i want to copy apple*.txt to destiondirectory, then copy orange*.txt to destination directory, and fruit*.txt to destinationdirectory.

After everything has been copied i want to create a text file of each thing being copied to it's own text file. So a directory listing from the destinationdirectory.

So i would just get a text file of all the files that match a specific criteria IE apple*

Thanks for the help and advice.

   string[] filesToCopy = listBox1.Items.
        string sourcefolder1 = @"K:\rkups";
        string destinationfolder = @"K:\g_aa_ge\qc";
        {
            strin开发者_如何转开发g source = Path.Combine(sourcefolder1, filesToCopy[] + ".ann");
            string target = Path.Combine(destinationfolder, filesToCopy[] + ".ann");
            File.Copy(source,target);

 DirectoryInfo di = new DirectoryInfo(destinationfolder);
        FileInfo[] annfiles = di.GetFiles(string+"*.txt);
        foreach(FileInfo fi in annfiles)

the string+ is where i dont understand where/how to list each item in the listbox, and where string[] filesToCopy = listBox1.Items. not sure how to list each item in the string

updated: 1) read each item in listbox

2) try to copy from a sourcedirectory to a destinationdirecory the item in listbox

3) repeat

thats it


I made a small example which is doing more or less what you wanted except generateing the log file.

You should be able to work it from there.

In my example, the code was just populating a second text box with the names of the copied files. It was tested and compiled.

Hope this helps ! Anthony

private void button1_Click(object sender, EventArgs e)
{
    string dirInput = "c:/test";
    string dirOutput = "c:/test2";
    listBox2.Items.Clear();

    bool overwriteFilesInOutputDir = true;

    if (Directory.Exists(dirInput))
    {
        if (!Directory.Exists(dirOutput))
            Directory.CreateDirectory(dirOutput);

        DirectoryInfo di = new DirectoryInfo(dirInput);
        foreach (string filterItem in listBox1.Items)
        {
            FileInfo[] rgFiles = di.GetFiles(filterItem);
            foreach (FileInfo fi in rgFiles)
            {
                File.Copy(fi.FullName, dirOutput + Path.DirectorySeparatorChar + fi.Name, overwriteFilesInOutputDir);
                listBox2.Items.Add(fi.Name);
            }
        }
    }
}

Like other people mentioned, it would help if you would try to do it yourself first and ask when you are stuck.

listBox1 contains the filters such as ".xls" or ".asp", listBox2 was just for me to check the names of the files copied.

Anthony


I'm still a little confused on what you want to do, but I fixed up your code for you...

string source, fileToCopy, target;
string sourcefolder1 = @"K:\rkups";
string destinationfolder = @"K:\g_aa_ge\qc";
DirectoryInfo di = new DirectoryInfo(destinationfolder);
FileInfo[] annfiles;

foreach (string s in listBox1.Items)
{
     fileToCopy = s;
     source = Path.Combine(sourcefolder1, fileToCopy + ".ann");
     target = Path.Combine(destinationfolder, fileToCopy + ".ann");
     File.Copy(source, target);

     annFiles = di.GetFiles("*.txt");

     // Do whatever you need to do here...

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜