我的程序在哪里可以编写配置和数据文件

编程入门 行业动态 更新时间:2024-10-26 18:17:51
本文介绍了我的程序在哪里可以编写配置和数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我的程序需要在某处编写配置文件和一些数据文件。我知道将文件写入程序的.EXE路径(将在C:\Program Files中)可能会导致与权限相关的问题。所以,我这样做: ProgramDataPath = System.Windows.Forms.Application.CommonAppDataPath ProgramDataPath是我程序中的全局字符串,每当函数想要读/写配置和数据文件时,它都会使用它作为基本路径。 好吧,我的第一个问题是:这是正确的吗?这是我的程序编写这些文件的正确位置吗?我不希望它们是特定于用户的。我希望它们适合所有用户。 假设它是正确的地方,这条路径总是这样: C:\ ProgramData \ MyCompany \ MyProgram\Version \ 因此文件将始终写在程序的版本号。但是如果我想将这些文件用于我程序的所有版本呢?我可以简单地编辑字符串并从中删除Version \,并直接在C:\ProgramData \ MyCompany \ MyProgram \下写下所有内容吗?这有什么问题吗? 提前致谢! 我试过的: 我尝试将文件写入System.Windows.Forms.Application.CommonAppDataPath并且它一直运行良好。但是,它们是根据程序的版本号编写的。

Hello all, My program needs to write configuration files and some data files somewhere. I know that writing files to my program's .EXE path (which will be within C:\Program Files) can cause issues related to permissions. So, I'm doing this: ProgramDataPath = System.Windows.Forms.Application.CommonAppDataPath ProgramDataPath is a global string within my program, and whenever a function wants to read/write configuration and data files, it'll use this as the base path. Well, my first question is: Is this correct? Is that the right place for my program to write those files? I don't want them to be user-specific. I'd like them to be for all users. Assuming that it is the right place, this path is always like this: C:\ProgramData\MyCompany\MyProgram\Version\ And so the files will always be written under the program's version number. But what if I want to use those files for all versions of my program? Can I simply edit the string and remove the "Version\" from it, and write everything directly under C:\ProgramData\MyCompany\MyProgram\ ? Are there any problems with that? Thanks in advance! What I have tried: I've tried writing files to System.Windows.Forms.Application.CommonAppDataPath and it has been working great. However, they're being written under the program's version number.

推荐答案

我建​​议你阅读:我应该在哪里存储我的数据? [ ^ ] I'd suggest to read this: Where should I store my data?[^]

要解决您的问题,您可以使用此: 使用您选择的名称在'C:\ProgramData \'文件夹中创建您自己的'数据文件夹': To solve your problem you can use this: create your 'own' data folder in the 'C:\ProgramData\' folder with a name of your choice: Dim MyDataFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\FolderName\"

在您的应用程序中创建一次数据文件夹:

create once in your application the data folder:

Directory.CreateDirectory(MyDataFolder)

并用以下内容保存数据:

and save you data with something like:

File.WriteAllText(MyDataFolder + "Mydata.inf", MyData)

更多推荐

我的程序在哪里可以编写配置和数据文件

本文发布于:2023-11-07 22:26:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   程序   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!