C .resx फ़ाइल से स्ट्रिंग को c # में कैसे पढ़ें? कृपया मुझे दिशानिर्देश भेजें। क्रमशः
C .resx फ़ाइल से स्ट्रिंग को c # में कैसे पढ़ें? कृपया मुझे दिशानिर्देश भेजें। क्रमशः
जवाबों:
यह उदाहरण रिसोर्स मैन पेज पर MSDN पेज से है। गेटस्ट्रिंग () :
// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager("items", Assembly.GetExecutingAssembly());
// Retrieve the value of the string resource named "welcome".
// The resource manager will retrieve the value of the
// localized resource using the caller's current culture setting.
String str = rm.GetString("welcome");
ResourceManagerतब की आवश्यकता होती है जब आप एक बाहरी संसाधन को लोड करना चाहते हैं। <Namespace>.Propertiesइसके बजाय उपयोग करें ।
ResourceManagerजब तक आप बाहरी संसाधन से लोड नहीं कर रहे हैं, तब तक इसकी आवश्यकता नहीं होनी चाहिए ।
अधिकांश बातों के लिए, मान लें कि आपने एक प्रोजेक्ट (DLL, WinForms, जो कुछ भी बनाया है) आप केवल प्रोजेक्ट नेमस्पेस, "संसाधन" और संसाधन पहचानकर्ता का उपयोग करते हैं। उदाहरण के लिए:
एक परियोजना के नाम मान लेना: UberSoft.WidgetPro
और आपके resx में शामिल हैं:

आप बस उपयोग कर सकते हैं:
Ubersoft.WidgetPro.Properties.Resources.RESPONSE_SEARCH_WILFRED
यह कोशिश करो, मेरे लिए काम करता है .. सरल
मान लें कि आपके संसाधन फ़ाइल का नाम "TestResource.resx" है, और आप कुंजी को तब गतिशील रूप से पास करना चाहते हैं,
string resVal = TestResource.ResourceManager.GetString(dynamicKeyVal);
नाम स्थान जोड़ें
using System.Resources;
ResourceManagerतब की आवश्यकता होती है जब आप एक बाहरी संसाधन को लोड करना चाहते हैं। <Namespace>.Propertiesइसके बजाय उपयोग करें ।
.Resx फ़ाइल खोलें और सार्वजनिक रूप से "एक्सेस संशोधक" सेट करें।
var <Variable Name> = Properties.Resources.<Resource Name>
.Resx फ़ाइल को प्रोजेक्ट गुणों के तहत Visual Studio का उपयोग करके जोड़ा गया था, स्ट्रिंग को एक्सेस करने का एक आसान और कम त्रुटि वाला तरीका है।
स्ट्रिंग तक पहुँचना उतना ही आसान है:
var resourceManager = JoshCodes.Core.Testing.Unit.Properties.Resources.ResourceManager;
var exampleXmlString = resourceManager.GetString("exampleXml");JoshCodes.Core.Testing.Unitप्रोजेक्ट के डिफ़ॉल्ट नामस्थान के साथ बदलें ।
यदि किसी कारण से आप अपनी संसाधन फ़ाइलों को App_GlobalResources में नहीं डाल सकते हैं, तो आप ResXResourceReader या XML रीडर का उपयोग करके सीधे संसाधन फ़ाइलें खोल सकते हैं।
यहाँ ResXResourceReader का उपयोग करने के लिए नमूना कोड है:
public static string GetResourceString(string ResourceName, string strKey)
{
//Figure out the path to where your resource files are located.
//In this example, I'm figuring out the path to where a SharePoint feature directory is relative to a custom SharePoint layouts subdirectory.
string currentDirectory = Path.GetDirectoryName(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"]));
string featureDirectory = Path.GetFullPath(currentDirectory + "\\..\\..\\..\\FEATURES\\FEATURENAME\\Resources");
//Look for files containing the name
List<string> resourceFileNameList = new List<string>();
DirectoryInfo resourceDir = new DirectoryInfo(featureDirectory);
var resourceFiles = resourceDir.GetFiles();
foreach (FileInfo fi in resourceFiles)
{
if (fi.Name.Length > ResourceName.Length+1 && fi.Name.ToLower().Substring(0,ResourceName.Length + 1) == ResourceName.ToLower()+".")
{
resourceFileNameList.Add(fi.Name);
}
}
if (resourceFileNameList.Count <= 0)
{ return ""; }
//Get the current culture
string strCulture = CultureInfo.CurrentCulture.Name;
string[] cultureStrings = strCulture.Split('-');
string strLanguageString = cultureStrings[0];
string strResourceFileName="";
string strDefaultFileName = resourceFileNameList[0];
foreach (string resFileName in resourceFileNameList)
{
if (resFileName.ToLower() == ResourceName.ToLower() + ".resx")
{
strDefaultFileName = resFileName;
}
if (resFileName.ToLower() == ResourceName.ToLower() + "."+strCulture.ToLower() + ".resx")
{
strResourceFileName = resFileName;
break;
}
else if (resFileName.ToLower() == ResourceName.ToLower() + "." + strLanguageString.ToLower() + ".resx")
{
strResourceFileName = resFileName;
break;
}
}
if (strResourceFileName == "")
{
strResourceFileName = strDefaultFileName;
}
//Use resx resource reader to read the file in.
//https://msdn.microsoft.com/en-us/library/system.resources.resxresourcereader.aspx
ResXResourceReader rsxr = new ResXResourceReader(featureDirectory + "\\"+ strResourceFileName);
//IDictionaryEnumerator idenumerator = rsxr.GetEnumerator();
foreach (DictionaryEntry d in rsxr)
{
if (d.Key.ToString().ToLower() == strKey.ToLower())
{
return d.Value.ToString();
}
}
return "";
}
System.Windows.Formsउपयोग करने के लिए एक संदर्भ जोड़ना होगा System.Resources.ResXResourceReader। इसके अलावा, आप इसके var enumerator = rsxr.OfType<DictionaryEntry>();बजाय LINQ का उपयोग और उपयोग कर सकते हैं ।
एक बार जब आप समाधान / असेंबली के लिए एक संसाधन (नाम: ResourceName और Value: ResourceValue) जोड़ लेते हैं, तो आप आवश्यक संसाधन प्राप्त करने के लिए बस "Properties.Resource.ResourceName" का उपयोग कर सकते हैं।
मैंने Visual Studio के माध्यम से .resx फ़ाइल जोड़ी। इसने designer.csमेरे द्वारा वांछित किसी भी कुंजी के मूल्य को तुरंत वापस करने के लिए गुणों के साथ एक फ़ाइल बनाई । उदाहरण के लिए, यह डिजाइनर फ़ाइल से कुछ ऑटो-जेनरेट किया गया कोड है।
/// <summary>
/// Looks up a localized string similar to When creating a Commissioning change request, you must select valid Assignees, a Type, a Component, and at least one (1) affected unit..
/// </summary>
public static string MyErrorMessage {
get {
return ResourceManager.GetString("MyErrorMessage", resourceCulture);
}
}
इस तरह, मैं बस करने में सक्षम था:
string message = Errors.MyErrorMessage;
विजुअल स्टूडियो के माध्यम से बनाई गई फ़ाइल कहां Errorsहै Errors.resxऔर MyErrorMessageयह कुंजी है।
मैंने अपनी संसाधन फ़ाइल को सीधे अपनी परियोजना में जोड़ा, और इसलिए मैं रेक्स फ़ाइल नाम के साथ ठीक अंदर स्ट्रिंग को एक्सेस करने में सक्षम था।
उदाहरण: Resource1.resx में, कुंजी "resourceKey" -> string "dataString"। स्ट्रिंग "dataString" प्राप्त करने के लिए, मैंने सिर्फ Resource1.resourceKey रखा।
ऐसा नहीं करने के कारण हो सकते हैं जिनके बारे में मुझे जानकारी नहीं है, लेकिन यह मेरे लिए काम करता है।
संसाधन फ़ाइल से मान प्राप्त करने का सबसे सरल तरीका। प्रोजेक्ट में संसाधन फ़ाइल जोड़ें। अब उस स्ट्रिंग को प्राप्त करें जहां आप जोड़ना चाहते हैं जैसे मेरे मामले में यह टेक्स्ट ब्लॉक (सिल्वरलाइट) था। कोई नामस्थान भी जोड़ने की जरूरत नहीं है। मेरे मामले में ठीक काम कर रहा है
txtStatus.Text = Constants.RefractionUpdateMessage;
संसाधनों को पुनः प्राप्त करने के लिए एक संसाधन प्रबंधक बनाएँ।
ResourceManager rm = new ResourceManager("param1",Assembly.GetExecutingAssembly());
String str = rm.GetString("param2");
param1 = "असेम्बलीनाम। RourceFolderName.ResourceFileName"
param2 = स्ट्रिंग का नाम जिसे संसाधन फ़ाइल से पुनर्प्राप्त किया जाना है
यह मेरे लिए काम करता है। आप स्ट्रिंग में ठीक के साथ एक strings.resx फ़ाइल है। इसे पढ़ने के लिए
String varOk = My.Resources.strings.ok