C# 我可以获取一个独立存储文件的路径并从外部应用程序读取它吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1112681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:31:59  来源:igfitidea点击:

Can I get a path for a IsolatedStorage file and read it from external applications?

c#.netisolatedstorage

提问by Jader Dias

I want to write a file where an external application can read it, but I want also some of the IsolatedStorage advantages, basically insurance against unexpected exceptions. Can I have it?

我想写一个外部应用程序可以读取它的文件,但我也想要一些隔离存储的优势,基本上是防止意外异常的保险。我可以拥有吗?

采纳答案by mlessard

You can retrieve the path of an isolated storage file on disk by accessing a private field of the IsolatedStorageFileStreamclass, by using reflection. Here's an example:

您可以IsolatedStorageFileStream通过使用反射访问类的私有字段来检索磁盘上隔离存储文件的路径。下面是一个例子:


// Create a file in isolated storage.
IsolatedStorageFile store = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
IsolatedStorageFileStream stream = new IsolatedStorageFileStream("test.txt", FileMode.Create, store);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("Hello");
writer.Close();
stream.Close();

// Retrieve the actual path of the file using reflection.
string path = stream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream).ToString();

I'm not sure that's a recommended practice though.

不过,我不确定这是推荐的做法。

Keep in mind that the location on disk depends on the version of the operation system and that you will need to make sure your other application has the permissions to access that location.

请记住,磁盘上的位置取决于操作系统的版本,您需要确保其他应用程序有权访问该位置。

回答by Mohamed Abed

Instead of creating a temp file and get the location you can get the path from the store directly:

您可以直接从商店获取路径,而不是创建临时文件并获取位置:

var path = store.GetType().GetField("m_RootDir", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(store).ToString();

回答by Tayyab Akram

I use Name property of FileStream.

我使用 FileStream 的 Name 属性。

private static string GetAbsolutePath(string filename)
{
    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

    string absoulutePath = null;

    if (isoStore.FileExists(filename))
    {
        IsolatedStorageFileStream output = new IsolatedStorageFileStream(filename, FileMode.Open, isoStore);
        absoulutePath = output.Name;

        output.Close();
        output = null;
    }

    return absoulutePath;
}

This code is tested in Windows Phone 8 SDK.

此代码在 Windows Phone 8 SDK 中进行了测试。