سؤال

أنا أستخدم Sharpsvn مكتبة في التطبيق. كجزء من اختبارات التكامل الآلية الخاصة بي ، أقوم بإنشاء مستودع اختبار ، والتحقق من نسخة عمل ، وأجر بعض الاختبارات ، ثم حذف كل من مجلدات النسخ المستودع ونسخ العمل.

ومع ذلك ، بسيطة Directory.Delete(workingCopyPath, true); يعطي دائما UnauthorizedAccessException مع الرسالة "يتم رفض الوصول إلى "إدخالات" المسار.". يمكنني إعادة إنتاج الخطأ مع هذا الرمز:

     using (var svnClient = new SvnClient())
     {
        svnClient.CheckOut(
           new SvnUriTarget(new Uri(repositoryPath)), workingCopyPath);
     }
     Directory.Delete(workingCopyPath, true);

لا يزال هذا الخطأ يحدث إذا كنت

  • حاول حذف نسخة عمل تم إنشاؤها بواسطة أ السابق تشغيل اختبارات التكامل
  • Thread.Sleep بضع ثوان قبل محاولة الحذف

إذا استخدمت Explorer لحذف نسخة العمل المؤقتة يدويًا ، فلن أحصل على أي خطأ.

ما يحدث الخطأ هنا؟ ما هي الطريقة الصحيحة لحذف نسخة عمل تخريب برمجي؟

هل كانت مفيدة؟

المحلول

يتحول Directory.Delete يرفض حذف ملفات القراءة فقط.

أستخدم هذه الطريقة الآن لحذف الدلائل:

private void DeleteDirectory(string path)
{
   var directory = new DirectoryInfo(path);
   DisableReadOnly(directory);
   directory.Delete(true);
}

private void DisableReadOnly(DirectoryInfo directory)
{
   foreach (var file in directory.GetFiles())
   {
      if (file.IsReadOnly)
         file.IsReadOnly = false;
   }
   foreach (var subdirectory in directory.GetDirectories())
   {
      DisableReadOnly(subdirectory);
   }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top