问题描述
|
在一个解决方案中,我有两个Windows Forms应用程序和库。
库类可以在IsolatedStorage中创建新的文件夹和文件,并在IsolatedStorage中列出所有文件和文件夹。
第一个应用程序使用库类来创建新的文件夹/文件
我要第二个列出第一个应用程序创建的文件夹。
如何使它们使用相同的隔离存储?
解决方法
使用
IsolatedStorageFile.GetUserStoreForAssembly
从库创建隔离存储。
详情在这里
您可以在库中使用以下类型。并且application1和application2可以通过您的库中的以下类型向同一隔离的存储进行写入/读取。
下面:
public class UserSettingsManager
{
private IsolatedStorageFile isolatedStorage;
private readonly String applicationDirectory;
private readonly String settingsFilePath;
public UserSettingsManager()
{
this.isolatedStorage = IsolatedStorageFile.GetMachineStoreForAssembly();
this.applicationDirectory = \"UserSettingsDirectory\";
this.settingsFilePath = String.Format(\"{0}\\\\settings.xml\",this.applicationDirectory);
}
public Boolean WriteSettingsData(String content)
{
if (this.isolatedStorage == null)
{
return false;
}
if (! this.isolatedStorage.DirectoryExists(this.applicationDirectory))
{
this.isolatedStorage.CreateDirectory(this.applicationDirectory);
}
using (IsolatedStorageFileStream fileStream =
this.isolatedStorage.OpenFile(this.settingsFilePath,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.Write))
using (StreamWriter streamWriter = new StreamWriter(fileStream))
{
streamWriter.Write(content);
}
return true;
}
public String GetSettingsData()
{
if (this.isolatedStorage == null)
{
return String.Empty;
}
using(IsolatedStorageFileStream fileStream =
this.isolatedStorage.OpenFile(this.settingsFilePath,System.IO.FileMode.Open,System.IO.FileAccess.Read))
using(StreamReader streamReader = new StreamReader(fileStream))
{
return streamReader.ReadToEnd();
}
}
}
编辑:
dll应该是一个重命名的程序集。下面的快照显示了如何向程序集添加强名称。