File.WriteAllTextを使用して非常に長い文字列をファイルに書き込んでおり、別のスレッドまたはプロセスが同じファイルを読み込もうとしているとします。何か例外がスローされますか?つまり、File.WriteAllTextメソッドが使用するFileShareパラメータは何ですか?それはドキュメントに書かれていません!File.WriteAllTextとConcurrent Access
答えて
これは.NET Framework 4.0のソースコードです。 FileShare.Readを内部的に使用するStreamWriterが使用されています。
[SecuritySafeCritical]
public static void WriteAllText(string path, string contents)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (path.Length == 0)
{
throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
}
InternalWriteAllText(path, contents, StreamWriter.UTF8NoBOM);
}
private static void InternalWriteAllText(string path, string contents, Encoding encoding)
{
using (StreamWriter writer = new StreamWriter(path, false, encoding))
{
writer.Write(contents);
}
}
これは、StreamWriterの基本ストリームを作成するコードです。
private static Stream CreateFile(string path, bool append)
{
return new FileStream(path, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read, 0x1000, FileOptions.SequentialScan);
}
MSDNは、どの共有モードが使用されているかについては説明していません。
実行時に(例えば、Process Monitorを使用して、WIn32 APIフラグをFileShare
の値に変換して)ソースコードを見るか、実行時にそのオプションを調べることができます。
しかし、と記載されていないため、これは変更される可能性があります。それは自分がFileShare
パラメータを取りFileStream
オーバーロードのいずれかを使用してファイルを開く重要な場合
また、このオーバーStreamWriter
を開き、テキストを書きます。
例外はありますか?
はい。ファイルがすでに互換性のない共有モードで開いている場合は、ファイルのオープンは失敗します。
逆アセンブラを見ると、' File.WriteAllText'は 'FileShare.Read'を渡す' FileStreamn'を使います。 – Richard
+1 "しかし、それは文書化されていないので、パッチや新しいバージョンがそれを変更する可能性があります。" – CodesInChaos
逆アセンブルしたフレームワークのバージョンは? – Mehran
例外はありますか?
はい。あるプロセスがファイルに書き込んでいる間に、他のプロセスが読み取っていないことを確認する必要があります。lock
。たとえFileShare
パラメータをRead
に設定しても、その後にファイルを開くことができ、すぐに例外をスローすることはできませんが、読者はおそらく破損した結果になる可能性があります。
プロセス間ロックにはどのような種類のロックを使用しますか?通常の 'lock {/ * code * /}'ブロックは不十分であると思います。 –
- 1. C#とFile.WriteAllTextエラー
- 2. Concurrent LinkedListとConcurrentLinkedQueue
- 3. ユニコードの問題File.WriteAllText
- 4. C++ Concurrent GET requests
- 5. Java Concurrent API
- 6. Spring JDBCTemplate - Concurrent Calls
- 7. ファインアップローダConcurrent Chunking S3
- 8. Neo4j/Cypher concurrent MERGE
- 9. Silverlight 5のConcurrent ObservableCollection
- 10. java.lang.NoSuchMethodError:com.google.common.util.concurrent.MoreExecutors.platformThreadFactory()Ljava/util/concurrent/ThreadFactory;
- 11. エラー:java.lang.NoSuchMethodError:com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;
- 12. Hibernate + concurrent inserts(MySQL INSERT IGNORE)
- 13. C++ 11 std :: vector in concurrent environment
- 14. Java Concurrent API Work-Stealing Algorithm
- 15. 同じドメインへのConcurrent Net :: HTTP.get
- 16. java.lang.NoClassDefFoundError:bin/gremlin.shを実行しているときにjava/util/concurrent/CompletableFuture
- 17. File.WriteAllTextを使ってローカルドライブにファイルを保存するには?
- 18. C#のFile.WriteAllText()ステートメントでファイルが作成されない
- 19. ArcGISとACCESSテーブル
- 20. NhibernateとMS Access
- 21. Microsoft AccessとSharePoint
- 22. CompletionハンドラがDispatch GroupとConcurrent Queueで期待どおりに動作しない
- 23. MS Accessカウントダウンタイマーアップデートとフォルダトリガー
- 24. クラスが見つかりません。com/google/common/util/concurrent/FutureFallback
- 25. Python Concurrent ProcessPoolExecutorがRaspberry Pi(アイドルPython 3.4.2)で動作しません
- 26. EclipseでCVS(Concurrent Version Control)を作成中にエラー
- 27. Concurrent Rubyで一連のタスクを連鎖する
- 28. ArrayListのスローエラー:異なるクラスのコードからの "Concurrent Modification Exception"
- 29. BlockedIndefinitelyOnMVarをConcurrentコードで理解しています
- 30. デフォルトでは、DispatchQueue .serialまたは.concurrentですか?
[MSDN(http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx) –
@Jalal MSDNファイル 'によって使用される共有モード文書化しません.WriteAllText'。 – CodesInChaos
申し訳ありませんが、私はすぐに読んでいました。私は彼が 'FileShare'について質問していると思った。 –