简体   繁体   English

无法写入Windows Phone中的文本文件

[英]Can't write to a text file in Windows Phone

Ok so I need to write to a text file on my home network in c#. 好的,我需要在c#中写入家庭网络上的文本文件。 The problem is that I cant use anything with System.IO.File because it is not supported on Windows Phone. 问题是我无法使用System.IO.File因为它在Windows Phone上不受支持。 I'm trying to use System.IO.StreamWriter but this is creating a problem. 我正在尝试使用System.IO.StreamWriter但这会产生问题。

System.IO.StreamWriter ow;
ow = new System.IO.StreamWriter(@"\\RASPBERRYPI\pi\led.txt");

This is as far as I have got and it says: 这是我所拥有的,它说:

"Cannot convert from string to System.IO.Stream" “无法从字符串转换为System.IO.Stream”

I have been searching for an answer for this but nobody seems to have this problem. 我一直在寻找答案,但似乎没有人有这个问题。

Read the StreamWriter documentation for Windows phone; 阅读Windows手机的StreamWriter 文档 ; it takes a System.IO.Stream , not a string and there is no such thing as System.IO.string . 它需要一个System.IO.Stream ,而不是一个string并且没有System.IO.string这样的东西。

If you can actually write to a path (and if you can't check out isolated storage ) you can do this: 如果您实际上可以写入路径(如果您无法查看隔离存储 ),则可以执行以下操作:

using (var fs = new FileStream(@"\\RASPBERRYPI\pi\led.txt", FileMode.CreateNew)
using (var writer = new StreamWriter(fs))
{
    writer.Write(textToAdd);
}

I've made a small example to illustrate writing (and reading). 我举了一个小例子来说明写作(和阅读)。 This is my xaml: 这是我的xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <RichEditBox Name="inputTxtBx" Margin="10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
                 AcceptsReturn="True"/>
    <Button Grid.Row="1" Name="write" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Write"
            Click="write_Click"/>
    <TextBlock Name="outputTxtBlk" Grid.Row="2" Margin="10" VerticalAlignment="Stretch" 
               Style="{StaticResource BodyTextBlockStyle}"/>
    <Grid Grid.Row="3" HorizontalAlignment="Stretch">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button Grid.Column="0" Name="read" VerticalAlignment="Center" HorizontalAlignment="Center" 
                Content="Read" Click="read_Click" Margin="5"/>
        <Button Grid.Column="1" Name="delete" VerticalAlignment="Center" HorizontalAlignment="Center" 
                Content="Delete" Click="delete_Click" Margin="5"/>
    </Grid>
</Grid>

And this is the code behind illustrating reading and writing to a file on a windows phone: 这是说明在Windows手机上读取和写入文件的代码:

private async void write_Click(object sender, RoutedEventArgs e)
{
    await WriteToFile();
}

private StorageFile file;
private async Task WriteToFile()
{
    // Create a new file named "outputFile.txt"
    file = await ApplicationData.Current.LocalFolder.CreateFileAsync("outputFile.txt", CreationCollisionOption.OpenIfExists);

    // Get the string in inputBx
    string toWrite;
    inputTxtBx.Document.GetText(TextGetOptions.AdjustCrlf, out toWrite);

    // Write the data from the textbox
    using(var stream = await file.OpenStreamForWriteAsync())
    {
        using(var writer = new StreamWriter(stream))
        {
            await writer.WriteAsync(toWrite);
        }
    }

    inputTxtBx.Document.SetText(TextSetOptions.ApplyRtfDocumentDefaults, "");
}

private async void read_Click(object sender, RoutedEventArgs e)
{
    if(file == null)
    {
        outputTxtBlk.Text = "You have to write first!";
    }
    else
    {
        // Read the data from disk
        using(var stream = await file.OpenStreamForReadAsync())
        {
            using(var reader = new StreamReader(stream))
            {
                outputTxtBlk.Text = await reader.ReadToEndAsync();
            }
        }
    }
}

private async void delete_Click(object sender, RoutedEventArgs e)
{
    if(file == null)
    {
        outputTxtBlk.Text = "You have to write first!";
    }
    else
    {
        await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
        outputTxtBlk.Text = "File purged forever!";
        file = null;
    }
}

I hope this helps. 我希望这有帮助。

You can't. 你不能。 Windows Phone does not support accessing SMB / network file shares. Windows Phone 不支持访问SMB /网络文件共享。

using System.IO;
using(StreamWriter sw = new StreamWriter("Text.txt"))
{
    sw.WriteLine("bla");
}

Try this. 尝试这个。

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

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