Click here to Skip to main content
15,897,518 members
Home / Discussions / C#
   

C#

 
GeneralRe: IP Address Pin
Som Shekhar15-Feb-10 22:13
Som Shekhar15-Feb-10 22:13 
GeneralRe: IP Address Pin
sanforjackass15-Feb-10 22:08
sanforjackass15-Feb-10 22:08 
GeneralRe: IP Address Pin
Som Shekhar15-Feb-10 22:10
Som Shekhar15-Feb-10 22:10 
GeneralRe: IP Address [modified] Pin
sanforjackass15-Feb-10 22:15
sanforjackass15-Feb-10 22:15 
QuestionAutomatically resize the controls based on screen resolution in C# application Pin
abyclassic15-Feb-10 2:03
abyclassic15-Feb-10 2:03 
AnswerRe: Automatically resize the controls based on screen resolution in C# application Pin
Marcel Gheorghita15-Feb-10 2:24
Marcel Gheorghita15-Feb-10 2:24 
GeneralRe: Automatically resize the controls based on screen resolution in C# application Pin
abyclassic18-Feb-10 2:11
abyclassic18-Feb-10 2:11 
QuestionCompact framework: how to read and write to a XML file Pin
nuttynibbles15-Feb-10 1:58
nuttynibbles15-Feb-10 1:58 
Hi,

i got a AppSettings.exe.xml to read and write to. it looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="dir" value="\directory"/>
    <add key="fieldname1" value="\value1"/>
    <add key="fieldname2" value="\value2"/>
  </appSettings>
</configuration>


reading frm xml is not of a problem. but writing is..
everytime i write to the xml, the last char will get trim off.

for example if i wanna write or append, the xml will look like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="dir" value="\directory"/>
    <add key="fieldname1" value="\value1"/>
    <add key="fieldname2" value="\value2"/>
  </appSettings>
</configuration


note: the missing '>'. And if writing continue, more chars will get trim off

im using this class to do the read and write to xml:
namespace used:
using System;
using System.Xml;
using System.Configuration;
using System.Reflection;

using OpenNETCF.Configuration;
using System.Windows.Forms;


public class ConfigSettings
    {
        private ConfigSettings() { }

        public static string ReadSetting(string key)
        {
            return ConfigurationSettings.AppSettings[key];
        }

        public static void WriteSetting(string key, string value)
        {
            // load config document for current assembly
            XmlDocument doc = loadConfigDocument();

            // retrieve appSettings node
            XmlNode node = doc.SelectSingleNode("//appSettings");

            if (node == null)
                throw new InvalidOperationException("appSettings section not found in config file.");

            try
            {
                // select the 'add' element that contains the key
                XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
                if (elem != null)
                {
                    // add value for key
                    elem.SetAttribute("value", value);
                }
                else
                {
                    // key was not found so create the 'add' element 
                    // and set it's key/value attributes 
                    elem = doc.CreateElement("add");
                    elem.SetAttribute("key", key);
                    elem.SetAttribute("value", value);
                    node.AppendChild(elem);
                }
                doc.Save(getConfigFilePath());
            }
            catch
            {
                throw;
            }
        }

        public static void RemoveSetting(string key)
        {
            // load config document for current assembly
            XmlDocument doc = loadConfigDocument();

            // retrieve appSettings node
            XmlNode node = doc.SelectSingleNode("//appSettings");

            try
            {
                if (node == null)
                    throw new InvalidOperationException("appSettings section not found in config file.");
                else
                {
                    // remove 'add' element with coresponding key
                    node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));
                    doc.Save(getConfigFilePath());
                }
            }
            catch (NullReferenceException e)
            {
                throw new Exception(string.Format("The key {0} does not exist.", key), e);
            }
        }

        private static XmlDocument loadConfigDocument()
        {
            XmlDocument doc = null;
            try
            {
                doc = new XmlDocument();
                doc.Load(getConfigFilePath());
                return doc;
            }
            catch (System.IO.FileNotFoundException e)
            {
                throw new Exception("No configuration file found.", e);
            }
        }

        private static string getConfigFilePath()
        {
            return @"\Program Files\AppSetting.exe.xml";
        }
    }

Questionmouse click event of textbox?? Pin
sudhir behera15-Feb-10 1:41
sudhir behera15-Feb-10 1:41 
AnswerRe: mouse click event of textbox?? Pin
OriginalGriff15-Feb-10 1:47
mveOriginalGriff15-Feb-10 1:47 
GeneralRe: mouse click event of textbox?? Pin
Luc Pattyn15-Feb-10 2:02
sitebuilderLuc Pattyn15-Feb-10 2:02 
Questionshowing password ****** Pin
naghoumeh1415-Feb-10 0:51
naghoumeh1415-Feb-10 0:51 
AnswerRe: showing password ****** Pin
Abhinav S15-Feb-10 0:52
Abhinav S15-Feb-10 0:52 
AnswerRe: showing password ****** [modified] Pin
OriginalGriff15-Feb-10 1:13
mveOriginalGriff15-Feb-10 1:13 
GeneralRe: showing password ****** Pin
Abhinav S15-Feb-10 4:43
Abhinav S15-Feb-10 4:43 
Questionshow a webcam photo Pin
naghoumeh1415-Feb-10 0:21
naghoumeh1415-Feb-10 0:21 
AnswerRe: show a webcam photo Pin
Nematjon Rahmanov15-Feb-10 0:33
Nematjon Rahmanov15-Feb-10 0:33 
GeneralRe: show a webcam photo Pin
naghoumeh1415-Feb-10 0:59
naghoumeh1415-Feb-10 0:59 
GeneralRe: show a webcam photo Pin
Nematjon Rahmanov15-Feb-10 1:31
Nematjon Rahmanov15-Feb-10 1:31 
Question[NOT SOLVED ANYMORE] Renaming fields / Controlling access to fields [modified] Pin
blackblizzard15-Feb-10 0:10
blackblizzard15-Feb-10 0:10 
AnswerRe: Renaming fields / Controlling access to fields Pin
Luc Pattyn15-Feb-10 0:32
sitebuilderLuc Pattyn15-Feb-10 0:32 
GeneralRe: Renaming fields / Controlling access to fields Pin
blackblizzard15-Feb-10 1:15
blackblizzard15-Feb-10 1:15 
QuestionThe suggested solution doesn't work: help, please Pin
blackblizzard22-Feb-10 3:23
blackblizzard22-Feb-10 3:23 
AnswerRe: The suggested solution doesn't work: help, please Pin
Luc Pattyn22-Feb-10 10:49
sitebuilderLuc Pattyn22-Feb-10 10:49 
GeneralRe: The suggested solution doesn't work: help, please Pin
blackblizzard22-Feb-10 22:38
blackblizzard22-Feb-10 22:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.