简体   繁体   English

如何将C#SpecialFolder路径存储到SQLite数据库中?

[英]How to store C# SpecialFolder path into SQLite database?

I am working on a WPF app and I am stuck in a tricky problem. 我正在使用WPF应用程序,但遇到了棘手的问题。

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). SQLite数据库中有一个名为“ OutputFolder”的TEXT字段,该字段存储不同的文件夹路径(顾名思义,该文件夹会在其中生成一些输出的文件夹)。

I want to save special folder path ( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) ) into SQLite database in "OutputFolder". 我想将特殊文件夹路径( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) )保存到“ OutputFolder”中的SQLite数据库中。

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. 如果您不能更改db数据结构(以添加其他字段),则可能是一种将有关特殊文件夹的信息注入路径的方法。

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 . 在本例中,MyDocuments = 5参见Environment.SpecialFolder

First of all, Rekshino 's answer can be one of the way to achieve this. 首先, Rekshino的答案可能是实现这一目标的方法之一。

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) OutputFolder =目录路径(静态路径或特殊文件夹下的剩余路径)

IsSpecialOutputFolder = Flag to identify where current path has any special folder or not. IsSpecialOutputFolder =标识当前路径在何处有特殊文件夹的标志。

SpecialFolderId = Enum value of Environment.SpecialFolder . SpecialFolderId = Environment.SpecialFolder枚举值。 (In our case MyDocuments = 5). (在我们的示例中,MyDocuments = 5)。 I set default value to -1 as it is not value of any item in Environment.SpecialFolder enum. 我将默认值设置为-1,因为它不是Environment.SpecialFolder枚举中任何项目的值。

See Environment.SpecialFolder for more detail for this enum . 有关此enum更多详细信息,请参见Environment.SpecialFolder

UI: 用户界面:

在此处输入图片说明

Code behind info: 信息背后的代码:

DropDown to give more Environment.SpecialFolder selection to user. DropDown为用户提供更多Environment.SpecialFolder选择。 Bind following Dictionary<int, string> to it. 将以下Dictionary<int, string>绑定到它。 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. CheckBox选中/取消选中将启用和禁用DropDown

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

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

相关问题 如何将Environment.SpecialFolder路径存储为字符串,然后将其解析回Environment.SpecialFolder? - How can I store an Environment.SpecialFolder path as a string and then parse it back to Environment.SpecialFolder? 如何在C#中指定相对的SQLite数据库路径? - How to specify a relative SQLite database path in C#? 如何使用C#将图像路径存储在mysql数据库中 - how to store image path in mysql database using C# 如何在Environment.SpecialFolder.ProgramFiles C#中转义空间 - how to escape the space in Environment.SpecialFolder.ProgramFiles C# SpecialFolder。个人现有的sqlite数据库 - SpecialFolder.Personal existing sqlite database 使用C#管理Windows Store应用程序的SQLite数据库 - Managing SQLite Database for Windows Store Apps using C# 如何使用C#在/从SQLITE数据库中存储和检索pdf文件? - How to store and retrieve pdf files into/from SQLITE database using C#? 通过C#访问sqlite数据库时相对路径不起作用 - Relative path not working while accessing a sqlite Database through C# 构建相对路径连接字符串以访问c#中的SQLite数据库 - Building a relative path connection string to access a SQLite database in c# 如何在C#中解决SQLite“数据库已锁定”? - How to solve the SQLite “Database is locked” in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM