简体   繁体   English

C#Windows 8应用程序创建文件

[英]C# windows 8 app creating a file

I'm trying to create a file then write to the file and read from it as well... kind of like settings for my app to load every time it loads. 我正在尝试创建一个文件,然后写入该文件并从中读取...有点像我的应用程序每次加载时都要加载的设置。 Why is this not working for me? 为什么这对我不起作用? I'm running visual studio 2012 and I think when I run the program there the file should be created in the project's folder... my method it's async and void... don't really know what is going on haha 我正在运行Visual Studio 2012,我认为当我运行程序时应该在项目的文件夹中创建文件...我的方法是异步和无效的...真的不知道发生了什么哈哈

 StorageFile sampleFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("config.txt", CreationCollisionOption.ReplaceExisting);

How can I create this in the local folder? 如何在本地文件夹中创建此文件? so every time the program runs no matter in what computer it will create the file and load it when the user close and re-open the program? 因此,无论程序何时在任何计算机上运行,​​它都会创建文件并在用户关闭并重新打开程序时加载该文件?

Man, great question! 伙计,很好的问题!

Here's the exact logic to do what you are asking: 这是您要执行的操作的确切逻辑:

public class MyData
{
    public string Title { get; set; }
}

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        this.DataContext = await LoadData();
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        SaveData(this.DataContext as MyData);
        base.OnNavigatedFrom(e);
    }

    private async Task<MyData> LoadData()
    {
        var _Data = await StorageHelper.ReadFileAsync<MyData>(
            this.GetType().ToString(), StorageHelper.StorageStrategies.Local);
        return _Data ?? new MyData() { Title = "Welcome" };
    }

    private async void SaveData(MyData data)
    {
        await StorageHelper.WriteFileAsync(
            this.GetType().ToString(), data, StorageHelper.StorageStrategies.Local);
    }
}

The StorageHelper class can be found here . 可以在这里找到StorageHelper类。 or on my blog http://jerrynixon.com 或在我的博客http://jerrynixon.com上

Best of luck! 祝你好运!

How can I create this in the local folder? 如何在本地文件夹中创建此文件?

You can't, and anyway you're not supposed to... Windows Store apps run in a sandbox, they have a very restricted access to the file system. 您不能,而且无论如何都不应该... Windows应用商店应用程序在沙箱中运行,它们对文件系统的访问非常受限。 Basically, you have access to: 基本上,您可以访问:

  • your app's LocalFolder (which is not the installation folder) and RoamingFolder 您应用的LocalFolder不是安装文件夹)和RoamingFolder
  • documents library, pictures library, music library, etc, depending on your app's capabilities (declared in the app manifest) 文档库,图片库,音乐库等,具体取决于您应用的功能(在应用清单中声明)
  • files selected by the user using a file picker 用户使用文件选择器选择的文件

And I think that's about it... You can also access the files in the installation folder ( Package.Current.InstallationFolder ), but only for reading. 我想就是这样...您还可以访问安装文件夹( Package.Current.InstallationFolder )中的文件,但仅供阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM