2012-11-12 44 views
0

保存用戶信息,不知何故,我得到一個「不允許例外」,我似乎無法弄清楚什麼是錯的,我知道的答案很簡單,但我真的不能發現有什麼錯我的代碼... 它發生時,我嘗試寫一些東西到新創建的文件,我可以想像,它也將當它試圖讀取文件...任何信息會被承認... THX提前對我有一些麻煩,Windows Phone應用程序

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; // ny 
using System.Net; 
using System.Collections.Specialized; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
using Microsoft.Phone.Tasks; 
using Microsoft.Phone.Shell; 
using System.IO; // ny 
using System.IO.IsolatedStorage; // ny 

namespace SmartSence 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     private static UTF8Encoding enc = new UTF8Encoding(); 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
      if (CreateStore().FileExists("Userdata\\Userdata.txt")) 
      { 
       mail.Text = ReadFile(CreateStore()); 
      } 
     } 


     private void minknap_Click(object sender, RoutedEventArgs e) 
     { 


      string newusername; 
      newusername = Username.Text; 
      mytext1.Text = newusername; 

      string igen; 
      igen = "&password="; 

      string newpassword; 
      newpassword = mypassword.Password; 
      mypasswordblock.Text = newpassword; 

      WebBrowserTask webBrowserTask = new WebBrowserTask(); 

      webBrowserTask.Uri = new Uri("http://www.smartsence.dk/winindexcheck.php?id=" + mytext1.Text + igen + mypasswordblock.Text, UriKind.Absolute); 

      webBrowserTask.Show(); 


     } 
     private void signin_Click(object sender, RoutedEventArgs e) 
     { 




      string signin; 
      signin = mail.Text; 
      signintext.Text = signin; 

      string igen; 
      igen = "&password="; 

      string newpassword; 
      newpassword = mail.Text; 
      signintext.Text = newpassword; 

      if (!CreateStore().DirectoryExists("Userdata")) 
      { 

       CreateStore().CreateDirectory("Userdata"); 
       if (!CreateStore().FileExists("Userdata\\data.txt")) 
       { 
        CreateStore().CreateFile("Userdata\\data.txt"); 
       } 
       IsolatedStorageFileStream stream = new IsolatedStorageFileStream("Userdata\\Userdata.txt", FileMode.Create, CreateStore()); 
       WriteToFile(CreateStore(), signin); 

      } 

      WebBrowserTask webBrowserTask = new WebBrowserTask(); 

      webBrowserTask.Uri = new Uri("http://www.smartsence.dk/winindexcheck.php?id=" + signintext.Text + igen + signintext.Text, UriKind.Absolute); 

      webBrowserTask.Show(); 



     } 
     private void minknap2_Click(object sender, RoutedEventArgs e) 
     { 
      NavigationService.Navigate(new Uri("/Info.xaml", UriKind.Relative)); 
     } 

     private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
     { 

     } 

     private void info_Click(object sender, EventArgs e) 
     { 
      MessageBox.Show("SmartSence App 4.0 - Is free to use, it´s has never been easier to navigate around the web. Buy the app and get free VIP status forever - Please support us :) "); 
     } 
     private void VIP_Click(object sender, EventArgs e) 
     { 
      WebBrowserTask webBrowserTask = new WebBrowserTask(); 

      webBrowserTask.Uri = new Uri("http://www.smartsence.dk/winindexappvipcheck2.php", UriKind.Absolute); 

      webBrowserTask.Show(); 
     } 

     private void image1_ImageFailed(object sender, ExceptionRoutedEventArgs e) 
     { 

     } 
     #region NewMethods 
     private IsolatedStorageFile CreateStore() 
     { 
      IsolatedStorageFile lager = IsolatedStorageFile.GetUserStoreForApplication(); 
      return lager; 
     } 

     private void WriteToFile(IsolatedStorageFile storeFile, string content) 
     { 
      IsolatedStorageFileStream fileStream = storeFile.OpenFile("Userdata\\Userdata.txt", FileMode.Open, FileAccess.Write); // Exception: not allowed on IsolatedStorageFile 
      using (StreamWriter writer = new StreamWriter(fileStream)) 
      { 
       writer.WriteLine(content); 
      } 
     } 

     private string ReadFile(IsolatedStorageFile storeFile) 
     { 
      IsolatedStorageFileStream fileStream = storeFile.OpenFile("Userdata\\Userdata.txt", FileMode.Open, FileAccess.Read); 
      using (StreamReader reader = new StreamReader(fileStream)) 
      { 
       return reader.ReadLine().Trim(); 
      } 
     } 
     private void mail_Tap(object sender, GestureEventArgs e) 
     { 
      mail.Text = string.Empty; 
     } 
     #endregion 


    } 
} 
+0

什麼操作,拋出異常? –

+0

該操作將writeToFile(IsolatedStorageFile storeFile,字符串內容)在那裏第一線,我最右邊 –

回答

0

我認爲這個問題是你不關你的IsolatedStorageFileStream,因此該文件仍然是開放的,因此不能被寫入,因此拋出此異常。您應該確保自己將自己的&寫入使用中,或者自己致電Dispose

public MainPage() 
     { 
      InitializeComponent(); 
      if (CreateStore().FileExists("Userdata\\Userdata.txt")) 
      { 
       mail.Text = ReadFile(CreateStore()); //opens file and never closes it. 
      } 
     } 

要解決此問題,您應該使用using語句。

private void WriteToFile(IsolatedStorageFile storeFile, string content) 
     { 
      using (IsolatedStorageFileStream fileStream = storeFile.OpenFile("Userdata\\Userdata.txt", FileMode.Open, FileAccess.Write){ // Exception: not allowed on IsolatedStorageFile 
      using (StreamWriter writer = new StreamWriter(fileStream)) 
      { 
       writer.WriteLine(content); 
      } 
     } 
} 

     private string ReadFile(IsolatedStorageFile storeFile) 
     { 
      using(IsolatedStorageFileStream fileStream = storeFile.OpenFile("Userdata\\Userdata.txt", FileMode.Open, FileAccess.Read){ 
      using (StreamReader reader = new StreamReader(fileStream)) 
      { 
       return reader.ReadLine().Trim(); 
      } 
     } 
} 
+0

評論我只是想你告訴我:使用_using statements_(沒有工作),並通過_ReadFile的方式嘗試()_如果已經有一個名爲「Userdata.txt」目錄中的「使用用戶數據」 ......我想找到我所有的流和使用後關閉流... –

+0

可你只需要使用文件中的構造方法應該只運行http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragesettings(v=VS.95).aspx,或者是有您使用的*重量級*版本的理由嗎?它看起來像你只是存儲字符串。 –