在這裏你可以找到完整的解決方案。只需將代碼複製/過去到新的WPF應用程序項目。
不要忘記一些程序集的引用,即
System.Web.Extention
System.Web.Script.Serialization
System.IO.IsolatedStorage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MySettings settings = MySettings.Read(true);
Debug.WriteLine("Current value of 'myInteger': " + settings.myInteger);
Debug.WriteLine("Incrementing 'myInteger'...");
settings.myInteger++;
Debug.WriteLine("Saving settings...");
settings.Save(true);
Debug.WriteLine("Done.");
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
MySettings settings = MySettings.Read(true);
Debug.WriteLine("Current value of 'myInteger': " + settings.myInteger);
Debug.WriteLine("Incrementing 'myInteger'...");
settings.myInteger++;
settings.myString = "new S1";
Debug.WriteLine("Saving settings...");
settings.Save(true);
Debug.WriteLine("Done.");
lbl1.Content = settings.myString;
}
}
class MySettings : AppSettings<MySettings>
{
public string myString = "Hello World";
public int myInteger = 1;
}
public class AppSettings<T> where T : new()
{
private const string DEFAULT_FILENAME = "Settings";
private static string GetSettingFileName
{
get
{
var exeName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var fullFileName = string.Format("{0}_{1}.json", exeName, DEFAULT_FILENAME);
return fullFileName;
}
}
public void Save(bool isIsolatedStrorage)
{
if (isIsolatedStrorage)
{
try
{
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(GetSettingFileName, FileMode.Create, IsolatedStorageFile.GetUserStoreForAssembly()))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write((new JavaScriptSerializer()).Serialize(this));
}
}
}
catch (Exception)
{
return;
}
}
else // We will create settings file in the same directory. All users will can access to it. Settings are non user independed.
{
File.WriteAllText(GetSettingFileName, (new JavaScriptSerializer()).Serialize(this));
}
}
public static void Save(T pSettings, bool isIsolatedStrorage)
{
if (isIsolatedStrorage)
{
try
{
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(GetSettingFileName, FileMode.Create, IsolatedStorageFile.GetUserStoreForAssembly()))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write((new JavaScriptSerializer()).Serialize(pSettings));
}
}
}
catch (Exception)
{
return;
}
}
else // We will create settings file in the same directory. All users will can access to it. Settings are non user independed.
{
File.WriteAllText(GetSettingFileName, (new JavaScriptSerializer()).Serialize(pSettings));
}
}
public static T Read(bool isIsolatedStrorage)
{
T t = new T();
if (isIsolatedStrorage)
{
try
{
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(GetSettingFileName, FileMode.Open, IsolatedStorageFile.GetUserStoreForAssembly()))
using (StreamReader reader = new StreamReader(stream))
{
t = (new JavaScriptSerializer()).Deserialize<T>(reader.ReadToEnd());
}
}
catch (Exception)
{
}
}
else // We will create settings file in the same directory. All users will can access to it. Settings are non user independed.
{
if (File.Exists(GetSettingFileName))
t = (new JavaScriptSerializer()).Deserialize<T>(File.ReadAllText(GetSettingFileName));
}
return t;
}
}
}
您可以找到路徑一樣
C語言創建的設置文件:\ Users \用戶名\ AppData的\本地\ IsolatedStorage \ cogrxuh4.gma \ 140esil0.t05 \ Url.bejzwgwbgheugcdwci5jbycjpt0p5rke \ AssemFiles
也許這部分"cogrxuh4.gma\140esil0.t05\Url.bejzwgwbgheugcdwci5jbycjpt0p5rke"
將在你的情況不同。
同樣使用這個類,你可以在本地保存所有設置在你有EXE文件的同一目錄中。
最後,您需要定義一些AppConfigurationManager
類,您可以在其中異步實現所有作業。
可能重複... http://stackoverflow.com/questions/275536/where-is-net-isolated-storage-stored – Vivasaayi