I think I see what you're trying to do, Andrus.
You might want to look into IsolatedStorage.
Here is some psudo code:
if (IsolatedStorageFileExists == false) {
CreateIsolatedStorageFile();
}
// Now your isolated storage file is guaranteed
// to exist, so you can read your values:
OpenIsolatedStorageFile();
ReadContentsFromIsolatedStorageFile();
Here is some sample code:
using System.IO.IsolatedStorage;
public Form1() {
Cursor = Cursors.WaitCursor;
Enabled = false;
try {
InitializeComponent();
m_ready = false;
m_about = new frmAbout();
m_ds = new DataSet("AIO_Test_Results");
GetCpAppData();
m_autoGenNote = new List<string>();
m_htmlHead = new List<string>();
m_ds = new DataSet();
IsolatedStorageFile store = null;
IsolatedStorageFileStream settings = null;
StreamReader reader = null;
try {
store = .IsolatedStorageFile.GetUserStoreForApplication();
settings = new IsolatedStorageFileStream("Form1.cfg", FileMode.Open,
store);
reader = new StreamReader(settings);
this.Top = ToInt32(reader.ReadLine()); // Form's Location X
this.Left = ToInt32(reader.ReadLine()); // Form's Location Y
this.Height = ToInt32(reader.ReadLine()); // Form's Height
this.Width = ToInt32(reader.ReadLine()); // Form's Width
string state = reader.ReadLine(); // Form's WindowState
if (state.Contains("Maximized") == true) {
this.WindowState = FormWindowState.Maximized;
} else if (state.Contains("Minimized") == true) {
this.WindowState = FormWindowState.Minimized;
} else {
this.WindowState = FormWindowState.Normal;
}
} catch (Exception err) { // perhaps file wasn't there
Console.WriteLine("IsolateStorage StreamReader Error: " +
err.ToString());
} finally {
if (store != null) {
if (settings != null) {
if (reader != null) {
reader.Close();
}
settings.Dispose();
}
store.Dispose();
}
}
} finally {
Enabled = true;
Cursor = Cursors.Default;
}
}
private void Form1_Closing(object sender, FormClosingEventArgs e) {
sessionTimer.Stop();
try {
IsolatedStorageFile store =
IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream settings = new
IsolatedStorageFileStream("form1.cfg", FileMode.Create, store);
StreamWriter writer = new StreamWriter(settings);
writer.WriteLine(this.Top.ToString()); // Form's Top
writer.WriteLine(this.Left.ToString()); // Form's Left
writer.WriteLine(this.Height.ToString()); // Form's Height
writer.WriteLine(this.Width.ToString()); // Form's Width
writer.WriteLine(this.WindowState.ToString()); // Form's WindowState
writer.Close();
settings.Dispose();
store.Dispose();
} catch (Exception err) {
Console.WriteLine("Form Close Error: " + err.ToString());
} // just doesn't save if there's an error
}
"Andrus" wrote:
Can you explain in more detail what you are trying to do?
Application can open 100 different MDI Forms. Every form has different name
assignet to it, like MainForm, CustomerEditForm etc.
On open each form should read its position and size from user settings. On
close it should save new settins to user-specific area in disk
(c:\Users\Jon\AppData\Local subdirectory in Vista).
When appl is installed to new computer, default values for forms positon and
size should be retrieved, different for every form name.
Best way I found is to read those from app.config file special section:
<configuration>
<configSections>
<sectionGroup name="formSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="FormSetting"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
.....
</configSections>
<formSettings>
<FormSetting>
<setting name="CustomerEditForm" serializeAs="Xml">
<value>
<Location>
<X>5</X>
<Y>10</Y>
</Location>
<Size>
<Width>200</Width>
<Height>100</Height>
</Size>
</value>
</setting>
<setting name="ProductEditForm" serializeAs="Xml">
<value>
<Location>
<X>15</X>
<Y>20</Y>
</Location>
<Size>
<Width>230</Width>
<Height>80</Height>
</Size>
</value>
</setting>
....
</FormSetting>
</formSettings>
.....
In appl config time I want to add menu command "Save this form size and
position as default if user specific setting is not present".
This command should write or update userSettings/FormDefault section with
current form position and size in app.config file.
If application is used by new windows user, forms are opened in location and
size set by this command.
Every windows user can change form positon from default to other. This new
position is stored in user settins area using code below.
How to change lines
Properties.Settings.Default[ this.Name ] = formData;
Properties.Settings.Default.Save();
so that it stores setting to user settings area just like regular
userSetting ?
[DataContract]
public class FormData {
[DataMember]
public Point Location;
[DataMember]
public Size ClientSize;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
var formData = new FormData();
formData.Location = this.Location;
formData.Size = this.Size;
// How to save settings to formSettings group :
Properties.Settings.Default[ this.Name ] = formData;
Properties.Settings.Default.Save();
}
Andrus.