本文介绍了如何为两个 Windows 窗体应用程序共享一个独立存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个解决方案中有两个 Windows 窗体应用程序和库.

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

Library类可以在IsolatedStorage中创建新的文件夹和文件,并列出IsolatedStorage中的所有文件和文件夹.

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/filesI want the second one to list folders created by first app.How can i make them use the same isolated storage?

推荐答案

使用 IsolatedStorageFile.GetUserStoreForAssembly 从库创建隔离存储.

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

详细信息 这里

您可以在库中使用以下类型.应用程序 1 和应用程序 2 可以通过库中的以下类型写入/读取同一个独立存储.

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.

以下:

 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 应该是一个强命名的程序集.下面的快照显示了如何向程序集添加强名称.

The dll should be a strongly named assembly. Below snapshots show how to add a strong name to the assembly.

这篇关于如何为两个 Windows 窗体应用程序共享一个独立存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:42
查看更多