开发者

app.config syntax for structure

I have difficulties figuring out a useful app.config structures for my program.

What I am doing is writing a windows service, which watches several directories on a server for files being uploaded.

My problem is making my program configurable for n folders when in each of those n folders, i need to watch for a different collection of filetypes e.g. folder1: .dwg; folder2: .dwg;.doc;.xls;

then when a new file is uploaded, for example a .dwg file to folder1, then i need to run program1 to convert the dwg to a dwf, and program2 to convert the dwg to a .swf. but that only if filetype is .dwg and folder = folder1

if a .dwg is uploaded for example to folder2, then it needs to be converted to a pdf

The closes i have come to make this configurable in a app.config file is this:

            <Folder2 name="C:\inetpub\wwwroot\CAFM\Fotos" filetypes=".dwg;.pdf;">
                <action>
                    <add key="ExecutableAndPath" value="C:\Users\Me\Desktop\verydoc\dwg2vec.exe"/>
                    <add key="AtCommandBegin" value=""/>
                    <add key="AtCommandMiddle" value=""/>
                    <add key="AtCommandEnd" value=""/>
                </action>
            </Folder2>
        </Folders>
    </Raumplaner>


    <DMS>
        <Folders>
            <Folder name="C:\inetpub\wwwroot\CAFM\Dokumente" filetypes=".dwg;.pdf;">
                <action>
                    <add key="ExecutableAndPath" value="C:\Users\Me\Desktop\verydoc\dwg2vec.exe"/>
                    <add key="AtCommandBegin" value=""/>
                    <add key="AtCommandMiddle" value=""/>
                    <add key="AtCommandEnd" value=""/>
                </action>
            </Folder>
        </Folders>
   开发者_开发技巧 </DMS>
</Projects>


enter code here

But that's not quite what I want. I have difficulties bringing this to a usable from in app.config


Do you mean usable as in "easy to create config file" or "Easy to use in my program".

I have a similar service where I layed it out like this

<FolderConfiguration>
    <Folders>
        <Folder ImportFolder="c:\Foo"
                FileTypes=".xxx"
                Action="blabla"/>
        <Folder ImportFolder="C:\Bar
                FileTypes="..."/>
    </Folders>
</FolderConfiguration>

The code looks like this

public class FolderConfigSection : ConfigurationSection
{
    [ConfigurationProperty("Folders", IsDefaultCollection = true)]
    public FolderConfigCollection Folders {
        get { return (FolderConfigCollection)base["Folders"]; }
    }
}

public class FolderConfigElement : ConfigurationElement {
    private const string ImportFolderConfigName = "ImportFolder";
    private const string FileTypesConfigName = "FileTypes";

    [ConfigurationProperty(ImportFolderConfigName, IsKey = true, IsRequired = true)]
    public string ImportFolder {
        get { return (string)this[ImportFolderConfigName]; } 
        set { this[ImportFolderConfigName] = value; }
    }

    [ConfigurationProperty(FileTypesConfigName, IsRequired = true)]
    public string FileTypes {
        get { return (string)this[FileTypesConfigName]; }
        set { this[FileTypesConfigName] = value; }
    }
}

[ConfigurationCollection(typeof(FolderConfigElement), AddItemName = "Folder",
 CollectionType=ConfigurationElementCollectionType.BasicMap)]
public class FolderConfigCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement() {
        return new FolderConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element) {
        return (element as FolderConfigElement).ImportFolder;
    }
}

and then I access it via

      FolderConfigSection folderConfigSection = ConfigurationManager.GetSection("FolderConfiguration") as FolderConfigSection;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜