【问题标题】:How to Create a file and folder in the Program Files using C#如何使用 C# 在 Program Files 中创建文件和文件夹
【发布时间】:2014-06-01 18:57:42
【问题描述】:

我有一个使用 c# 创建的程序,该程序将驻留在 c:\program files 文件夹中。 如果我要运行这部分,我会得到错误

SQLiteConnection.CreateFile("db/MyDatabase.sqlite");

我已经在 http://blogs.windows.com/windows/archive/b/developers/archive/2009/08/04/user-account-control-data-redirection.aspx 阅读了有关用户帐户控制的内容,但我真的不知道如何去做..

我的程序将在 C:\program files\my program\db\MyDatabase.sqlite 中

【问题讨论】:

  • 尝试以管理员身份运行您的程序。
  • 我愿意.. 但我总是得到一个错误.. 它说类似“收集错误......”
  • 为什么不使用 Windows 为这类东西提供的文件夹呢? %APPDATA% 和文档?
  • 不要说“类似”,而是将the exact error message复制并粘贴到您的问题中。
  • Program Files 是存储数据的最差目录。 stackoverflow.com/questions/5116911/…

标签: c#


【解决方案1】:

尝试使用 AppData 文件夹 - 无论如何您都不应该将此类内容保存在 ProgramFiles 中:

var dir = Path.Combine(Environment
    .GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyProgram");
if(!Directory.Exists(dir))
    Directory.CreateDirectory(dir);
SQLiteConnection.CreateFile(Path.Combine(dir, "MyDatabase.sqlite"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-21
    • 2012-08-17
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    相关资源
    最近更新 更多