简体   繁体   English

使用LibGit2Sharp以编程方式删除本地存储库

[英]Programmatically delete local repository with LibGit2Sharp

I would like to delete the local repo folder that I cloned from remote repository using LibGit2Sharp. 我想删除使用LibGit2Sharp从远程存储库克隆的本地repo文件夹。 I read here here that I have to Dispose() the Repository before I can delete it, but it still not works fine. 这里读到,我必须在删除它之前Dispose()存储库,但它仍然无法正常工作。

using (var repo = new LibGit2Sharp.Repository(path))
{
    repo.Dispose();
}

Directory.DeleteFolder(path);

And I still have an exception: 我还有一个例外:

Access to the path 'c16566a7-202a-4c8a-84de-3e3caadd5af9' is denied.

The content of the 'path' variable is the following: 'path'变量的内容如下:

 C:\Users\USERNAME\AppData\Local\dftmp\Resources\c16566a7-202a-4c8a-84de-3e3caadd5af9\directory\UserRepos\github.com\domonkosgabor\testrepo

This folder was created by a worker role to a local storage. 此文件夹由辅助角色创建到本地存储。

What should I do to delete the whole folder (including .git)? 我该怎么做才能删除整个文件夹(包括.git)?

For the benefit of anyone else having this problem: 为了其他任何有此问题的人的利益:

I had the same problem, but I was still getting an UnauthorizedAccessException even though I was running as administrator, and I was disposing the repository object correctly. 我有同样的问题,但即使我以管理员身份运行,我仍然得到一个UnauthorizedAccessException ,我正确地处理了存储库对象。 It turns out that some of the files in the .git folder are marked as ReadOnly , so I had to loop through each file and remove the ReadOnly attribute before deleting. 事实证明.git文件夹中的某些文件被标记为ReadOnly ,因此我必须遍历每个文件并在删除之前删除ReadOnly属性。 I wrote a custom method to do this: 我写了一个自定义方法来做到这一点:

/// <summary>
/// Recursively deletes a directory as well as any subdirectories and files. If the files are read-only, they are flagged as normal and then deleted.
/// </summary>
/// <param name="directory">The name of the directory to remove.</param>
public static void DeleteReadOnlyDirectory(string directory)
{
    foreach (var subdirectory in Directory.EnumerateDirectories(directory)) 
    {
        DeleteReadOnlyDirectory(subdirectory);
    }
    foreach (var fileName in Directory.EnumerateFiles(directory))
    {
        var fileInfo = new FileInfo(fileName);
        fileInfo.Attributes = FileAttributes.Normal;
        fileInfo.Delete();
    }
    Directory.Delete(directory);
}

I would like to delete the local repo folder that I cloned from remote repository using LibGit2Sharp. 我想删除使用LibGit2Sharp从远程存储库克隆的本地repo文件夹。 I read here here that I have to Dispose() the Repository before I can delete it. 我在这里读到,在删除它之前我必须Dispose()存储库。

LibGit2Sharp keeps hold on some files in the .git folder (mainly the packfiles for performance reasons). LibGit2Sharp保持.git文件夹中的某些文件(主要是出于性能原因的包文件)。 Calling Dispose() will release those handles and deallocate the non managed memory. 调用Dispose()将释放这些句柄并释放非托管内存。

As such, it's indeed a strong recommendation to rely on the using statement (or, at the very least to Dispose() the Repository instance when you're done with it). 因此,确实强烈建议依赖于using语句(或者至少在完成它时Dispose() Repository实例)。

If you don't do this, those handles will eventually be released through finalizers when your AppDomain has unloaded, but you will have no real control regarding "when" that's going to happen. 如果你不这样做,那么当你的AppDomain卸载时,这些句柄最终将通过终结器释放,但你将无法真正控制“何时”将要发生的事情。

Edit: Reading your code once again, I overlooked something. 编辑:再次阅读您的代码,我忽略了一些事情。 The recommended pattern is either 推荐的模式是

using (var repo = new LibGit2Sharp.Repository(path))
{
    // Do amazing stuff
}

or 要么

var repo = new LibGit2Sharp.Repository(path);
// Do amazing stuff
repo.Dispose();

Indeed, the using statement will automatically issue a call to Dispose() once the code reach the end of the scope. 实际上,一旦代码到达范围的末尾, using语句将自动发出对Dispose()的调用。

Access to the path 'c16566a7-202a-4c8a-84de-3e3caadd5af9' is denied. 访问路径'c16566a7-202a-4c8a-84de-3e3caadd5af9'被拒绝。

Regarding this point, I think this has nothing to do with LibGit2Sharp. 关于这一点,我认为这与LibGit2Sharp无关。

Is the process (trying to delete the folder named after a guid) running under an identity granted with enough rights to do so? 是否在具有足够权限的身份下运行的进程(尝试删除以guid命名的文件夹)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM