2

さて、私が読んだ投稿が簡単な修正であると言っていることを解決しようとして、途方もない時間を費やしました。

ドキュメント内のファイルに書き込みたいのですが、これが私のコードです。

        string st = @"C:\Users\<NAME>\Documents\Sample1.txt";

        System.IO.StreamWriter file = new System.IO.StreamWriter(st);
        file.WriteLine(Convert.ToString(Sample1[0]));
        file.Close();

ユーザー名はどこにありますか。次のエラーが表示されます

「タイプ 'System.IO.DirectoryNotFoundException' の初回例外が mscorlib.ni.dll で発生しました。タイプ 'System.IO.DirectoryNotFoundException' の例外が mscorlib.ni.dll で発生しましたが、ユーザー コードで処理されませんでした」

Windows Phone 開発に Visual Studio Express を使用しています。

誰かが私が間違っていることを指摘できれば、私は感謝します。

ありがとう。

4

3 に答える 3

2

投稿した文字列を使用していると思います。その場合は、代わりにSpecialFolder列挙型を使用する必要があります。

var st = string.format(@"{0}\Sample1.txt", 
             Environment.GetFolderPath(Environment.SpecialFolder.Personal));
于 2012-12-30T23:29:35.573 に答える
1

Environment.SpecialFolder列挙を利用しPath.Combine(...)て、パスを作成するために使用する必要があります。

var path = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.Personal),
    "Sample1.txt");

using (var sw = new StreamWriter(path))
{
    sw.WriteLine(Convert.ToString(Sample1[0]));
}

また、使い捨てなのでStreamWriter、ステートメント内に配置する必要があります。using

于 2012-12-30T23:33:15.927 に答える
0

MyDocuments を使用するには:

    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

これにより、ホスト コンピューター上の MyDocuments へのパスが返されます。

MSDN で SpecialFolders のリストを確認できます: http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

編集: Windows Phone 向けに開発していることに気付きました。MSDN の他の SpecialFolders を読んでください。

于 2012-12-30T23:34:55.010 に答える