设置程序必须在安装程序启动时运行
我有一个 C# .net 4 应用程序,我开始创建安装程序。
安装的程序运行良好,但我的客户希望应用程序以窗口启动(它是一个“始终位于顶部”工具栏,可帮助他们管理呼叫)。
我想创建一个“类安装程序”,并在其中插入注册表中的密钥。
我有两个问题:
第一:如何找到可执行路径?它可以在安装之间改变。我在某处找到了 Application.ExecutablePath,但它似乎位于 Application.Window.Forms 中,所以我认为它与 WPF 不兼容
其次:我需要将此密钥插入本地计算机的注册表中。 Windows 安装程序中有没有办法指定用户必须具有管理员权限?
这是我开始的代码:
RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("CstStart", ???);
非常感谢您的帮助!
编辑:我发现我可以将一些数据传递给我的自定义操作,其中:
/DIR="[TARGETDIR]\"
CustomActionData 中的
。通常据说我可以通过 但是
this.Context.Parameters["DIR"];
...似乎在我的安装程序类中, this.Context 为 null :(
EDIT2: Context 为 null 因为我在构造函数中执行此操作,我现在正在执行此操作如果是 this.AfterInstall ,现在我得到一个上下文,它似乎包含一个 var“ assemblypath ”,其中包含我需要的路径。
I've a C# .net 4 application, I'm starting to create the installer.
The installed program works fine, but my customer want that the application start with window(it's an "Always of top" toolbar which help them to manage their call).
I thought to create a "Class Installer", and in it insert a key in the registry.
I've two problems:
First: How can I find the executable path? It can change between installation. I found somewhere a Application.ExecutablePath, but it seems it located into Application.Window.Forms so I think its not compatible with WPF
Second: I need to insert this key in the registry of the local machine. Is there a way in a windows installer to specify that the user must have admin rights?
Here is the code I started:
RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("CstStart", ???);
Thank you very much for your help!
EDIT: I found that I can pass to my Custom action some data, with :
/DIR="[TARGETDIR]\"
in the CustomActionData.
Normally it's said I can retrieve it through the
this.Context.Parameters["DIR"];
But.... it seems that in my installer class, this.Context is null :(
EDIT2: The Context was null because I was doing this in the constructor, I'm now doing this in the event this.AfterInstall and now I get a context, which seems to contains a var "assemblypath" which contains exactly the path I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的假设是您正在这里的安装项目中工作......
执行此操作的一种方法 - 我并不是说这是最好的,但它可能是最简单的 - 就是执行以下操作如下:
在“文件系统”选项卡中,右键单击“目标计算机上的文件系统”,“添加特殊文件夹”,“用户的启动文件夹”,这样的文件夹就会出现在列表中。接下来,获取项目输出的快捷方式(通过右键单击)并将该快捷方式拖到“启动”文件夹中。
最后,由于您希望所有用户都使用此快捷方式,因此请进入“部署项目属性”并设置“InstallAllUsers”为 true。
现在,此方法有一个警告,即应用程序不会在 Windows 启动时启动,而是在有人登录时启动。从你所说的你的项目(一个 UI 应用程序)来看,这可能没问题。
如果你真的想破解注册表,你可以考虑创建一个自定义操作 dll。您可以在其中编写您喜欢的任何内容,并且在安装过程结束时,安装程序将调用您的 dll,以便您可以完成您的工作。您可以将可执行文件的路径等内容作为参数传递到 dll 中(您可以从安装程序对话框中获取该文件,而不是从任何对象中获取)。我不会详细讨论这一点,因为网络上肯定有关于此的负载。
您应该意识到,您可以传递到 dll 中的信息量是有限的 - 如果内存允许,可能会低至 256 个字符。你可能只需要一个路径就可以了,但是当你开始做更多的事情时......
至于检测用户是否是本地管理员,这有点棘手,因为自定义操作 dll 在最后运行在安装过程中而不是在开始时 - 因此您可以编写代码来找出当前用户所在的组,但很可能在该代码运行之前您就已经崩溃了。我不相信(至少在 Microsoft 安装项目中)可以知道这一点。
My assumption is you're working from a Setup project here.....
one way to do this - I'm not saying it is the best but it is probably the easiest - is to do the following:
In the "File System" tab, right click on "File System on Target Machine", "Add Special Folder", "User's Startup Folder" and such folder appears on the list. Next, get a shortcut to your project output (by right clicking) and drag that shortcut into the Startup folder.
Last of all, since you want this for all users, go into the Deployment Project Properties and set "InstallAllUsers" to true.
Now, there is a caveat with this approach, in that the app does not start when Windows starts, but when someone logs on. From what you say of your project (a UI app) this may be ok.
If you really want to go hacking the registry you could look at creating a custom action dll. This is something where you can write pretty much what you like, and at the end of the install process the setup program will call into your dll so you can do your stuff. You can pass things like the path of the executable as a parameter into the dll (which you would pick up from the installer dialog, not from any object). I won't go into this is detail as there must be loads on the web about this.
You should be aware that there is a finite amount of information you can pass into the dll - might be as low as 256 chars if memory serves. You'll probably be ok with just a path but when you start doing more....
As regards detecting whether a user is a local Admin, that is a bit more tricky, if only because the custom action dll gets run at the end of the installation rather than at the start - so you could write code to find out what groups the current user is in, but the chances are you'd have blown up before that code could run. I don't believe - in a Microsoft Setup project at least - that it is possible to know this.