簡體   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