Gaurang Dave
Gaurang Dave

Reputation: 4046

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.

Upvotes: 0

Views: 224

Answers (2)

Gaurang Dave
Gaurang Dave

Reputation: 4046

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:

enter image description here

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.
}

Upvotes: 0

Rekshino
Rekshino

Reputation: 7325

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.

E.g. 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.

Upvotes: 1

Related Questions