Click here to Skip to main content
14,106,748 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I'd like to able to set a path in the config file for my C# 4.0 assembly.
It's likely that the path will be to one of the windows special folders (CommonApplicationData). However in my code I don't want to assume that there will be a special folder in that path, I'd just like an easy/straight-forward way to expand it if there is.

The Path class static method GetFullPath doesn't pick up on an Environment.SpecialFolder enum in a string path (I know this was a big ask, but I did hope)

So I went looking at Environment.ExpandEnvironmentVariables and this led me to the CSIDL and KNOWNFOLDERID[^].
We're still running on XP and adding support for Widnows 7 (I don't think we support Vista)

So I suppose I could put %CSIDL_COMMON_APPDATA% in the path in my config file and use ExpandEnvironmentVariable(), assuming I'm not installing to Windows 7 in which case I assume my installer needs to update config files to use "FOLDERID_ProgramData".

Is this the way I should be dealing with this issue?
Is there a best practice for this that I've missed?

1. How should I represent a windows special folder in a path string in a config file?
2. How do I expand this representation into a full path in my code?

Any advice is welcome, thanks.

[Clarification]
Judging by JSOPs response this is probably just wishful thinking but I thought I'd clarify what exactly I'm trying to achieve:

I understand the Environment.SpecifalFolder enum and how to use the Environment.GetFolderPath method, I can even convert a string representation of a special folder to a path:
Environment.GetFolderPath((Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), mySpecialFolder))


I'm trying to avoid breaking my path into 2 parts in the config and dealing with a 'SpecialFolder' part separately, because we may not actually use a special folder.
I'd like to be able to configure my path as:
"C:\MyCompanyName\MyAppName\MyFolder"

or
"C:\Documents and Settings\All User\Application Data\MyCompanyName\MyAppName\MyFolder"

But i'd rather not explicitly expand the special folder in the configuration file.

I'd like to be able to enter:
"%CSIDL_COMMON_APPDATA%\MyCompanyName\MyAppName\MyFolder"

or
"%FOLDERID_ProgramData%\MyCompanyName\MyAppName\MyFolder"

or
"%System.Environment.SpecialFolder.CommonApplicationData%\MyCompanyName\MyAppName\MyFolder"

or where we're not using a special folder, enter just:
"C:\MyCompanyName\MyAppName\MyFolder"


And I'm asking if there's a library method that will allow me to expand the % wrapped bits in the path automatically, (and that will not fall over if there are no % warpped bits)?

I'm guessing from JSOPs earlier reply that there is not.
[/Clarification]
Posted
Updated 3-Mar-17 2:54am
v2
Comments
Philippe Mori 26-Aug-11 14:29pm
   
For folders, Vista is essentially like Windows 7 thus there are no reason not to support Vista if you do support XP.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

If I understand what you want, this might help. I wrote this method to look for the specified folder name within the specified special folder. If it's found, it returns the fully-qualified folder path. if NOT found, it creates the specified folder inside the specified specialFolder, and then returns the fully qualified path. It does NOT support folders more than one folder deep in the hierarchy from the specified special folder.

//--------------------------------------------------------------------------------
public static string CreateAppDataFolder(System.Environment.SpecialFolder specialFolder, string folderName)
{
    string appDataPath  = "";
    string dataFilePath = "";
    folderName          = folderName.Trim();
    if (folderName != "")
    {
        try
        {
            appDataPath = System.Environment.GetFolderPath(specialFolder);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (folderName.Contains("\\"))
        {
            string[] path = folderName.Split('\\');
            int folderCount = 0;
            int folderIndex = -1;
            for (int i = 0; i < path.Length; i++)
            {
                string folder = path[i];
                if (folder != "")
                {
                    if (folderIndex == -1)
                    {
                        folderIndex = i;
                    }
                    folderCount++;
                }
            }
            if (folderCount != 1)
            {
                throw new Exception("Invalid folder name specified (this function only creates the root app data folder for the application).");
            }
            folderName = path[folderIndex];
        }
    }
    if (folderName == "")
    {
        throw new Exception("Processed folder name resulted in an empty string.");
    }
    try
    {
        dataFilePath = System.IO.Path.Combine(appDataPath, folderName);
        if (!Directory.Exists(dataFilePath))
        {
            Directory.CreateDirectory(dataFilePath);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    AppDataFolder = dataFilePath;
    return dataFilePath;
}
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Use System.Environment.GetFolderPath(Environment.SpecialFolder) or System.Environment.GetFolderPath(Environment.SpecialFolder, Environment.SpecialFolderOption). In your case, use System.Environment.SpecialFolder.CommonApplicationData or System.Environment.SpecialFolder.LocalApplicationData for the first parameter.

See:
http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolderoption.aspx[^].

—SA
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

If someone stumbles on this (like me):

Environment.ExpandEnvironmentVariables(%VARNAME%\subpath)

from:
StackOverflow[^]

BR,
Daniel
   
Comments
Graeme_Grant 3-Mar-17 8:56am
   
Why are you answering a 6-year-old question?
CHill60 3-Mar-17 9:41am
   
What are you trying to add to the thread? The OP already knew about ExpandEnvironmentVariables and mentions it in their 6 - year old question
Daniel Balogh 10-May-17 5:29am
   
Sorry, overseen in the OP-Question.... Why after 6 years? I found it now, and assume someone else may find this thread now or in the future...
But you're right, he knew about the suggested solution.... Sorry again...
BR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 3 Mar 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100