Cloudz
Cloudz

Reputation: 11

Checklistbox to open files

I have created something that grabs all file names that have the extension .lua with them. This will then list them in a CheckListBox. Everything goes well there but I want to know which one of the CheckListBox's are ticked/checked and then open them in notepad.exe. To dynamically add the files Code (works perfectly, and adds the files i want)

       string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string path = appData + "\\Lua";
        string[] fileArray = Directory.GetFiles(path, "*.lua");
        for (int i = 0; i < fileArray.Length; i++)
        {
            string Name = Path.GetFileName(fileArray[i]);
            string PathToLua = fileArray[i];
            ScriptsBoxBox.Items.AddRange(Name.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
            Console.WriteLine(fileArray[i]);
        }

Then when i check the items i want to open in notepad i use `

System.Diagnostics.Process.Start("notepad.exe", ScriptsBoxBox.CheckedItems.ToString());

Or

System.Diagnostics.Process.Start("notepad.exe", ScriptsBoxBox.CheckedItems);

Neither works and im pretty sure it's on my end. So my problem is that i cannot open the file that is ticked/checked in checklistbox and want to resolve this problem. However when I do

System.Diagnostics.Process.Start("notepad.exe", PathToLua);

It opens the files with .lua extension ticked or not which makes sense.

Upvotes: 1

Views: 100

Answers (3)

Cloudz
Cloudz

Reputation: 11

Thanks everyone that helped but I solved it on my own (pretty easy when you read :P) For anyone in the future that wants to do this here is how i accomplished it.

        string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string path = appData + "\\Lua";
        string[] fileArray = Directory.GetFiles(path, "*.lua");
        for (int i = 0; i < fileArray.Length; i++)
        {
            string Name = Path.GetFileName(fileArray[i]);
            string PathToLua = fileArray[i];
            //ScriptsBoxBox.Items.AddRange(Name.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
            // Console.WriteLine();
            Console.WriteLine(ScriptsBoxBox.CheckedItems.Contains(Name));
            var pathname = ScriptsBoxBox.CheckedItems.Contains(Name);
            if (ScriptsBoxBox.CheckedItems.Contains(Name))
            {
                System.Diagnostics.Process.Start("notepad.exe", fileArray[ScriptsBoxBox.CheckedItems.IndexOf(Name)]); // I supposed this would get the correct name index, and it did! fileArray by default seems to get the path of the file.
            }

Upvotes: 0

SILENT
SILENT

Reputation: 4268

I don't know WinForms as well as WPF but here goes

You need an object that contains your values

    public class LuaFile
    {
        public string FileName { get; set; }
        public string FilePath { get; set; }
        public LuaFile(string name, string path)
        {
            FileName = name;
            FilePath = path;
        }
        public override string ToString()
        {
            return FileName;
        }
    }

Replace your for loop with

            foreach (var file in files)
            {
                ScriptsBoxBox.Items.Add(new LuaFile(Path.GetFileName(file), file));
            }

And to run the checked files

            foreach (var file in ScriptsBoxBox.CheckedItems)
            {
                System.Diagnostics.Process.Start("notepad.exe", ((LuaFile)file).FilePath);
            }

Upvotes: 0

Cyral
Cyral

Reputation: 14153

I don't think there are any arguments that you can pass to notepad to open a list of specific files. However, you can use a loop to open each file.

foreach (var file in ScriptsBoxBox.CheckedItems)
{
    System.Diagnostics.Process.Start("notepad.exe", file);
}

Upvotes: 2

Related Questions