Hi, I've been working on this mini project on a WFA. Basically what I'm trying to do is, move files through a checkedListBox. I have the c.listbox and i got the items on it, but I want to have the files moved (only the checked ones) into a pre-defined directory and opened on the click of a button. I've tried an assortment of methods of doing this, but I just can't seem to get the path of the items that have been 'checked'.

I've used the FileInfo class to get the files into the checkedlistbox, because I'm looking only for the .vpk files:

private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(dDir);
            FileInfo[] vpkFiles = di.GetFiles("*.vpk");
            foreach (FileInfo fi in vpkFiles)
            {
                checkedListBox1.Items.Add(fi.Name);
            }
        }

I've tried a bunch of things to get the path of the items, but this is where I left off, I tried doing the same for this part, and I even tried using Path.GetDirectories of the item, but it only seems to bring me to the path of the 'Bin' folder:

foreach (object itemChecked in checkedListBox1.CheckedItems)
            {
                string P = Path.GetFullPath(itemChecked.ToString());
                DirectoryInfo di = new DirectoryInfo(P);
                FileInfo[] maps = di.MoveTo(dDir);
            }

Recommended Answers

All 2 Replies

I don't believe you're going to be able to easily get the path of a file just by having its file name. That's what Search does. The first thing that popped up in my head was to create a dictionary. Make the key be the file's name and make the value the full path to the file.

You'll need to add to the dictionary while you have the file's FileInfo by using its Name (just the file's name) and FullName (the full path to it) properties. Then in that second foreach you'll need to look up the path from the dictionary.

Here's your code edited to do the above. I can not test it right now.

Your first block.

public Dictionary<string, string> FileDict = new Dictionary<string, string>(); //make a new dictionary, with its key being a string and value being a string

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(dDir);

            FileInfo[] vpkFiles = di.GetFiles("*.vpk");
            foreach (FileInfo fi in vpkFiles)
            {
                FileDict.Add(fi.Name, fi.FullName); //assign key as the file's name, and value as the full path to it.
                checkedListBox1.Items.Add(fi.Name);
            }
        }

Second block.

foreach (object itemChecked in checkedListBox1.CheckedItems)
            {
                string P = FileDict[itemChecked.ToString()]; //lookup the value of the current item
                DirectoryInfo di = new DirectoryInfo(P);
                FileInfo[] maps = di.MoveTo(dDir); //MoveTo doesn't return FileInfo[] :)
            }

Ah thanks for replying, I'm pretty new to C# still, I'll try this out =]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.