gpt4 book ai didi

c# - 升级后删除以前版本的用户设置配置文件和目录 C#

转载 作者:行者123 更新时间:2023-12-05 04:05:27 26 4
gpt4 key购买 nike

我有一个将用户设置存储在标准 user.config 文件中的 C# 应用程序。

这显然位于以应用程序版本号命名的目录中,即

My_App/1.0.0.0/user.config

成功升级到我的应用程序的新版本 (1.0.0.1) 后,v1.0.0.0 的设置被复制并保存到新版本 1.0.0.1。这是简单的部分。

现在我想在升级后删除这个之前的版本目录1.0.0.0

我意识到目录的路径不能硬编码,因为位置因环境而异。

我一直在寻找使用:

using System.Configuration;
...
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
MessageBox.Show("Local user config path: {0}", config.FilePath);

它立即抛出一个错误,我需要添加:

using EnvDTE;

现在我有错误:

CS0117“ConfigurationManager”不包含“OpenExeConfiguration”的定义

CS0103 当前上下文中不存在名称“ConfigurationUserLevel”

CS1061“Configuration”不包含“FilePath”的定义,并且找不到接受“Configuration”类型的第一个参数的扩展方法“FilePath”(是否缺少 using 指令或程序集引用?)

有解决办法吗?

否则每次更新都会创建一个新目录,虽然每个目录只有 1kb,但让它们都挂载起来似乎很荒谬。

另外,我的应用程序没有安装在用户系统上,它都是直接从分布式.exe运行


更新 1


使用 EnvDTE 添加错误;根据评论删除。

CS0246 找不到类型或命名空间名称“Configuration”(是否缺少 using 指令或程序集引用?)

CS0103 当前上下文中不存在名称“ConfigurationManager”

CS0103 当前上下文中不存在名称“ConfigurationUserLevel”


更新 2


我以为我可以通过以下方式到达某个地方:

string path = Application.LocalUserAppDataPath;
MessageBox.Show(path);

相反,但这只是在我的原始路径旁边创建了一条新路径,其中有一个空的 <Program Version>目录格式为:

<Company Name>/<Program Name>/<Program Version>

在实际user.config路径 <Program Name>添加了一个哈希字符串以形成类似于:

<Program Name.exe_Url_aqod5o1hapcyjk3ku1gpyynhem0mefle>

我会假设这个散列不是一个常量,并且在机器和安装之间确实有所不同


更新 3


我终于能够检索到 user,config 的确切路径像这样的文件:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
MessageBox.Show(config.FilePath);

using System.Configuration没有工作,我没有意识到它在代码中以稍微暗淡的颜色显示,因为它没有被引用,所以我通过右键单击资源管理器中的解决方案将其添加为引用。

现在config包含 user.config 的完整路径在我运行 Windows 10 的情况下的文件:

<DRIVE LETTER>\Users\<USER NAME>\AppData\Local\<Company Name>\<Program Name>\<Program Version>\user.config

我需要操纵 config绳子并爬出 <Program Version>目录 & 到 <Program Name>目录以查看其下是否有除当前 <Program Version> 以外的任何目录目录,如果有,我需要删除它们。


更新 4


好的,现在真的有进展了。我操纵了 filePath 字符串以达到 <Program Version> 的级别 parent 并使用此代码:

string FullfilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
string ProgNamefilePath = Path.GetFullPath(Path.Combine(FullfilePath, @"..\..\"));
Directory.Delete(ProgNamefilePath, true);
//MessageBox.Show(ProgNamefilePath);//For Testing Purposes

我可以删除这个目录下的所有目录,太棒了。

不,我卡住了,因为我不想删除与 <Program Version> 有关的所有目录但只有与当前版本不匹配的版本。

Application.ProductVersion;

最佳答案

一个不眠之夜思考这个问题并尝试考虑其他途径,我终于想到了这个并且它有效。

可能有更简洁的方法来做到这一点,但无论如何......

我可以将它放在代码的最高位置,以便在编码升级时希望对我自己来说非常明显,我把它放在:

public class MainForm : Form
{
//***********************************
//***********************************
//**** THIS RELEASE IS v1.0.0.1 ****
//***********************************
//****** REMEMBER TO EDIT THIS ******
//***********************************
const string DeleteThisVersionDirectory = "1.0.0.0"; //**** Set this to the <Program Version> Directory to be Deleted on Upgrade, usually version prior to new release. ****
//***********************************
//***********************************

您可以看到它直接位于 Form 下可见性打开。

然后我创建了这个方法:

private void ApplicationUpdate()
{
string FullfilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
string VersionDirectoryfilePath = Path.GetFullPath(Path.Combine(FullfilePath, @"..\..\" + DeleteThisVersionDirectory));
if (Properties.Settings.Default.UpdateSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpdateSettings = false;
Properties.Settings.Default.Save();
Directory.Delete(VersionDirectoryfilePath, true);
}
}

这个方法在这里调用:

public MainForm()
{
InitializeComponent();
ApplicationUpdate();
GetSettingsValues();
}

一切正常,所以在成功升级之后,用户设置被转移到新程序版本的user.config,以前的程序版本目录和user.config 文件被删除。

我只需要记住在每个版本更新编码中更改 const string DeleteThisVersionDirectory

关于c# - 升级后删除以前版本的用户设置配置文件和目录 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51138693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com