简体   繁体   English

开发过程C#桌面应用程序中Data文件夹的正确位置在哪里

[英]where is the correct location for Data folder in developing process C# deskTop app

i write desktop app in C# .... this app will auto generate an C/C++ code for an embededd system project我用C#编写桌面应用程序......这个应用程序将自动为嵌入式系统项目生成 C/C++ 代码
so i have to copy some pre_writen drivers to the target folder (where the file is generated)所以我必须将一些 pre_writen 驱动程序复制到目标文件夹(生成文件的位置)

the problem is i don't know where i can correctly put the sourc driver in !!问题是我不知道在哪里可以正确地放置源驱动程序!!

for now i put the source driver in in the project foldr and i refrance it in my code lik现在我将源驱动程序放在项目文件夹中,然后在我的代码中引用它

            // projectfolder\bin\Debug\netcoreapp3.1\file.exe
            string path = (Assembly.GetExecutingAssembly().Location);
            DirectoryInfo exeDir = new DirectoryInfo(path);
            DirectoryInfo projectDir = exeDir.Parent.Parent.Parent.Parent;
            // now i can get the driver foler like this
            string DriverPath = Path.Combine(projectDir.fullName,"drivers");

i guss that folder in the product level will be in C:\\ProgramData but for developing where the location shoud be in ?我认为产品级别的文件夹将在C:\\ProgramData但用于开发位置应该在什么位置?

ac# dekstop app 在Environment.CurrentDirectory + "\\\\Folder"的默认位置

This is mostly a question of how often the data will change.这主要是数据更改频率的问题。 And how that will interact with the very strict management of write rights since NTFS became common (While the rule predates NTFS, with mostly FAT - and thus no rights to stop us - few programmers cared).以及自从 NTFS 变得普遍以来,这将如何与非常严格的写权限管理相互作用(虽然该规则早于 NTFS,但主要是 FAT - 因此没有权利阻止我们 - 很少有程序员关心)。

You can and even should put stuff into the Programm directory itself, if it does not change outside of Programm Updates/Reinstallations.如果在程序更新/重新安装之外没有更改,您可以甚至应该将内容放入程序目录本身。 If it only changes with a Installer or Updater - things that need administrative rights for their work anyway - it belong there.如果它只随安装程序或更新程序发生变化——无论如何都需要对其工作进行管理权限的东西——它就属于那里。

If changes happen more often then that - especially stuff that can change at runtime or even needs to change at runtime - where the programm folder becomes unuseable.如果更改发生得更频繁,那么程序文件夹将变得不可用,尤其是那些可以在运行时更改甚至需要在运行时更改的内容。 In this case the programm directory should at tops include a template file.在这种情况下,程序目录应该在顶部包含一个模板文件。 Something you can copy to the real directory as a baseline, to have something to start working with.您可以将某些内容复制到真实目录作为基线,以便开始使用。 As for where to put this changeable data, look at the SpecialFolders Enumeration .至于将这些可变数据放在哪里,请查看SpecialFolders Enumeration The Userprofile - particulary Appdata folders are the point to look at. Userprofile - 特别是 Appdata 文件夹是要查看的点。 Wich specific one to use is a question of how you want it to interact with Domain Controler Synchronisation (Roaming vs non-Roaming user).使用哪个特定的问题是您希望它如何与域控制器同步(漫游与非漫游用户)进行交互。 Or if you want it to end up in Common.或者,如果您希望它以 Common 结尾。

Drivers sound like that rare case, where the programm directory is entirely fitting.驱动程序听起来像那种罕见的情况,其中 programm 目录完全合适。 Optionally you could have the drivers installed as a parallel application.您可以选择将驱动程序安装为并行应用程序。 Something that can be managed/updated seperately and is simply a requirement for your programm.可以单独管理/更新的东西,只是您的程序的要求。 Something that might be shared across multiple applications.可能在多个应用程序之间共享的东西。

In Windows there are different types of data that may be consumed by an Application such as

  • Application Properties应用程序属性

    Application settings enable you to store application information dynamically.应用程序设置使您能够动态存储应用程序信息。 Settings allow you to store information on the client computer that should not be included in the application code.设置允许您在客户端计算机上存储不应包含在应用程序代码中的信息。

    Ex: Position, Font, Styles, User Configuration例如:位置、字体、样式、用户配置
    File Type: App.Config(Is created at design time and is by default put into BIN folder) and User.Config (Created at run time)文件类型: App.Config(在设计时创建,默认放入 BIN 文件夹)和 User.Config(在运行时创建)
    Project > Add New Item > Installed > Visual C# Items > Application Configuration File项目 > 添加新项 > 已安装 > Visual C# 项 > 应用程序配置文件


  • Application Data应用数据

    Contains all the data, settings, and user files that are required by the installed software and UWP apps.包含已安装软件和 UWP 应用所需的所有数据、设置和用户文件。 These are user agnostic data.这些是用户不可知的数据。

    Ex: Images, Resources, Sprites, Data Templates, txt files.例如:图像、资源、精灵、数据模板、txt 文件。 It may also contain App.Config files它还可能包含 App.Config 文件

    File Location: C:\\ProgramData Environment.SpecialFolder.CommonAppData文件位置: C:\\ProgramData Environment.SpecialFolder.CommonAppData


  • User Data用户数据

    Contain all data that is user specifc, such as his/her app specific settings.包含用户特定的所有数据,例如他/她的应用程序特定设置。

    Ex: Any type of data例如:任何类型的数据

    File Location: C:\\Users\\[USERNAME]\\AppData\\Roaming Environment.SpecialFolder.AppData or Environment.SpecialFolder.MyDocuments文件位置: C:\\Users\\[USERNAME]\\AppData\\Roaming Environment.SpecialFolder.AppDataEnvironment.SpecialFolder.MyDocuments


i guss that folder in the product level will be in C:\\ProgramData but for developing where the location shoud be in ?我认为产品级别的文件夹将在 C:\\ProgramData 但用于开发位置应该在什么位置?

yes, it will go to ProgramData, but during the development you can have it in your BIN folder or a directory of your choice if it matters是的,它会转到 ProgramData,但在开发过程中,您可以将它放在 BIN 文件夹或您选择的目录中(如果重要的话)

However most of the above will come into picture when you package your App to an MSI and bundle in all the dependency files and where they need to go.然而,当您将应用程序打包到 MSI 并捆绑所有依赖文件以及它们需要去的地方时,上述大部分内容都会出现。



EDIT编辑

Based on the OPs clarification in comments, .CPP files will automatically go into the BIN/Debug Folder of your C# app.根据注释中的 OP 说明,.CPP 文件将自动进入 C# 应用程序的BIN/Debug文件夹。
You will need to dynamically place your device drives in the directory where your exe is running您需要将设备驱动器动态放置在运行 exe 的目录中

To access the directory where your app is running :要访问您的应用程序运行的目录:

string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location Output of Path = Absolute path where your exe is running C:\\Users\\User1\\source\\repos\\Test\\Test\\bin\\Debug\\Test.exe string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location Path 的输出 = exe 运行的绝对路径C:\\Users\\User1\\source\\repos\\Test\\Test\\bin\\Debug\\Test.exe

string exeDirectory = Path.GetDirectoryName(exePath); The Directory path where your exe is situated C:\\Users\\User1\\source\\repos\\Test\\Test\\bin\\Debug您的 exe 所在的目录路径C:\\Users\\User1\\source\\repos\\Test\\Test\\bin\\Debug

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM