VB.NET Access to the path is denied

23,769

Solution 1

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

Solution 2

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)

Share:
23,769

Related videos on Youtube

Bob Ma
Author by

Bob Ma

Updated on July 31, 2020

Comments

  • Bob Ma
    Bob Ma almost 4 years

    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
    
  • Bob Ma
    Bob Ma almost 10 years
    It still doesn't work :(. If I ran software with VB 2012, then it is working but if i made installpackage and run sw, then It gives access error :(
  • Gridly
    Gridly almost 10 years
    What is the exact error and which line is throwing it?