Can't write text file on Windows Phone

So, I need to write a text file on my home network in C #. The problem is that I cannot use anything with System.IO.File

because it is not supported on Windows Phone. I am trying to use System.IO.StreamWriter

but this creates a problem.

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

      

This is how I understood it and he says:

"Cannot convert from string to System.IO.Stream"

I was looking for an answer for this, but it doesn't seem to have this problem.

+3


source to share


4 answers


You can not. Windows Phone does not support access to SMB / network shares.



+1


source


Read the StreamWriter

documentation for your Windows Phone; it accepts System.IO.Stream

, not a string

, and there is no such thing as System.IO.string

.

If you can actually write the path (and if you cannot check 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);
}

      

+1


source


I made a small example to illustrate writing (and reading). This is my 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 to illustrate how to read and write a file on a Windows phone:

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;
    }
}

      

Hope this helps.

+1


source


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

      

Try it.

-1


source







All Articles