问题描述
大家好,
我的程序需要在某处编写配置文件和一些数据文件。我知道将文件写入程序的.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.
推荐答案
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)
这篇关于我的程序在哪里可以编写配置和数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!