使用方法中的变量

时间:2015-06-02 22:43:20

标签: c# batch-file windows-store-apps windows-store

  

如何从方法中调出string[] phpcorrect?   如果有任何简单的解决方案可以做到这一点,请分享   如果有更接近Windows.Storage.FileIO.ReadTextAsync的方法,请分享。

public async void ReadFile()
{
    // settings
    var path = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php1.txt";
    var path1 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php2.txt";
    var path2 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php3.txt";
    var path3 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php4.txt";
    var path4 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php5.txt";
    var path5 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php6.txt";
    var path6 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php7.txt";
    var path7 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php8.txt";
    var path8 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php9.txt";
    var path9 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php10.txt";
    var path10 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php11.txt";
    var path11 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php12.txt";
    var path12 = @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php13.txt";
    var path13= @"F:\Designe- Video\projects\Phpgonehelp\Phpgonehelp\PHPCODE\Php14.txt";

    var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

    // acquire file
    var file = await folder.GetFileAsync(path);
    var file1 = await folder.GetFileAsync(path1);
    var file2 = await folder.GetFileAsync(path2);
    var file3 = await folder.GetFileAsync(path3);
    var file4 = await folder.GetFileAsync(path4);
    var file5 = await folder.GetFileAsync(path5);
    var file6 = await folder.GetFileAsync(path6);
    var file7 = await folder.GetFileAsync(path7);
    var file8 = await folder.GetFileAsync(path8);
    var file9 = await folder.GetFileAsync(path9);
    var file10 = await folder.GetFileAsync(path10);
    var file11 = await folder.GetFileAsync(path11);
    var file12 = await folder.GetFileAsync(path12);
    var file13 = await folder.GetFileAsync(path13);
    var file14 = await folder.GetFileAsync(path13);

    string[] phpcorrect = { await Windows.Storage.FileIO.ReadTextAsync(file),
                            await Windows.Storage.FileIO.ReadTextAsync(file1),
                            await Windows.Storage.FileIO.ReadTextAsync(file2),
                            await Windows.Storage.FileIO.ReadTextAsync(file3),
                            await Windows.Storage.FileIO.ReadTextAsync(file4),
                            await Windows.Storage.FileIO.ReadTextAsync(file5),
                            await Windows.Storage.FileIO.ReadTextAsync(file6),
                            await Windows.Storage.FileIO.ReadTextAsync(file7),
                            await Windows.Storage.FileIO.ReadTextAsync(file8),
                            await Windows.Storage.FileIO.ReadTextAsync(file9),
                            await Windows.Storage.FileIO.ReadTextAsync(file10),
                            await Windows.Storage.FileIO.ReadTextAsync(file11),
                            await Windows.Storage.FileIO.ReadTextAsync(file12),
                            await Windows.Storage.FileIO.ReadTextAsync(file13)
                           };
    }

1 个答案:

答案 0 :(得分:1)

  

我怎么能从方法中调用字符串[] phpcorret?

你的意思是你想返回那个值吗?像这样:

public async Task<string[]> ReadFile()
{
    // the code you already have

    return phpcorrect;
}

现在消费代码可以使用该值:

var phpcorrect = await ReadFile();

请注意,这也解决了您使用 不应该执行的async void这一事实。异步方法应始终至少返回Task,以便等待它。否则无法观察到该方法中的错误,也无法保证该方法的完成。