है, मुझे काम करने के लिए कस्टम कॉन्फ़िगरेशन अनुभाग प्राप्त करने में समस्या हो रही है। यह कुछ कोड है जो मुझे वेब से मिला है ताकि इस क्षेत्र को थोड़ा बेहतर समझने की कोशिश की जा सके और मुझे अपने स्वयं के कस्टम कॉन्फ़िगरेशन सेक्शन में जाना चाहिए।कस्टम कॉन्फ़िगरेशन अनुभाग जिसमें संग्रह
जब मैं कंसोल ऐप में कोड चलाता हूं तो मुझे मिली त्रुटि ' अपरिचित विशेषता' एक्सटेंशन 'है। ध्यान दें कि गुण नाम केस-संवेदी हैं। '
मुख्य अनुप्रयोग में कोड बातें चलते रहने के लिए
var conf = ConfigurationManager.GetSection("uploadDirector");
है और जहां अपवाद प्रतीत होता है यह है।
इस config अनुभाग मैं आशा करता हूं/प्राप्त करने के लिए
<AuthorisedClients>
<AuthorisedClient name="Client">
<Queue id="1" />
<Queue id="7" />
</AuthorisedClient>
<AuthorisedClient name="Client2">
<Queue id="3" />
<Queue id="4" />
</AuthorisedClient>
</AuthorisedClients>
यहाँ कोड मैं वेब
.config फ़ाइल
<uploadDirector>
<filegroup name="documents" defaultDirectory="/documents/">
<clear/>
<add extension="pdf" mime="application/pdf" maxsize="100"/>
<add extension="doc" mime="application/word" maxsize="500"/>
</filegroup>
<filegroup name="images">
<clear/>
<add extension="gif" mime="image/gif" maxsize="100"/>
</filegroup>
</uploadDirector>
UploadDirectorConfigSection से मिल गया है है की कोशिश कर रहा है .cs
public class UploadDirectorConfigSection : ConfigurationSection {
private string _rootPath;
public UploadDirectorConfigSection() {
}
[ConfigurationProperty("rootpath", DefaultValue="/", IsRequired=false, IsKey=false)]
[StringValidator([email protected]"[email protected]#$%^&*()[]{};'\|\\")]
public string RootPath {
get { return _rootPath; }
set { _rootPath = value; }
}
[ConfigurationProperty("", IsRequired = true, IsKey = false, IsDefaultCollection = true)]
public FileGroupCollection FileGroups {
get { return (FileGroupCollection) base[""]; }
set { base[""] = value; }
}
}
FileGroupCollection.cs
public class FileGroupCollection : ConfigurationElementCollection {
protected override ConfigurationElement CreateNewElement() {
return new FileGroupElement();
}
protected override object GetElementKey(ConfigurationElement element) {
return ((FileGroupElement) element).Name;
}
public override ConfigurationElementCollectionType CollectionType {
get {
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName {
get {
return "filegroup";
}
}
protected override bool IsElementName(string elementName) {
if (string.IsNullOrWhiteSpace(elementName) || elementName != "filegroup")
return false;
return true;
}
public FileGroupElement this[int index] {
get { return (FileGroupElement) BaseGet(index); }
set {
if(BaseGet(index) != null)
BaseRemoveAt(index);
BaseAdd(index, value);
}
}
}
FileGroupElement.cs
public class FileGroupElement : ConfigurationElement {
[ConfigurationProperty("name", IsKey=true, IsRequired = true)]
[StringValidator(InvalidCharacters = @" [email protected]#$%^&*()[]{}/;'""|\")]
public string Name {
get { return (string) base["name"]; }
set { base["name"] = value; }
}
[ConfigurationProperty("defaultDirectory", DefaultValue = ".")]
public string DefaultDirectory {
get { return (string) base["Path"]; }
set { base["Path"] = value; }
}
[ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
public FileInfoCollection Files {
get { return (FileInfoCollection) base[""]; }
}
}
FileInfoCollection.cs
public class FileInfoCollection : ConfigurationElementCollection {
protected override ConfigurationElement CreateNewElement() {
return new FileInfoCollection();
}
protected override object GetElementKey(ConfigurationElement element) {
return ((FileInfoElement) element).Extension;
}
}
FileInfoElement.cs
public class FileInfoElement : ConfigurationElement {
public FileInfoElement() {
Extension = "txt";
Mime = "text/plain";
MaxSize = 0;
}
[ConfigurationProperty("extension", IsKey = true, IsRequired = true)]
public string Extension {
get { return (string)base["extension"]; }
set { base["extension"] = value; }
}
[ConfigurationProperty("mime", DefaultValue = "text/plain")]
public string Mime {
get { return (string) base["mime"]; }
set { base["mime"] = value; }
}
[ConfigurationProperty("maxsize", DefaultValue = 0)]
public int MaxSize {
get { return (int) base["maxsize"]; }
set { base["maxsize"] = value; }
}
}
अपने वांछित विन्यास के बारे में, शायद सबसे सरल कार्यान्वयन की तरह दिखाई देगा वाह, यह मेरा सटीक मुद्दा भी है। धन्यवाद! –
यह मेरा मुद्दा भी था। आपका बहुत बहुत धन्यवाद। एक अपवित्र पकड़ो :-)। – Oybek
धन्यवाद महोदय! बिल्कुल सही, मेरी इच्छा है कि मैं दो बार ऊपर उठा सकता हूं! – Jonas