如何在配置文件的路径中使用Windows特殊文件夹进行C#(4.)程序集? [英] How to use Windows Special folders in a path in a config file for a C# (4.) assembly?

查看:72
本文介绍了如何在配置文件的路径中使用Windows特殊文件夹进行C#(4.)程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在配置文件中为我的C#4.0程序集设置路径。

路径可能是其中一个Windows特殊文件夹(CommonApplicationData)。但是在我的代码中我不想假设在该路径中会有一个特殊的文件夹,我只是想要一个简单/直接的方法来展开它。



Path类的静态方法GetFullPath没有在字符串路径中获取Environment.SpecialFolder枚举(我知道这是一个很大的问题,但我确实希望)



所以我去看了Environment.ExpandEnvironmentVariables,这让我看到了 CSIDL和KNOWNFOLDERID [ ^ ]。

我们仍在运行XP并添加对Widnows 7的支持(我认为我们不支持Vista)



所以我想我可以将%CSIDL_COMMON_APPDATA%放在配置文件的路径中并使用ExpandEnvironmentVariable(),假设我没有安装到Windows 7,在这种情况下我假设我的安装程序需要更新配置文件以使用FOLDERID_ProgramData。



这是我应该处理这个问题的方式吗?

我错过了最好的做法吗?



1.如何在配置文件中的路径字符串中表示Windows特殊文件夹?

2.如何将此表示扩展为完整路径在我的代码中?



欢迎任何建议,谢谢。



[澄清]

根据JSOPs的回应判断,这可能只是一厢情愿,但我想我会澄清我到底想要实现的目标:



我理解环境.SpecifalFolder枚举以及如何使用Environment.GetFolderPath方法,我甚至可以将特殊文件夹的字符串表示转换为路径:

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))





我试图避免在配置中将我的路径分成2部分并单独处理'SpecialFolder'部分,因为我们实际上可能没有使用特殊文件夹。

我是喜欢能够将我的路径配置为:



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"





我问是否有一种库方法可以让我自动扩展路径中的%包裹位(如果没有%warpped位,那就不会倒掉)?



我猜想来自JSOPs的早期回复没有。

[/澄清]



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]

推荐答案

如果我理解你想要什么,这可能有所帮助。我写了这个方法来查找指定的特殊文件夹中的指定文件夹名称。如果找到,则返回完全限定的文件夹路径。如果未找到,则在指定的specialFolder中创建指定的文件夹,然后返回完全限定的路径。它不支持从指定的特殊文件夹中的层次结构深处的多个文件夹。



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;
}


它可以很容易地完成,请查看:



http://technet.microsoft.com/nl-nl/library/ee176604% 28en-us%29.aspx [ ^ ]



其他方式:

访问所有Windows特殊文件夹 [ ^ ]



祝你好运!
It can be done very easy, check this out:

http://technet.microsoft.com/nl-nl/library/ee176604%28en-us%29.aspx[^]

Other ways:
Accessing All of Windows Special Folders[^]

Good luck!


使用 System.Environment.GetFolderPath(Environment.SpecialFolder) System.Environment.GetFolderPath(Environment.SpecialFolder,Environment.SpecialFolderOption)。在您的情况下,对第一个参数使用 System.Environment.SpecialFolder.CommonApplicationData System.Environment.SpecialFolder.LocalApplicationData



请参阅:

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

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx [< a href =http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspxtarget =_ blanktitle =New Window> ^ ],
http://msdn.microsoft.com/en-us/library /system.environment.spe cialfolderoption.aspx [ ^ ]。



-SA
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


这篇关于如何在配置文件的路径中使用Windows特殊文件夹进行C#(4.)程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆