5

As I searched there is a folder in windows partition named "ProgramData" that contains applications' data which is used by applications in run time. Since this folder does not need admin permission and it is common between system users it is the best place to put the runtime files. In C#.Net I reach this folder address by this code:

Application.CommonAppDataPath

The problem is that I can not find the right folder to put my data in it while I'm creating windows installer(msi file) by Visual Studio Setup Project. I want to know how can I add this folder to my setup project.

Regards.

Reza Ameri
  • 1,803
  • 3
  • 24
  • 32
  • As I searched more I found this article in [link](http://www.codeproject.com/Tips/61987/Allow-write-modify-access-to-CommonApplicationData) I found out that only creator of a file in ProgramData folder has the permission to write in it. But, my problem is still on! – Reza Ameri Sep 27 '12 at 11:08

2 Answers2

7

Here is how you can add it:

  1. Go to "File System" view in your project and right click on "File System on Target Machine"
  2. Under "Add Special Folder" select "Custom folder" and give it a name
  3. Now right click on that folder and select "Properties Window"
  4. Set the "DefaultLocation" attribute to this: "[CommonAppDataFolder]"
  5. Now you can add any sub-folders you want under this new folder and place in there the files desired and your files should install in the desired path.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa367992(v=vs.85).aspx

Bogdan Mitrache
  • 10,536
  • 19
  • 34
  • Thanks buddy it is really cool. I didn't check this out due to the problem I mentioned above about permissions and stuff. I found the working solution and I'll post it here. – Reza Ameri Sep 28 '12 at 06:26
0

I found out that my question was wrong for my purpose because of the permission stuff. The solution I found is written here in stackoverflow.com and I mixed it up by the following code in my application to get address of public documents.

private static string getAddress()
    {
        RegistryKey rk = Registry.LocalMachine;
        RegistryKey sk = rk.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\explorer\\Shell Folders");
        string address = "";
        if (sk != null)
        {
            address = (string)sk.GetValue("Common Documents", @"c:\Users\Public\Documents");
        }
        return address;
    }

Let's overview what I said and what I did, First I found out that what I wanted is wrong and ApplicationData folder is accessible just for it's creator while I wanted a folder that is shared between all Users. So I found this link and followed it, create the folder I wanted in FileSystem Explorer in my Installer Project. Then I changed my C# code and made it read the address from registry.

Community
  • 1
  • 1
Reza Ameri
  • 1,803
  • 3
  • 24
  • 32
  • 1
    Congratulations. You've found a *very* broken way to attempt to [locate a shell folder](http://blogs.msdn.com/b/oldnewthing/archive/2003/11/03/55532.aspx). – Damien_The_Unbeliever Sep 28 '12 at 06:45
  • If you want to find the Common Documents folder in C#, you should use [`Environment.GetSpecialFolder(SpecialFolder.CommonDocuments)`](http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx). You shouldn't be randomly reading bits of the registry. – Damien_The_Unbeliever Sep 28 '12 at 06:56
  • 2
    Yeah got you, the reason I didn't use that piece code is that I'm using .Net Framework 2.0 and it does not support the 'SpecialFolder.CommonDocuments' and just supports the 'CommonApplicationData' that I explained the issue about this too. So I think I did my best! :) – Reza Ameri Sep 28 '12 at 07:04
  • 1
    Then you ought to P/Invoke to `SHGetKnownFolderPath` or the older `SHGetFolderPath`. Either way, you ought to be using a *documented* API, not reading random registry keys. – Damien_The_Unbeliever Sep 28 '12 at 07:12
  • Wow! I don't have any idea about what you said! – Reza Ameri Sep 28 '12 at 07:15