无法写入 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");
据我所知,上面写着:
"Cannot convert from string to System.IO.Stream"
我一直在寻找这个问题的答案,但似乎没有人遇到这个问题。
using System.IO;
using(StreamWriter sw = new StreamWriter("Text.txt"))
{
sw.WriteLine("bla");
}
试试这个。
阅读 StreamWriter
documentation for Windows phone;它需要 System.IO.Stream
,而不是 string
,并且没有 System.IO.string
.
这样的东西
如果你真的可以写一个路径(如果你不能签出isolated storage)你可以这样做:
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 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;
}
}
希望对您有所帮助。
你不能。 Windows Phone does not support 正在访问 SMB/网络文件共享。
好的,所以我需要用 c# 写入家庭网络上的文本文件。问题是我不能对 System.IO.File
使用任何东西,因为 Windows Phone 不支持它。我正在尝试使用 System.IO.StreamWriter
,但这会造成问题。
System.IO.StreamWriter ow;
ow = new System.IO.StreamWriter(@"\RASPBERRYPI\pi\led.txt");
据我所知,上面写着:
"Cannot convert from string to System.IO.Stream"
我一直在寻找这个问题的答案,但似乎没有人遇到这个问题。
using System.IO;
using(StreamWriter sw = new StreamWriter("Text.txt"))
{
sw.WriteLine("bla");
}
试试这个。
阅读 StreamWriter
documentation for Windows phone;它需要 System.IO.Stream
,而不是 string
,并且没有 System.IO.string
.
如果你真的可以写一个路径(如果你不能签出isolated storage)你可以这样做:
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 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;
}
}
希望对您有所帮助。
你不能。 Windows Phone does not support 正在访问 SMB/网络文件共享。