2012-02-29 17 views
0

Windows Phone 7で問題が発生しましたが、その回答が見つかりませんでした。 私はテキストボックスからプロジェクトファイルにある単純なtxtファイルに何かを書き込もうとしています。 StreamWriterを使用しようとしましたが、StreamReaderだけが動作し、IsolatedStorageを使用するようになりましたが、再び動作しませんでした。 それでは、私にこの問題の解決策を教えてください。ファイルに書き込むにはどうすればよいですか?

編集:私は使用 コード:

var myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
// Specify the file path and options. 
using (var isoFileStream = new IsolatedStorageFileStream("/Discount%20it;component/Tax.txt", FileMode.OpenOrCreate, myStore)) 
{ 
    //Write the data 
    using (var isoFileWriter = new StreamWriter(isoFileStream)) 
    { 
        isoFileWriter.WriteLine(textBox1.Text); 
    } 
} 

string fileName = "/Discount%20it;component/Tax.txt"; 

using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    // we need to check to see if the file exists 
    if (!isoStorage.FileExists(fileName)) 
    { 
        // file doesn't exist...time to create it. 
        isoStorage.CreateFile(fileName); 
    } 

    // since we are appending to the file, we must use FileMode.Append 
    using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage)) 
    { 
        // opens the file and writes to it. 
        using (var fileStream = new StreamWriter(isoStream)) 
        { 
            fileStream.Write(textBox1.Text); 
        } 
    } 
} 
+0

使用したコードを表示して、「うまくいかない」と説明してください - クラッシュしたのか、エラーを出したのか、何もしなかったのですか? – Jason

+0

@ Jason時々、私はプリミッション、IsolatedStorage例外、StreamWriter例外を持っていないと言いました。 – Yehonatan

+0

コードを正しく書式設定してください。答えを得るのに役立ちます。 – Will

答えて

0

それを説明し、MSDN here上のセクションがあります。これは機能しませんか?

そのページに示す例コード:

private void btnWrite_Click(object sender, RoutedEventArgs e) 
{ 
    // Obtain the virtual store for the application. 
    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 

    // Create a new folder and call it "MyFolder". 
    myStore.CreateDirectory("MyFolder"); 

    // Specify the file path and options. 
    using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.OpenOrCreate, myStore)) 
    { 
     //Write the data 
     using (var isoFileWriter = new StreamWriter(isoFileStream)) 
     { 
      isoFileWriter.WriteLine(txtWrite.Text); 
     } 
    } 
} 

今、あなたはあなたがそれにファイルを書き込もうとする前に、プログラムで分離ストレージにディレクトリを作成する必要があることに注意します。

開発のために、ディレクトリーとファイルは、開発マシンのディレクトリに、this pageのように作成されます。 Windows 7マシンで開発している場合は、隔離されたディレクトリとファイルがVistaと同じ場所にあることが予想されます。非ローミングユーザープロファイルUsers<username>AppData\Local

上記のコードでは、ファイルは<username>\AppData\Local\MyFolderである必要があります。

コードでこの行:

IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 

地図お使いのdevのマシン上<username>\AppData\LocalにIsolatedStorage、それmyStoreを呼び出します。

この行は、あなたが、その後に書き込むことができることがディレクトリを作成します。アプリケーションは、このファイルから読み込むようにするに

myStore.CreateDirectory("MyFolder"); 

ここではサンプルコードがあります:あなたが取得する必要がありますどのように

// This code opens and reads the contents of myFile.txt. 
private void btnRead_Click(object sender, RoutedEventArgs e) 
{ 
    // Obtain a virtual store for the application. 
    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 

    try 
    { 
     // Specify the file path and options. 
     using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.Open, myStore)) 
     { 
      // Read the data. 
      using (var isoFileReader = new StreamReader(isoFileStream)) 
      { 
       txtRead.Text = isoFileReader.ReadLine(); 
      } 
     } 

    } 
    catch 
    { 
     // Handle the case when the user attempts to click the Read button first. 
     txtRead.Text = "Need to create directory and the file first."; 
    } 
} 

お知らせIsolated Storageファイルの実際の場所と、ファイルを開くときにファイルパスにディレクトリ名を含めます。

(2012年1月3日)の追加

あなたもFileAccess.Writeを使用していることを確認しFileMode.Appendを使用している場合。 ファイルが存在するかどうかを確認する代わりに、FileMode.OpenOrCreateを使用してください。その分離ストレージを確保シルバー使用する場合 が有効になっている:いない(容易に)修飾されXAPの一部として配布され

Start -> All Programs -> Microsoft Silverlight -> Microsoft Silverlight -> Application Storage -> make sure the 'enable application storage' checkbox is checked. 
+0

私のプロジェクトに合うようにコードを修正しました: using(var isoFileStream = new IsolatedStorageFileStream( "\\ Tax.txt"、FileMode.Create、myStore)) isoFileWriter.Write(textBox1.Text); まだ何も起こっていませんが、Tax.txtファイルはプロジェクトファイル(mainpage.xamlはどこですか)にあります – Yehonatan

+0

私が理解しているように、ファイルのディレクトリを最初に作成する必要があります。 。 – ChrisBD

+0

私のプロジェクトで "Files"という名前のディレクトリを作成しましたが、新しいディレクトリを作成せずにあなたが私に与えたことをしようとしているときに、このエラーが発生します - OperationはIsolatedStorageFileStreamでは許可されていません。 – Yehonatan

0

ファイル。

通常、更新してファイルに保存するデータがある場合は、IsolatedStorageで行う必要があります。

あなたはXAPの一部として、ファイルのデフォルトのデータを出荷、次の操作を実行したいと思うと仮定すると:

データを読みたいときは: - IsolatedStorage内のファイルが存在するかどうかを確認します。
- そこから読んでください。
- それは、その後XAP

に付属のデータを書き込むしたいファイルから読み取らない場合: - IsolatedStorageにそれを書いて、既存のファイル/データを上書き

+0

しかし、どのように私はそれにアクセスできますか?私のアプリケーションに自動的に接続されていますか?また、これも常に私にこのことを伝えています - IsolatedStorageFileStreamでの操作は許可されていません - var isoFileStream = new IsolatedStorageFileStream( "Files \\ Tax.txt"、FileMode.Create、myStore – Yehonatan

関連する問題