问题描述
我对文件锁定有问题。我不知道为什么,但是我可以在应用程序中删除使用过的和打开的文档,这是代码。有人可以帮我吗?
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控件是可以在应用程序中使用的控件, 因此文件锁定应在应用程序级别而不是在应用程序级别实现 控制一个。
您可以手动锁定此文件:
来自How to manually Lock a file for other applications:
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();
}
}
}