Add Existing item in runtime in wpf

Posted by prjmca-22036 under WPF on 5/3/2013 | Points: 10 | Views : 992 | Status : [Member] | Replies : 1
Hi, I am creating resource file at runtime in WPF its creating and displaying in resource filder but not showing in solution though refresh the folder also. When i do it manually add exsiting item, then only its adding please help me out how to add it to solution when ever its creating.

Thanks in advance...

My code is:


public void LaguageCulture(string languageid)
{
try
{
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@lagid", languageid);
//languageid =3
var dsResources = SqlHelper.ExecuteDataset(_objSqlConnection, "sproc_GetResourceNames", param);
string culturecode = string.Empty;
if (dsResources.Tables[1].Rows.Count > 0)
{
culturecode = dsResources.Tables[1].Rows[0][0].ToString();
}
FileInfo file = new FileInfo(System.Windows.Forms.Application.StartupPath.Replace("\\bin\\Debug", "") + "\\Resources\\EnglishResource." + culturecode + ".resx"); // Culture code is en-US
if (!file.Exists)
{
var resx = new ResXResourceWriter(System.Windows.Forms.Application.StartupPath.Replace("\\bin\\Debug", "") + "\\Resources\\EnglishResource." + culturecode + ".resx");
if (dsResources.Tables[0].Rows.Count > 0)
{
for (var i = 0; i < dsResources.Tables[0].Rows.Count; i++)
{
resx.AddResource(dsResources.Tables[0].Rows[i]["ResourceName"].ToString(), dsResources.Tables[0].Rows[i]["ResourceValue"].ToString());
}
}
resx.Generate();
//Application.Current.Resources.Add(culturecode + ".resx", resx);
resx.Close();
}
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culturecode);
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(culturecode);
}
catch (Exception)
{
throw;
}
}




Responses

Posted by: Peermohamedmydeen on: 5/27/2013 [Member] Bronze | Points: 25

Up
0
Down
The file should be content file of type and it should be added in to the project file to show under solution/project. So,

Application.Current.Resources.Add(culturecode + ".resx", resx); 


the above piece of code to add resources to app at run time.

To include a resource file,

Project p= new Project();

p.Load(fullPathName);

var iG = p.AddNewItemGroup();

var newBuild = iG.AddNewItem("MyFile", "Test\TestResource.resx");
newBuild .SetMetadata("Link", "TestResource.resx");
p.Save(fullPathName, Encoding.UTF8);


Try this..


PMM

prjmca-22036, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response