Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / SettingsProperty property - создания( save/load settings ) / 7 сообщений из 7, страница 1 из 1
05.09.2013, 11:50
    #38387627
wiktor591
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
SettingsProperty property - создания( save/load settings )
Подскажите парни, вот создаю я CustomSetting записываю туда текст.
В итоге в файл user запись записала, но когда я хочу загрузить её обратно в radTextBox1 она нечего не загружает.( это если я с программы выйду) А если в программу не закрыть, то все загружает.

Подскажите, кто сталкивался с такой проблемой.

Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Addsettings
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("CustomSetting");
            property.SerializeAs = SettingsSerializeAs.ProviderSpecific;
            property.DefaultValue = "Default";
            property.IsReadOnly = false;
            property.PropertyType = typeof(string);
            property.Provider = Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
            property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
            Properties.Settings.Default.Properties.Add(property);

            // Load settings now.

            Properties.Settings.Default.Reload();

            // Update the user itnerface.

            radTextBox1.Text = (string)Properties.Settings.Default["CustomSetting"];
            Properties.Settings.Default.Save();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default["CustomSetting"] = radTextBox1.Text;
            
        }
    }
}
...
Рейтинг: 0 / 0
05.09.2013, 12:02
    #38387653
wiktor591
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
SettingsProperty property - создания( save/load settings )
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Addsettings
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            
            radTextBox1.Text = (string)Properties.Settings.Default["CustomSetting"];
        }

        private void radButton2_Click(object sender, EventArgs e)
        {
            System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("CustomSetting");
            property.SerializeAs = SettingsSerializeAs.Xml;
            property.DefaultValue = "Default";
            property.IsReadOnly = false;
            property.PropertyType = typeof(string);
            property.Provider = Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
            property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
            Properties.Settings.Default.Properties.Add(property);

            // Load settings now.

            Properties.Settings.Default.Reload();

            // Update the user itnerface.

            Properties.Settings.Default["CustomSetting"] = radTextBox1.Text;
            Properties.Settings.Default.Save();
        }
    }
}



исправил немного, теперь выдает ошибку вот такую.

System.Configuration.SettingsPropertyNotFoundException was unhandled

The settings property 'CustomSetting' was not found.

хотя настройки сохранены в файл.
...
Рейтинг: 0 / 0
05.09.2013, 12:25
    #38387704
fortibransa
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
SettingsProperty property - создания( save/load settings )
А попробуй не из студии (не из дебагера) запустить
...
Рейтинг: 0 / 0
05.09.2013, 12:39
    #38387734
wiktor591
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
SettingsProperty property - создания( save/load settings )
fortibransa,

Ситуация такая:

Если приложение открытое, то оно загружает текст, который есть и не выдает ошибку.

Если приложение закрыть, текст сохраняется в файл

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<Addsettings.Properties.Settings>
<setting name="CustomSetting" serializeAs="Xml">
<value>
<string>FFFFFFFF</string>
</value>
</setting>
</Addsettings.Properties.Settings>
</userSettings>
</configuration>

А вот уже при запуске программы при загрузки его в текстбокс выдает ошибку.
...
Рейтинг: 0 / 0
05.09.2013, 14:33
    #38387959
fortibransa
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
SettingsProperty property - создания( save/load settings )
Ну, я тебе про что говорю, сам экзэшник запусти не из студии.
те, запусти его, нажми radButton2, закрой. Снова запусти экзэшник, нажми radButton1
...
Рейтинг: 0 / 0
05.09.2013, 16:53
    #38388185
wiktor591
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
SettingsProperty property - создания( save/load settings )
fortibransa,

ошибку все равно пишет
...
Рейтинг: 0 / 0
05.09.2013, 16:54
    #38388191
wiktor591
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
SettingsProperty property - создания( save/load settings )
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Addsettings
{
    public partial class Form1 : Form
    {
        //ensures no runtime errors if you try and view the setting before its created
        private bool _customSettingExists = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            //You're saving your CustomSetting to properties, so you should read it from Default.Properties
            if (_customSettingExists)
                radTextBox1.Text = (string)Properties.Settings.Default.Properties["CustomSetting"].ToString();
        }

        private void radButton2_Click(object sender, EventArgs e)
        {
            System.Configuration.SettingsProperty property = new
            System.Configuration.SettingsProperty("CustomSetting");
            property.SerializeAs = SettingsSerializeAs.Xml;
            property.DefaultValue = "Default";
            property.IsReadOnly = false;
            property.PropertyType = typeof(string);
            property.Provider =
            Properties.Settings.Default.Providers["LocalFileSettingsProvider"];

            property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), 
                new System.Configuration.UserScopedSettingAttribute());
            Properties.Settings.Default.Properties.Add(property);

            // Load settings now.

           // Properties.Settings.Default.Reload();

            // Update the user itnerface.
           
            Properties.Settings.Default["CustomSetting"] = radTextBox1.Text;
            Properties.Settings.Default.Save();

            //now that you know a custom setting exists
            _customSettingExists = true;
        }
    }
}



переделал, теперь пишет вот такое.

System.NullReferenceException was unhandled Object reference not set to an instance of an object.

вот тут
Код: c#
1.
Properties.Settings.Default["CustomSetting"] = radTextBox1.Text; 



Хотя объект создался, что ему еще нужно хз.
...
Рейтинг: 0 / 0
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / SettingsProperty property - создания( save/load settings ) / 7 сообщений из 7, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]