考虑以下计划:
var path = Path.Combine( Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName())); Directory.CreateDirectory(path); var testFile = Path.Combine(path, "test.txt"); File.WriteAllText(testFile, "Test file"); var source = Repository.Init(path); using (var repository = new Repository(source)) { repository.Index.Add("test.txt"); } Directory.Delete(path, true);删除存储库文件夹后,我收到一个UnauthorizedAccessException - 拒绝访问其中一个内部git文件。 为了删除文件夹,还有什么我应该处理的吗?
Consider following program:
var path = Path.Combine( Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName())); Directory.CreateDirectory(path); var testFile = Path.Combine(path, "test.txt"); File.WriteAllText(testFile, "Test file"); var source = Repository.Init(path); using (var repository = new Repository(source)) { repository.Index.Add("test.txt"); } Directory.Delete(path, true);On deletion of repository folder I get an UnauthorizedAccessException - access to one of internal git files is denied. Is there anything else I should dispose of in order to delete the folder?
最满意答案
为了删除文件夹,还有什么我应该处理的吗?
你的处理模式很好。 上述问题的起源不同。
如文档中所述,当存在与权限相关的问题时,将引发UnauthorizedAccessException 。
实际上,Libgit2Sharp的行为类似于git,并将.git/objects层次结构下的文件标记为只读,因此在尝试删除它们时会抛出异常。
为了解决这个问题,并在运行测试时简化清理阶段,我们开发了一个辅助方法(即DirectoryHelper.DeleteDirectory() ),它以递归方式取消设置这些只读属性并删除文件和目录。
请参阅源代码 ,您是否愿意重复使用它。
Is there anything else I should dispose of in order to delete the folder?
Your disposing pattern is just fine. The mentioned issue has a different origin.
As stated in the documentation, UnauthorizedAccessException is raised when there's a permission related issue.
Indeed, Libgit2Sharp behaves similarly to git regarding this and marks files under the .git/objects hierarchy as read-only, thus the thrown exception when attempting to delete them.
In order to work around this, and ease the clean up phase when our tests are run, we've developed a helper method (ie. DirectoryHelper.DeleteDirectory()), which recursively unsets those read-only attributes and delete files and directories.
See the source code would you be willing to reuse it.
更多推荐
发布评论