VB.NET拒绝访问该路径

时间:2014-07-31 02:13:44

标签: vb.net file-io

NET创建文件夹

我想创建用于保存数据的文件夹 我想将这些数据复制到其他文件夹。 但是,无论何时,我都尝试创建文件夹并生成文件 我正在访问路径“path”被拒绝错误

我尝试在文件夹上禁用只读选项,但它无法正常工作

我试过这种方式但是没有用。

将文件夹保存在Program Files的文件夹下 我正在将数据复制到“C:RESULT”

然而,它不起作用..我不知道为什么......

您可以帮我了解如何创建文件夹并将数据复制到新文件夹吗?

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

3 个答案:

答案 0 :(得分:1)

您必须为特定目录授予权限(读取,写入用户)。 例如,如果您在应用程序中创建目录,则必须在应用程序上设置权限(读取,写入)

答案 1 :(得分:0)

问题是在前几行代码中创建临时目录。这将相对于程序的执行路径创建。在我的测试中,它在/ bin / Debug /文件夹中创建了它。

然后尝试更改为默认的直接和临时文件夹名称。该文件夹未在此默认目录下创建,因此该错误来自。

在创建目录

之前,需要将defaultDir与temp dir组合在一起
    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()

答案 2 :(得分:0)

这里尝试这个不要忘记进口。

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