繁体   English   中英

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

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

好的,我需要在c#中写入家庭网络上的文本文件。 问题是我无法使用System.IO.File因为它在Windows Phone上不受支持。 我正在尝试使用System.IO.StreamWriter但这会产生问题。

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

这是我所拥有的,它说:

“无法从字符串转换为System.IO.Stream”

我一直在寻找答案,但似乎没有人有这个问题。

阅读Windows手机的StreamWriter 文档 ; 它需要一个System.IO.Stream ,而不是一个string并且没有System.IO.string这样的东西。

如果您实际上可以写入路径(如果您无法查看隔离存储 ),则可以执行以下操作:

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

我举了一个小例子来说明写作(和阅读)。 这是我的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>

这是说明在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;
    }
}

我希望这有帮助。

你不能。 Windows Phone 不支持访问SMB /网络文件共享。

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

尝试这个。

暂无
暂无

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

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