我想将文件存储在我的 Windows 8 或 Windows 7 独立存储位置。?知道位置,我想在该位置放置一些文件。那么,如何获取隔离存储文件的位置..?
using(IsolatedStorageFile isoPath=IsolatedStorageFile.GetUserStoreForApplication())
{
}
我们在 IsolatedStorageFile.GetUserStoreForApplication()... 上获得的位置是什么?
我想将文件存储在我的 Windows 8 或 Windows 7 独立存储位置。?知道位置,我想在该位置放置一些文件。那么,如何获取隔离存储文件的位置..?
using(IsolatedStorageFile isoPath=IsolatedStorageFile.GetUserStoreForApplication())
{
}
我们在 IsolatedStorageFile.GetUserStoreForApplication()... 上获得的位置是什么?
在这里您可以找到完整的解决方案。只需将代码复制/粘贴到新的 WPF 应用程序项目中即可。
不要忘记一些程序集参考,即
System.Web.Extension
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\USER_NAME\AppData\Local\IsolatedStorage\cogrxuh4.gma\140esil0.t05\Url.bejzwgwbgheugcdwci5jbycjpt0p5rke\AssemFiles
在您的情况下,这部分"cogrxuh4.gma\140esil0.t05\Url.bejzwgwbgheugcdwci5jbycjpt0p5rke"
可能会有所不同。
同样使用此类,您可以将所有设置本地保存在您拥有EXE文件的同一目录中。
最后,您将需要定义一些AppConfigurationManager
可以异步实现所有作业的类。