2

我有一个将用户设置存储在标准 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“配置”不包含“文件路径”的定义,并且找不到接受“配置”类型的第一个参数的扩展方法“文件路径”(您是否缺少使用指令或程序集引用?)

有没有解决的办法?

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

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


更新 1


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

CS0246 找不到类型或命名空间名称“配置”(您是否缺少 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>父级并使用以下代码:

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

2 回答 2

2

一个不眠之夜想着这个并试图考虑其他途径,我终于想出了这个并且它有效。

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

在我可能放置的代码中尽可能高的位置,以便在编码升级时希望它对我自己非常明显,我放置了这个:

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

这一切都完美无缺,因此在成功Upgrade将用户设置传输到user.config新程序版本后,之前的程序版本目录和user.config文件将被删除。

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

于 2018-07-03T07:54:21.777 回答
0

我就是这样做的。升级后,我删除了除实际文件夹外的所有 user.config 文件夹。因此我不需要在顶部手动添加当前版本。希望有帮助。我知道这篇文章有点老了,但它可能会对某人有所帮助。在我的应用程序中,我分 3 部分执行此操作以使其正常工作。

a) UI 上的全局变量

bool DeleteOldDirectories = false;

b) 在我的 Main() 中,在 UI 之前我升级属性并将它们保存在我的新版本中

if (Settings.Default.UpgradeRequired)
            {
                Settings.Default.Upgrade();
                Settings.Default.UpgradeRequired = false;
                Settings.Default.Save();
                DeleteOldDirectories = true;
            }

在 Window.Loaded 方法中,在我的 UI 加载并且我确定所有属性都已正确更新后,我确实删除了旧目录,因此从现在开始的所有更改都使用我的最新版本保存,并且所有对旧版本的引用都消失了.

 if (deleteOldDirectories)
        {
            string FullfilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
            string VersionDirectoryfilePath = System.IO.Path.GetFullPath(System.IO.Path.Combine(FullfilePath, @"..\..\"));
            string[] fileEntries = Directory.GetDirectories(VersionDirectoryfilePath);
            foreach (string filename in fileEntries)
            {
             //   MessageBox.Show(filename);
                if (filename != VersionDirectoryfilePath + System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString())
                    Directory.Delete(filename, true);
            }
        }
于 2020-11-10T11:14:16.320 回答