Defining where files are installed

2019-09-09 02:34发布

问题:

I am trying to deploy a .NET application as a ClickOnce Application, but I am having trouble defining where the application is installed. I need to know this because I have to include support files. I have already added the support files as "existing items". I had assumed that the program would install in Program Files, but it does not exist there. Instead, there is just a shortcut on the desktop. Can someone explain how/where the install path is defined using VS2012?

回答1:

Clickonce application gets installed under the user profile, not the Program Files path.

On windows Vista and Windows 7, clickonce application path will be somewhere in c:\users\username\AppData\Local\Apps\2.0\

On Windows XP, clickonce application path will be somewhere in c:\document and Settings\username\LocalSettings\Apps\2.0\

Note that Clickonce application path is different everytime upon installation, I found the best way is to make your app to write its own app path to the reg key, this way you know exactly where the app path is by looking at the reg.



回答2:

One solution I found was to use some of the Application class properties to determine where ClicOnce installed an instance of my program. But be aware that some those are deleted on uninstall of the program.

// To get the path for the executable file that started the application, not including the executable name.
PATH_RESOURCES = Application.StartupPath ;

For persistant data I created references to specific paths like :

PATH_USERDATA = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\myAppName\";
        if (!System.IO.Directory.Exists((string)PATH_USERDATA))
        {
            System.IO.Directory.CreateDirectory((string)PATH_USERDATA);
        }


PATH_REPORTS = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\myAppName\";
        if (!System.IO.Directory.Exists((string)PATH_REPORTS ))
        {
            System.IO.Directory.CreateDirectory((string)PATH_REPORTS );
        }


回答3:

So as user831062 pointed out, ClickOnce apps get installed under the user profile, not the Program Files path. Because of this, the install directory is different on every machine and almost impossible to access directly.

The part that I was hung-up on, was where are the files that I have included in the project located, and more importantly - how do I access them?

Well, as mentioned IN THIS LINK, if you mark the file as a "data file", under:

Project Properties > Publish > Application Files > Publish Status

you'll be able to access them using something like:

textBox = File.ReadAllLines(ApplicationDeployment.CurrentDeployment.DataDirectory + @"\myFile.txt")).ToList();

If you don't mark it as a "data file", but rather as just an "Include (Auto)", it will just be located in the install directory itself, which can be accessed by calling the file directly using something like:

textBox = File.ReadAllLines(@"myFile.txt")).ToList();

Anyway, took me an hour or so to find this, so hopefully it helps someone else out.



回答4:

If you have added your files to your project, set the property for "build action" to "content" and set "copy to output directory" to "copy always". This way, the files will be included in your deployment. When the application is run, retrieve the location of the assembly and look in the same relative folder as where they were included in the project. For example, if they are in the top folder of the build output directory (/bin/debug/ or /bin/release/), they will be included in the same folder as the executable, which you can discover using this:

System.Reflection.Assembly.GetExecutingAssembly().Location