How to read a file that is part of the solution on program load?

后端 未结 2 1532
误落风尘
误落风尘 2021-01-22 03:40

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 pro

相关标签:
2条回答
  • 2021-01-22 04:02

    Include an encrypted version of the sql file. Have your program load, decrypt it then execute it. At least that's how I'd do it.

    0 讨论(0)
  • 2021-01-22 04:15

    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);
    

    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.

    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;
    
    0 讨论(0)
提交回复
热议问题