如何访问 WPF 项目中的本地文件夹以加载和存储文件?

How can I access a local folder inside a WPF project to load and store files?

我需要一个矢量文件库,每次都必须使用相同的文件。我想从文件夹中加载它们并可以选择存储新的。

我尝试在包含以下文件的 WPF 项目中创建一个库文件夹: Solution/Project/Library/file1.dxf 我这样加载它们:

string currentDir = Directory.GetCurrentDirectory();
var cutOff = currentDir.LastIndexOf(@"\bin\");
var folder = currentDir.Substring(0, cutOff) + @"\Library\";

string[] filePaths = Directory.GetFiles(folder, "*.dxf");

这在 运行 在 PC 上构建项目时有效,但是当 .exe 在另一台 PC 上 运行 时程序崩溃。我该如何解决这个问题或者是否有更好的方法?

Environment.SpecialFolder.ApplicationData下创建一个子文件夹,如果有library文件夹,则读取其中的文件。如果不创建它并将现有的库文件保存到它(这里来自资源):

string appFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string path = appFolder + @"\MyAppLibrary\";

if (!Directory.Exists(path))
{
    Directory.CreateDirectory(path);

    // Add existing files to that folder
    var rm = Properties.Resources.ResourceManager;
    var resSet = rm.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
    foreach (var res in resSet)
    {
        var entry = ((DictionaryEntry)res);
        var name = (string)entry.Key;
        var file = (byte[])rm.GetObject(name);

        var filePath = path + name + ".dxf";
        File.WriteAllBytes(filePath, file);
    }
}

// Load all files from the library folder
string[] filePaths = Directory.GetFiles(path, "*.dxf");

谢谢Jonathan Alfaro and Clemens