How to share one IsolatedStorage for two Windows Forms Applications?

和自甴很熟 提交于 2019-12-23 18:39:50

问题


I have two Windows Forms applications and library in one solution.

Library class can create new folders and files in IsolatedStorage and list all files and folders in IsolatedStorage.

First application uses library class to create new folders/files I want the second one to list folders created by first app. How can i make them use the same isolated storage?


回答1:


Use IsolatedStorageFile.GetUserStoreForAssembly to create isolated storage from the library.

Details here

You could use the below type in your library. And the application1 and application2 can write/read to/from the same isolated storage via the below type in your library.

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.



来源:https://stackoverflow.com/questions/6167622/how-to-share-one-isolatedstorage-for-two-windows-forms-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!