我在一个解决方案中有两个 Windows 窗体应用程序和库。
Library类可以在IsolatedStorage中创建新的文件夹和文件,并列出IsolatedStorage中的所有文件和文件夹。
第一个应用程序使用库类创建新文件夹/文件
我希望第二个列出第一个应用程序创建的文件夹。
我怎样才能让他们使用相同的隔离存储?
Use IsolatedStorageFile.GetUserStoreForAssembly
创建与库隔离的存储。
Details here http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.getuserstoreforassembly%28v=vs.80%29.aspx
您可以在您的库中使用以下类型。 application1 和 application2 可以写入/读取相同的隔离存储通过您的库中的以下类型。
Below:
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();
}
}
}
EDIT:
The dll should be a strongly named assembly. Below snapshots show how to add a strong name to the assembly.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)