locked
Directory.Exists query RRS feed

  • Question

  • I am trying to use the following code 

            Dim target As String = LTrim("g:\data\models") + Mid(  what "e:\data\models\Documents\Drawings\Samples\2D Samples", ("e:\data\models").length + 1))
            If Directory.Exists(target) = False Then My.Computer.FileSystem.CreateDirectory(target)
     

    Even though the subdirectory on the G drive does not exist, the directory exists returns a True,  So I tried just the code after the THEN but the createdirectory did nothing.

    What am I doing wrong?

    regards

    Colin Reid

     

    • Moved by Amanda Zhu Monday, September 30, 2013 8:53 AM
    Friday, September 27, 2013 2:59 PM

Answers

  • I tried your code and it worked on my e drive.

    Imports System.IO
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim target As String = LTrim("e:\data\models") + Mid("e:\data\models\Documents\Drawings\Samples\2D Samples", ("e:\data\models").Length + 1)
            If Directory.Exists(target) = False Then My.Computer.FileSystem.CreateDirectory(target)
            Label1.Text = target
        End Sub
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    • Edited by Mr. Monkeyboy Monday, September 30, 2013 7:51 PM 5555
    • Marked as answer by Carl Cai Friday, October 4, 2013 1:18 AM
    Monday, September 30, 2013 7:49 PM
  • Directory.Exists and IO.Directory.Exists are the same, you need to learn more about what namespaces are i.e. Directory.Exists resides in System.IO and depending on how your Imported namespaces are under project properties on the reference tab dictates if you need to preside Directory.Exists with IO or not.

    In regards to pastin, if my path was C:\NoSuchFolder I would paste it in the address text box as shown below and note the result

    Now a path that exists on my machine C:\Data

    So yes it can be done


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Marked as answer by Carl Cai Friday, October 4, 2013 1:18 AM
    Saturday, September 28, 2013 1:49 PM

All replies

  • Hello,

    Either write the results of the variable target to a TextBox or Console.WriteLine to the IDE output window. Now open Windows Explorer and paste in the results, what happens ? If the folder does not exist you should get the same results as IO.Directory.Exists and if not something strange is going on.

    If I run this the folder indeed is not located

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Dim target As String = LTrim("g:\data\models") +
            Mid("e:\data\models\Documents\Drawings\Samples\2D Samples", ("e:\data\models").Length + 1)
        If IO.Directory.Exists(target) Then
            MessageBox.Show("Found it")
        Else
            MessageBox.Show("Not found")
        End If
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, September 27, 2013 5:01 PM
  • I am sorry but I don't quite understand.  I followed the section about the Console.writeline but Windows Explorer does not permit pasting a directory so I am not sure what results to expect.

    I switched from Directory.exists to IO.Directory exists but it persists in saying that it is true when the new directory clearly does not exist and if I do a CreateDirectory it is ignored.

    Is there any other cause, say. in Windows Explorer setup.

    Saturday, September 28, 2013 1:41 PM
  • Directory.Exists and IO.Directory.Exists are the same, you need to learn more about what namespaces are i.e. Directory.Exists resides in System.IO and depending on how your Imported namespaces are under project properties on the reference tab dictates if you need to preside Directory.Exists with IO or not.

    In regards to pastin, if my path was C:\NoSuchFolder I would paste it in the address text box as shown below and note the result

    Now a path that exists on my machine C:\Data

    So yes it can be done


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Marked as answer by Carl Cai Friday, October 4, 2013 1:18 AM
    Saturday, September 28, 2013 1:49 PM
  • Hello,

    I have moved it to Visual Basic forum for better response.

    Thanks,


    Amanda Zhu <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 30, 2013 8:53 AM
  • The code you pasted appears to be invalid

    "Mid(  what" is not recognized, I assume it is just a typo

    Also is there a reason why you are constructing this path in such a complicated manner ?

    Why not just assign "Target" to - 

    g:\data\models\Documents\Drawings\Samples\2D Samples

    in any case, I cannot reproduce your problem, it returns false.

    Monday, September 30, 2013 7:26 PM
  • I tried your code and it worked on my e drive.

    Imports System.IO
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim target As String = LTrim("e:\data\models") + Mid("e:\data\models\Documents\Drawings\Samples\2D Samples", ("e:\data\models").Length + 1)
            If Directory.Exists(target) = False Then My.Computer.FileSystem.CreateDirectory(target)
            Label1.Text = target
        End Sub
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    • Edited by Mr. Monkeyboy Monday, September 30, 2013 7:51 PM 5555
    • Marked as answer by Carl Cai Friday, October 4, 2013 1:18 AM
    Monday, September 30, 2013 7:49 PM