Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 595327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:02:59+00:00 2026-05-13T16:02:59+00:00

As an example, assume the following snippet of VB.NET code to delete a directory.

  • 0

As an example, assume the following snippet of VB.NET code to delete a directory.

 Try
      Dim SomeFolder="c:\somefolder"
      System.IO.Directory.Delete(SomeFolder, True)    
 Catch ioex As System.IO.IOException     
     'What went wrong? 
     'File locked by another process? 
     'File not found? 
     'something else?
 End Try       

In the exception handler, if the directory or a file inside it is open I’d like to give the user the opportunity to close the file and retry the operation, but only if the IOException was caused by a locking problem.

The problem is that the IOException can be thrown for any number of reasons, for example an invalid path or read-only flag set on the file. Each of these conditions sets a different value in the .message attribute of the exception object, but it just feels so wrong to hard-code a check for a specific string in the error message to detect the specific cause of the failure. I don’t have a lot of confidence that the error strings will be consistently worded with future versions of .net and would hate to have to fuss about with writing localization code to deal with the possibility that the message is returned in something other than English.

There has got to be a better way to deal with what has to be an extremely common exception handling concern. Am I missing something?

Update/Clarification: Thanks for the answers so far, but I may have made my example a little too generic. For right now at least, I am specifically looking for a way to detect the condition of the file being locked by another process within the exception handler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:02:59+00:00Added an answer on May 13, 2026 at 4:02 pm

    You can catch some of the exceptions that inherit from IOException.

    These include DirectoryNotFoundException, FileNotFoundException and others.

    Use Exception Handling for this:

    Try
      Dim SomeFolder="c:\somefolder"
      System.IO.Directory.Delete(SomeFolder, True)    
    Catch fnfex As System.IO.FileNotFoundException     
     'What went wrong? 
     'File not found? 
    Catch ioex As System.IO.IOException     
     'What went wrong? 
     'something else?
    End Try       
    

    Update:

    With file operations, it would be better to test for the existence of the file/folder before operating on it. This is best practice, as you avoid unnecessary exceptions:

    Dim SomeFolder="c:\somefolder"
    
    If Directory.Exists(SomeFolder) Then
       System.IO.Directory.Delete(SomeFolder, True)
    End If
    

    Update 2:

    Following the comments and the update to the question regarding locked files. I have had the same problem and did resort to parsing out the exception message, as there is no FileLockedException or similar 🙁

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume a .NET class library code that, for example, writes to the Windows registry
In the following example try { lineA lineB lineC lineD } catch { lineE
Check out the following snippet of HTML/Javascript code: <html> <head> <script type=text/javascript> var alerts
Assume following code, NSString *str=[[NSString alloc] initWithString:@sagar]; [str autorelease]; I have seen many times
Assume the following toy class, and modern compilers (recent gcc for example). template <typename
I've got a following code snippet: def isolation_level(level): def decorator(fn): def recur(level, *args, **kwargs):
Assume I have the following example: Example One $('.my_Selector_Selected_More_Than_One_Element').each(function() { $(this).stuff(); $(this).moreStuff(); $(this).otherStuff(); $(this).herStuff();
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
let's assume the following easy example: f = figure; plot(-10:10, (-10:10).^3, '*-r'); x =
i would like to explain my problem by the following example. assume the word:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.