简体   繁体   中英

How to store C# SpecialFolder path into SQLite database?

I am working on a WPF app and I am stuck in a tricky problem.

There is a TEXT field called "OutputFolder" into SQLite database which stores different folder paths (as name suggests its folder where some output is generated).

I want to save special folder path ( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) ) into SQLite database in "OutputFolder".

Actual value I want to store in database

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\temp";

I have no option to use any other directory or something else. Its very clear that I have to store this path into database.

I am open to add new column to specify any flag value or anything that identify path as special folder.

As I have understood - you need to store both types of paths absolute and one containing special folders. The problem is how to recognize a type of path and then to handle it accordingly.

If you can't change the db data structure(to add additional fields), then probably is a way to inject information about special folder into the path.

Eg you can use invalid(for path) characters to mark start and end of injection. Your path should then looks like:

var injectedPath = "?5?\\temp";

Of course on read you have to parse and if an injection being found replace it with real path. In our case MyDocuments = 5 see Environment.SpecialFolder .

First of all, Rekshino 's answer can be one of the way to achieve this.

Mean while I solved this.

As mentioned in question,

I am open to add new column to specify any flag value or anything that identify path as special folder.

Implemented Solution

DB Fields:

OutputFolder = Path of directory (static path or remaining path under special folder)

IsSpecialOutputFolder = Flag to identify where current path has any special folder or not.

SpecialFolderId = Enum value of Environment.SpecialFolder . (In our case MyDocuments = 5). I set default value to -1 as it is not value of any item in Environment.SpecialFolder enum.

See Environment.SpecialFolder for more detail for this enum .

UI:

在此处输入图片说明

Code behind info:

DropDown to give more Environment.SpecialFolder selection to user. Bind following Dictionary<int, string> to it. In future, it will allow to add more items.

public static Dictionary<int, string> AllowedSpecialFolders = new Dictionary<int, string>() 
{
    {0,"Desktop" }, {5, "MyDocuments"}
};

CheckBox check/uncheck makes DropDown enabled and disabled.

Implemented proper validation where special folder path entered and static path entered. Example: In case of special path, remaining directory path must not contain any root directory.

if (IsSpecialOutputFolder)
{
    // ALL OTHER PROCESSING AND VALIDATIONS HERE.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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