the*_*nut 3 c# directory locked-files
在目录中创建文件后,只要我创建文件的程序正在运行,目录就会被锁定.有没有办法释放锁?我需要稍后重命名该行目录,我总是得到IOException一句"访问路径"......"拒绝".
Directory.CreateDirectory(dstPath);
File.Copy(srcPath + "\\File1.txt", dstPath + "\\File1.txt"); // no lock yet
File.Create(dstPath + "\\" + "File2.txt"); // causes lock
Run Code Online (Sandbox Code Playgroud)
File.Create(string path) 创建一个文件并使该流保持打开状态.
你需要做以下事情:
Directory.CreateDirectory(dstPath);
File.Copy(srcPath + "\\File1.txt", dstPath + "\\File1.txt");
using (var stream = File.Create(dstPath + "\\" + "File2.txt"))
{
//you can write to the file here
}
Run Code Online (Sandbox Code Playgroud)
using语句确保您关闭流,并释放对文件的锁定.
希望这可以帮助