Reading and Writing keys to appsettings in WebConfigEditor - asp.net

My project contains web.config file and an external appSettings file. I am making a WebConfig Editor that has options to Read AppSettings key from web.config and external appSetting file to display them on webPage.
Also, I am allowing user to delete any key by clicking on Remove button.
Moreover, user can also update any key's value by clicking on update button.Or
he can also insert new key by clicking on Add New Key button.
The key issue I am facing is that whenfever I try to add a new key , it gets inserted into
web.config file as expected , but at the same time it adds all the keys present in external appSettings file into web.config ( which is abrupt behavior).
How to stop this migration of keys from external appSettings file to web.config on any key's update / delete/ add function?

For reading, put external file in Config folder under root and then use this code to read key/values based on key name it read from web.config or external file.
// get from web.config
String myKey = ConfigurationManager.AppSettings.Get("Key1");
String str += "AppSetting value from web.config:" + myKey;
// get from external AppSetting file
myKey = ConfigurationManager.AppSettings.Get("Key2");
String str2 += "AppSetting value from external AppSetting file:" + myKey;
where Key1 is in web.config and Key2 in external config file
also
to find al key values use foreach loop
foreach (string key in ConfigurationManager.AppSettings)
{
string value = ConfigurationManager.AppSettings[key];
Console.WriteLine("Key: {0}, Value: {1}", key, value);
}

While reading the keys add a unique signature with the keys of web.config file and external app settings file.
on web only show keys not the signature, and when u add keys add the same signature ( if adding for web.config then web.config's Signature ) and when writing to the web.config apply the check for signature if the signature for the key is of external app settings file then ignore the key otherwise write the key.
Its the Simple solution, well if u have any query do ask

Related

Access file outside of server or root in asp.net

i want to view file which is out side of my project or drive. for that i added a key in web.config with a value of destination location.
i.e.
<add key="fileuploadView" value="file:///D:\SubhrasData\xyz\I3ms\mis\doc\" />
now i get the path in following way
Dim path As String = ConfigurationSettings.AppSettings("fileuploads")
and assign path to view the file using hyperlink.
i.e. hypSpCertificate.NavigateUrl = path & gObjDt.Rows(0)("VCH_DOC_PATH")
but it first search in its folder first(in localhost). but i want that it should directly search the give file path.
You are assigning value in key with name "fileuploadView" and getting it by "fileuploads"
Shouldn' this be like
Dim path As String = ConfigurationSettings.AppSettings("fileuploadView");

Upload File with FileUpload and Stock in database using Framwork entity

I want to upload file with file upload and stock in DataBase SQLserver using framework entity , I use this code :
string strRealPath = Request.PhysicalApplicationPath;
if(FileUpload1.HasFile)
{
string fileName = FileUpload1.FileName;
FileUpload1.SaveAs(strRealPath + fileName);
//Now insert the file into the database.
}
f.photo = Convert.ToString(FileUpload1.FileBytes);
But I find anything added .I use the debugger he tell me that posted file is null
Thanks
Firstly, your form should be encrypted as multipart/form-data. Add parameter to your form tag like below:
enctype = "multipart/form-data"
Secondly you have to send file as controller parameter HttpPostedFile.
This tutorial will be useful for you. Let us know, does it work properly.
===EDIT===
When it comes to database. You have to have column of binary type (varbinary[max]), and you
should try save it there (remember about try/catch). To read content of the file, use stream reader.

How to read the Resource File using C#?

I have a web application which should be Localized to 3 languages. All the controls are taking the control text from the Resx file of that language. Now I have scenario like suppose if we have a messages,custom error messages to show for that particular culture. So for this I have created a seperate Foldere as "Resources" and created a resx as "DialogMessages.ar-IQ.resx".
How can I read the "DialogMessages.ar-IQ.resx" in C# ?
I have tried to read the file using ResxResourceReader class. Is this a correct process or any flaw exists ?
You can use ResXResourceReader and specifying the resource file location properly .
ResXResourceReader reader = new ResXResourceReader("Map path with resource file");
IDictionaryEnumerator iterator = reader.GetEnumerator();
while (iterator.MoveNext())
{
// process the collection of key value pair.
}

How can I reference an appSetting in a different part of web.config

I have my appSettings defined in a separate config file called Appsettings.Dev.Config, and I include that file inside my web.config file like so
<appSettings configSource="ConfigFiles\AppSettings.Dev.config"/>
Lets say one of the settings in the file is
<add key="MailerEmailAccount" value="myemail#myserver.com" />
Can I access the value of the setting MailerEmailAccount elsewhere inside web.config? How?
Nope, the web configuration file cannot pull "settings" from itself; it's not dynamic at all. The only sort of dynamic functionality is the ability to include other .config, but that's just a "suck all these settings in as if they were part of me" kind of thing.
It might be possible if you create a custom ConfigurationSection that pulls the value from appSettings.
Here's an article that explain how to create a custom configuration section:
http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx
I don't know if this is what you're looking for, but it's the only way I can think of to read a web.config setting from within the web.config.
EDIT
I haven't tested this, but maybe something like this would work?:
[ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
public string LocalName
{
get
{
return this["localName"] as string;
}
set
{
this["localName"] = WebConfigurationManager.AppSettings.Get(value);
}
}

how to check whether a file exists before creating it

I am creating an xml file. I need to check first if the file exists or not. If the file does not exist, create it and add the data cmg from a .cs file.
If the file exists, don't create the file just add the data cmg from a .cs file.
My code looks like this:
string filename="c:\\employee.xml";
XmlTextWriter tw=new XmlTextWriter(filename,null);//null represents
the Encoding Type//
tw.Formatting=Formatting.Indented; //for xml tags to be indented//
tw.WriteStartDocument(); //Indicates the starting of document (Required)//
tw.WriteStartElement("Employees");
tw.WriteStartElement("Employee","Genius");
tw.WriteStartElement("EmpID","1");
tw.WriteAttributeString("Name","krishnan");
tw.WriteElementString("Designation","Software Developer");
tw.WriteElementString("FullName","krishnan Lakshmipuram Narayanan");
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
so next time we add data to file we need to check if the file exits and add data to xml file
and as we have made empID as a primary key, if user tries to make duplicate entry we need to avoid
Is this possible to do?
if (!File.Exists(filename))
{
// create your file
}
or
if (File.Exists(filename))
{
File.Delete(filename);
}
// then create your file
File class is in System.IO namespace (add using System.IO; to your file)
You can't append records to an XML file, you have to read the file and then rewrite it.
So, just check if the file exists, and read the records from it. Then write the file including all previous records and the new record.
Have a look at the File.Exists method here
Testing for existance of a file before attempting to create it inherently is subject to a "things change after check" race condition. Who can guarantee you that your application isn't preempted and put to sleep for a moment after you checked, someone else creates/deletes that file, your app gets to run again and does exactly the opposite of what you intended ?
Windows (as well as all UN*X variants) supports file open/create modes that allow to perform that create-if-nonexistant/open-if-existant operation as a single call.
As far as .NET goes, this means for your task (create an XML file) you'd first create a System.IO.FileStream with the appropriate modes, see http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx and then pass that stream to the XmlWriter constructor. That's safer than simply performing an "exists" check and hoping for the best.

Resources