[英]Error reading Stream in C#
在我的應用程序中,我試圖從文件系統讀取流。 這是我的方法:
public Stream ReadStream (string path)
{
return new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
}
該方法有時可以工作,但大多數情況下會遇到以下異常:
Sharing violation on path /data/data/MyPath/11.png
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0
你能幫助我嗎? 非常感謝!
FileShare.None
表示如果系統上的其他任何文件在同一文件上打開了文件句柄,則將無法打開流。 這也意味着,只要打開句柄,其他進程將無法打開文件。
解決方案取決於您的需求,但是您可以例如將其更改為FileShare.Read
以禁止其他進程與您同時寫入文件,但仍允許他們打開文件進行讀取。
而且,查看您函數的名稱,我想您可以將其替換為File.OpenRead
。
編輯:剛注意到您的問題是關於單聲道,我不確定我在這里說的是否成立。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.