Bob Ma
Bob Ma

Reputation: 197

VB.NET Access to the path is denied

NET to create folder

I would like to create folder to save data and I would like to copy these data to other folder. However, whenever, I try to create folder and generate files I am getting Access to the path "path" is denied error

I tried to disable read-only option on the folder but it didn't work

I tried this way but it didn't work.

Save folder it under the folder in the Program Files and I am copying data to "C:RESULT"

however, it doesn't work .. i am not sure why....

can you help me how to create folder and copy data to new folder?

Private Sub createTimedFolder()
    Dim folder As String = Now.ToString("MM_dd_yyyy_hh_mm_ss")
    G_Folder = folder
    '  MsgBox(folder)
    If (Not System.IO.Directory.Exists(folder)) Then
        System.IO.Directory.CreateDirectory(folder)

    Else

    End If
    ' MsgBox(folder & " created ")
    Try
        'Set the current directory.
        Directory.SetCurrentDirectory(Path.Combine(defaultDir, folder))
    Catch e As DirectoryNotFoundException
        Console.WriteLine("The specified directory does not exist. {0}", e)
    End Try


    Dim LogBook = folder & "log.txt"
    logwriter = New System.IO.StreamWriter(LogBook)
End Sub

Upvotes: 2

Views: 29149

Answers (4)

giovannigiorgio
giovannigiorgio

Reputation: 31

You have to use this routine: ' Recursive function which keeps moving down the directory tree until a given depth.

Public Sub GetFiles(ByVal strFileFilter As String, ByVal strDirectory _
          As String, ByVal intDepthLimit As Integer, ByVal intCurrentDepth As Integer)

            Try
                Dim folderInfo As New DirectoryInfo(strDirectory)

                ' Is the current depth on this recursion less than our limit?
                ' If so, find any directories and get into them by calling GetFiles recursively (incrementing depth count)
                If intCurrentDepth < intDepthLimit Then
                    Dim directories() As DirectoryInfo
                    directories = folderInfo.GetDirectories()

                    For Each fDirectory In directories
                        ' Recursively call ourselves incrementing the depth using the given folder path.
                        GetFiles(strFileFilter, fDirectory.FullName, intDepthLimit, intCurrentDepth + 1)
                    Next
                End If

               
                Dim files() As FileInfo
                files = folderInfo.GetFiles(strFileFilter)

                For Each fFile In files
                    listView1.Items.Add(fFile.FullName)
                Next
            Catch ex As Exception
              
            End Try
        End Sub

Then you have to launch it this way: Example: GetFiles("*.txt", "C:\Docu", 1, 0)

Upvotes: 0

Dinesh Mandaviya
Dinesh Mandaviya

Reputation: 192

You have to give permission (Read,write for User) for particular directory. for example If You are creating directory in your application than you have to set permission on your application (Read,write)

Upvotes: 2

user3183324
user3183324

Reputation: 22

Here try this don't forget the Imports.

Imports Microsoft.Office.Interop
Imports System.IO
    Private Sub CreateTimedFloder()
            'You may change C:\ to the location of the folder that 
            'you want to create.
            Dim Directory As String = "C:\" & DateTime.Now.ToString("MM_dd_yyyy") & "_" & DateTime.Now.ToString("hh_mm_ss")
            Dim CompletePath As String = Directory & "\"

            If Dir(Directory, vbDirectory) = "" Then
                MkDir(Directory)
            End If

            Dim LogBook = File.Create(CompletePath & "Log.txt")
            Dim logwriter As New System.IO.StreamWriter(LogBook)

            logwriter.Write("hello")
            logwriter.Close()

        End Sub

Upvotes: 1

Gridly
Gridly

Reputation: 938

The problem is that in the first few lines of code you create the temp directory. This will be created relative to the execution path of the program. In my test it created it in the /bin/Debug/ folder.

Then you try change to the default direct and the temp folder name. The folder was not created under this default directory so that is where the error comes from.

You need to combine the defaultDir with the temp dir before creating the directory

    Private Sub createTimedFolder()
    Dim folder As String = Now.ToString("MM_dd_yyyy_hh_mm_ss")
    Dim CompletePath As String = Path.Combine(defaultDir, folder)

    Dim G_Folder As String = CompletePath
    '  MsgBox(folder)
    If (Not System.IO.Directory.Exists(CompletePath)) Then
        System.IO.Directory.CreateDirectory(CompletePath)

    Else

    End If
    ' MsgBox(folder & " created ")

    Try
        'Set the current directory.
        Directory.SetCurrentDirectory(CompletePath)
    Catch e As DirectoryNotFoundException
        Console.WriteLine("The specified directory does not exist. {0}", e)
    End Try


    Dim LogBook = folder & "log.txt"
    Dim logwriter As New System.IO.StreamWriter(LogBook)

    logwriter.Write("hello")
    logwriter.Close()

Upvotes: 0

Related Questions