c# - Application.UserAppDataPath 奇怪的行为

标签 c# winforms assemblyinfo

当我使用 Application.UserAppDataPath 访问配置时,出现 ArgumentException“路径中存在非法字符。”
据微软称is not a bug, but a feature;

The only way how you can get an illegal character in FileVersionInfo.ProductVersion (and hence in Application.ProductVersion and Application.UserAppDataPath) is when you also have AssemblyFileVersion on the assembly. The value that goes to AssemblyFileVersion is copied to the Win32 resources verbatim and overrides the value you pass to AssemblyVersion. This behavior is by design.

因此,通过在 AssemblyInfo.cs 中注释 [程序集:AssemblyFileVersion("...")] 可以解决此异常。

问题是: 我正在用 winforms 控件编写一些 .dll 。获得此路径的最简单设计是使用 Application.UserAppDataPath。但如果使用 Application.UserAppDataPath,则使用此库的开发人员无法为其 .exe 文件提供 AssemblyFileVersion。 (默认情况下,AssemblyFileVersion 位于 AssemblyInfo.cs 中)

此外,我找不到 Microsoft 的任何信息表明使用 AssemblyFileVersion 会阻止我的应用程序,并且其他人不应该使用它。所以实际上我不能向这个图书馆的用户询问。

这种奇怪的情况有道理吗? 有什么更好的解决方法可以获取没有此类问题的 Application.UserAppDataPath 路径?

虽然我有 AssemblyFileVersion 和 Application.UserAppDataPath 会抛出异常,但应用程序可以正常工作,我可以通过此路径

string path = ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;

配置文件在那里并且没有问题。但可以肯定的是,这是一种丑陋的方式。

最佳答案

如果我正确阅读了 Connect 上的那篇文章,您必须将 AssemblyFileVersion 与非法字符 ('*') 一起使用。

适本地以“如果您仍然相信这是一个错误”结尾......

您是否重现了该错误,这是一个现实的场景吗?

关于c# - Application.UserAppDataPath 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6091093/

相关文章:

c# - RSA 加密返回不同的输出

c# - 在 C# 中,如何删除被另一个进程锁定的文件?

C# WPF 窗口在 foreach 循环中不更新

c# - X509Chain.Build()方法解释

c# - 验证 TextBox 中的文本更改

c# - 最佳实践 bundle 产品版本控制(装配信息)

来自 AssemblyInfo 版本的 Git 标签

.net - Application.Exit 没有杀死消息泵?

c# - 如何在特定时间间隔后运行方法?

c# - 从 AssemblyInfo.cs 获取程序集版本