0

I was using Application Settings (as part of Visual Studio) but can't seem to get the other app using the settings from the original app.

Can someone help with storing a few string variables between two apps in .net c#?

EDIT: seems I need to add a reference to my first app from the second app to access the Properties.Default settings - how do I do this?

Marcus
  • 9,011
  • 10
  • 45
  • 65
  • do you want to share your config settings(data) between two projects.... – Glory Raj Dec 11 '11 at 19:46
  • @pratapchandra I'm not 100% sure - I just want to share some string variables between two projects - one is a console app, the other is a simple GUI where users can enter their username/password to be shared with the console app (data will be encrypted) – Marcus Dec 11 '11 at 19:50
  • Perhaps this post can help: [http://stackoverflow.com/questions/2004790/shared-memory-between-2-processes-applications](http://stackoverflow.com/questions/2004790/shared-memory-between-2-processes-applications) – Chris O Dec 12 '11 at 00:58

2 Answers2

3

if you want to share a config between to projects you can just add the file from the other project 'as a link': Right click on the project >> select 'Add existing file' >> navigate to the app.config file >> click the dropdown next to the add button and select add as a link.

or

if you want to share a config between two apps this is the method

I would have a shared assembly, which contains your settings class. You can then serialize/deserialize this class to a common place on the hard drive:

[XmlRoot()]
public class Settings
{
    private static Settings instance = new Settings();

    private Settings() {}

    /// <summary>
    /// Access the Singleton instance
    /// </summary>
    [XmlElement]
    public static Settings Instance
    {
        get
        {
            return instance;
        }
    }

    /// <summary>
    /// Gets or sets the height.
    /// </summary>
    /// <value>The height.</value>
    [XmlAttribute]
    public int Height { get; set; }

    /// <summary>
    /// Main window status (Maximized or not)
    /// </summary>
    [XmlAttribute]
    public FormWindowState WindowState
    {
        get;
        set;
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="Settings"/> is offline.
    /// </summary>
    /// <value><c>true</c> if offline; otherwise, <c>false</c>.</value>
    [XmlAttribute]
    public bool IsSomething
    {
        get;
        set;
    }

    /// <summary>
    /// Save setting into file
    /// </summary>
    public static void Serialize()
    {
        // Create the directory
        if (!Directory.Exists(AppTmpFolder))
        {
            Directory.CreateDirectory(AppTmpFolder);
        }

        using (TextWriter writer = new StreamWriter(SettingsFilePath))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            serializer.Serialize(writer, Settings.Instance);
        }
    }

    /// <summary>
    /// Load setting from file
    /// </summary>
    public static void Deserialize()
    {
        if (!File.Exists(SettingsFilePath))
        {
            // Can't find saved settings, using default vales
            SetDefaults();
            return;
        }
        try
        {
            using (XmlReader reader = XmlReader.Create(SettingsFilePath))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                if (serializer.CanDeserialize(reader))
                {
                    Settings.instance = serializer.Deserialize(reader) as Settings;
                }
            }
        }
        catch (System.Exception)
        {
            // Failed to load some data, leave the settings to default
            SetDefaults();
        }
    }
}

Your xml file will then look like this:

<?xml version="1.0" encoding="utf-8"?>
<Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Height="738" WindowState="Maximized" IsSomething="false" >
</Settings>
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • sounds good, but finding a place to save the file I thought is more tricky due to user permissions? – Marcus Dec 11 '11 at 19:55
2

If you want the same user to use the same settings for both apps OR you want all users to share the same settings, you have a couple of choices besides the standard appsettings:

1) Store the data in the registry. You could either make the settings user specific or global to the machine.

2) Store a structured file, such as an XML file, containing the settings in one of the standard directories: Environment.SpecialFolder.CommonApplicationData for all users or Environment.SpecialFolder.ApplicationData for a single user. This would be the approach I would use.

competent_tech
  • 44,465
  • 11
  • 90
  • 113