之前清除文件我使用這個代碼寫入到我的文件:如何寫進去
private async void play_Click(object sender, RoutedEventArgs e)
{
String MyScore;
Double previousScore = 0;
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var dataFolder1 = await local.CreateFolderAsync("MyFolder", CreationCollisionOption.OpenIfExists);
var file1 = await dataFolder1.CreateFileAsync("MyFile.txt", CreationCollisionOption.OpenIfExists);
var file = await dataFolder1.OpenStreamForReadAsync("MyFile.txt");
using (StreamReader streamReader = new StreamReader(file))
{
MyScore = streamReader.ReadToEnd();
}
if (MyScore != null && !MyScore.Equals(""))
{
previousScore = Convert.ToDouble(MyScore);
}
Double CurerentScore = 0;
Double Total = 0;
String scoreText = this.ScoreTB.Text;
CurerentScore = Convert.ToDouble(scoreText);
Total = previousScore - CurerentScore;
using (var s = await file1.OpenStreamForWriteAsync())
{
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(Convert.ToString(Total));
s.Write(fileBytes, 0, fileBytes.Length);
}
}
但寫進去之前,我想我的文件應該被清除。我該怎麼辦?
這是我到目前爲止所嘗試的,但問題是它將文件寫入filebytes.length,並且由於該文件在文件中寫入的新信息與長度相比較少privous長度那麼一些垃圾值或unnecessay的事情來了新的文件結束後
其實我使用fileBytes.Length因爲我的文件所著高達它的長度(假設我有我的文件135的下一次我subtarcting 40所以結果應該是95,但答案是955一個額外的5來自135,因爲135有3個數字,所以fileBytes.length使95到三位數,結果是955) – zap92
你試過了什麼? – Thomas