简体   繁体   English

C#-在设置中存储多个目录并将其加载到列表框中

[英]C# - Storing multiple directories in settings and loading them into listbox

I currently have a winform with a listbox control that allows a user to browse directories and add them to the listbox. 我目前有一个带有列表框控件的winform,该控件允许用户浏览目录并将其添加到列表框中。 These directory locations will then be saved using the application settings file (as a string?), but I'm not sure how I should separate each directory string. 然后将使用应用程序设置文件(作为字符串?)保存这些目录位置,但是我不确定如何分隔每个目录字符串。 Upon reloading of the form, I would like all the directories to be loading into the listbox as individual items from the settings file. 重新加载表单后,我希望所有目录都作为设置文件中的单个项加载到列表框中。

So I technically have two questions: 所以我从技术上有两个问题:

  1. What would be the most efficient way to save multiple locations within the application settings file? 在应用程序设置文件中保存多个位置的最有效方法是什么?

  2. How would I go about loading the directories into the listbox from the settings? 如何将目录从设置加载到列表框中? (Remember, there's multiple directories) (请记住,有多个目录)

My idea was to store all the directories in one string setting and separate them with a comma, but I'm not sure if there's a more efficient method to do this. 我的想法是将所有目录存储在一个字符串设置中,并用逗号分隔它们,但是我不确定是否有更有效的方法来执行此操作。

NO!!!! 没有!!!! NEVER DO THAT!!!! 绝对不要做!!!! Comma can be included in folder name and if you use comma as separator it could bring many critical issues to your program. 逗号可以包含在文件夹名称中,如果使用逗号作为分隔符,可能会给程序带来许多关键问题。

you can use characters that are illegal in folder names in windows. 您可以在Windows的文件夹名称中使用非法字符。 \\ / : * ? " < > |

also you can not use \\ and / because path maybe stores as c:\\\\data\\\\ and using '\\' as separator is bad, and also they maybe stored as c:/data/ so using / is risky. 您也不能使用\\/因为路径可能存储为c:\\\\data\\\\并且使用'\\'作为分隔符是不好的,并且它们也可能存储为c:/data/所以使用/有风险。 I think using * or | 我认为使用*| maybe a good idea both because of they are illegal and they cannot be anywhere of a folder path and the string that has some folder paths concatenated is readable and can simply being recognized by see * or | 也许是一个好主意,因为它们是非法的,并且它们不能位于文件夹路径的任何位置,并且具有某些文件夹路径的字符串是可读的,并且可以简单地由see *|识别| as separator. 作为分隔符。

    String[] paths = s.Split(new char[]{'*'}, StringSplitOptions.RemoveEmptyEntries);
    listBox1.Items.AddRange(paths);

where s is string of concatenated folder paths you have read from setting file. 其中s是您从设置文件中读取的串联文件夹路径的字符串。 also you can use 你也可以用

        String[] paths = s.Split(new char[]{'*'}, StringSplitOptions.RemoveEmptyEntries);
        listBox1.DataSource = paths;

You could do that, and then do a string.Split on the comma, and set the listbox's source to the result of the split. 您可以这样做,然后创建一个字符串。在逗号上分割,然后将列表框的源设置为分割的结果。

string settings = GetFromConfig();
var items = settings.Split(',');
listBox1.DataSource = items;

You can use a StringCollection in the settings file. 您可以在设置文件中使用StringCollection

Once in a collection you can just put them back into a List<String> and then add the items back via ListBox.Items property, specifically the Add method. 进入集合后,您只需将它们放回List<String> ,然后通过ListBox.Items属性(特别是Add方法)将这些项添加回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM