好的,我需要在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”
我一直在寻找答案,但似乎没有人有这个问题。
答案 0 :(得分:1)
阅读Windows手机的StreamWriter
documentation;它需要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);
}
答案 1 :(得分:1)
我做了一个小例子来说明写作(和阅读)。这是我的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;
}
}
我希望这会有所帮助。
答案 2 :(得分:1)
你不能。 Windows Phone does not support访问SMB /网络文件共享。
答案 3 :(得分:-1)
using System.IO;
using(StreamWriter sw = new StreamWriter("Text.txt"))
{
sw.WriteLine("bla");
}
试试这个。