[英]C# Filestream file locking
我有文件鎖定問題。 我不知道為什么,但我可以用我的應用程序刪除使用和打開的文檔,這是代碼。 有人可以幫我嗎?
public Form1()
{
InitializeComponent();
Document location path
var args = System.Environment.GetCommandLineArgs();
argPath
var argPath = args.Skip(1).FirstOrDefault();
if (!string.IsNullOrEmpty(argPath))
{
var fullPath = Path.GetFullPath(argPath);
if (!string.IsNullOrEmpty(fullPath))
{
FileStream Fs2 = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
}
}
}
假設您使用的是Devexpress.RichEditConrol ,因此這不會鎖定文件。
RichEdit 控件是一個可以在應用程序中使用的控件,因此文件鎖定應該在應用程序級別實現,而不是在控件級別。
看看這里的討論: https : //supportcenter.devexpress.com/ticket/details/t418100/opening-same-file-by-multiple-instances-of-richeditcontrol
您可以手動鎖定此文件:
public class FileLock : IDisposable
{
private FileStream _lock;
public FileLock(string path)
{
if (File.Exists(path))
{
_lock = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
IsLocked = true;
}
}
public bool IsLocked { get; set; }
public void Dispose()
{
if (_lock != null)
{
_lock.Dispose();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.