问题
I am creating a desktop app that needs to install an SDF file to the SpecialFolder.CommonDocuments
folder (C:\Users\Public\documents
in Win 7). In a Visual Studio desktop Deployment Project, how do I specify this folder in the File System Editor?
I tried creating a custom folder in the File System Editor and pointing it to the CommonDocuments folder in the Properties pane, like this:
Unfortunately, that specification won't build. I don't want to hard-code a folder path, since it varies between Windows versions. So, how do I specify the CommonDocuments folder in the FileSystem Editor? Thanks for your help.
回答1:
I figured this one out and documented it for internal purposes. So, I'll just reprint that writeup here:
Visual Studio deployment projects don't support the CommonDocuments folder directly, but we can add that support by using the Launch Conditions Editor, which has a "Search Target Machine" task. We will use the task to search the Windows registry for the path to the Public Documents folder and assign the result to an installer property (actually a variable) called COMDOCFOLDER. We will then use that variable to set the path to a custom folder in the File System Editor.
Here are the steps to perform the task. First, open the Launch Conditions Editor in a Visual Studio deployment project:
Right-click 'Search Target Machine' and select 'Add Registry Search' from the Context menu. A new item will appear (see 1 above). Name it Get Common Documents Folder
. In the properties pane (See 2 above), set the 'Property' property (the name of our variable) to COMDOCFOLDER
, set the 'Root' property (the Registry root key to search) to vsdrrHKLM
, and set the 'RegKey' property (The Registry key to find) to SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
. Notice that we omitted the root key from the 'RegKey' Property. Finally, set the 'Value' property (The name of the value we are searching for within the registry key) to Common Documents
. The COMDOCFOLDER variable will now hold the path to the Public Documents folder.
Next, go to the File System Editor in the Visual Studio deployment project:
Right-click 'File System on Target Machine' and select 'Add Special Folder > Custom Folder' from the context menu. A new item will appear (see 1 above). Rename the item Common Documents
. In the properties pane (See 2 above), set the 'Property' property to COMDOCFOLDER
. I set the 'DefaultLocation' property to the hard-coded value of the CommonDocuments folder for Windows Vista and later; this value would only be used if the COMDOCFOLDER property returned a null value, which shouldn't happen. The installer now has a Common Documents folder that points to the Public Documents folder, as specified in the Windows Registry.
There is more information in this Microsoft Support How-To.
回答2:
The answer of David Veeneman is great! Helped a lot. A little correction:
Right-click 'File System on Target Machine' and select 'Add Special Folder > Custom Folder' from the context menu. A new item will appear (see 1 above). Rename the item Common Documents. In the properties pane (See 2 above), set the 'Property' property to
[COMDOCFOLDER]
Those square brackets are necessary, otherwise you'll receive exception while executing the installer.
来源:https://stackoverflow.com/questions/5584380/visual-studio-installer-how-to-target-windows-public-documents-folder