如何读取程序加载时解决方案一部分的文件? [英] How to read a file that is part of the solution on program load?

查看:79
本文介绍了如何读取程序加载时解决方案一部分的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C#WinForms应用程序,该应用程序的组件之一是SQLite数据库.

I'm writing a C# WinForms application, and one of the components of the application is a SQLite database.

如果用户是第一次运行该程序,则应假定该程序在用户的主目录中创建必要的文件夹和文件(即数据库文件).很好.

If the user is running the program for the first time, the program is supposed to create the necessary folders and files (namely, the database file) in the user's home directory. That works fine.

但是,还需要设置数据库(即,需要添加表).我有一个SQL脚本,它将创建必要的表.但是,它当前存储在解决方案目录中,我不确定这是否是将程序实际打包到.exe文件中的最佳实践.

However, the database also needs to be set up (i.e., tables need to be added). I have a SQL script that will create the necessary tables; however, it is currently stored in the solution directory and I'm not sure if this is the best practice for when the program actually gets packaged into an .exe file.

每次需要设置数据库时,脚本都是相同的,所以我认为可能有一些选择:

The script will be the same every time the database needs to be set up, so I'm thinking there are probably a few options:

  • 让程序从SQL脚本读取并将其应用到数据库(除非有更好的方法,否则首选)

  • Have the program read from the SQL script and apply it to the database (preferred unless there is a better way)

将脚本文件的内容加载到内存中(将其硬编码到string中),并让应用程序以这种方式运行(由于以后的版本而不是首选,因此需要一种更新方式)现有结构,以免破坏现有数据库,因此这种方式可能会变得很复杂)

Load the contents of the script file into memory (hard-code it into a string) and have the application run it that way (not preferred because of future versions, there needs to be a way to update the existing structure so as to not obliterate the existing database, so this way could get complicated)

将SQL脚本作为程序包或独立文件的一部分包含在内(非常危险,因为用户不应该知道这一点)

Include the SQL script as part of the program package or a standalone file (very dangerous because users aren't supposed to know about that)

那么从伴侣"脚本文件运行SQL语句的最佳方法是什么?在程序准备好投入生产时,如何打包所有这些文件?我如何确保每次需要该文件时,程序都可以对其进行访问?

So what is the best way to run SQL statements from a "companion" script file? How does all of this get packaged when the program is ready for production, and how can I ensure that this file will be accessible by the program every time it is needed?

推荐答案

您可以设置要在输出目录中复制的文件.在解决方案资源管理器中选择文件,然后在属性窗口中,设置

You can set the file to be copied in output directory. Select the file in solution explorer and then in property window, set Copy to Output Directory to Copy Always. This way the file will be copied in output directory and you can load it this way:

var path = System.IO.Path.Combine(Application.StartupPath, @"Script.txt");
var content = System.IO.File.ReadAllText(path);

如果文件位于解决方案的根目录中,请使用上述文件名.如果文件位于解决方案的文件夹中(例如,对于Folder1中的文件),请在上面的代码中使用@"Folder1\Script.txt".

If the file in root of the solution, use filename as above. If the file is in a folder in your solution, for example for a file in Folder1, use @"Folder1\Script.txt" in above code.

作为另一个选项,您可以将文件添加到Resources.resx.然后它将被包含在资源中,您可以通过以下方式简单地访问它:

As another option you can add the file to Resources.resx. Then it will be included in resources and you can simply access it this way:

var content = Properties.Resources.Script;

这篇关于如何读取程序加载时解决方案一部分的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆