[英]VB.NET Access to the path is denied
NET创建文件夹
我想创建文件夹来保存数据,我想将这些数据复制到其他文件夹。 但是,每当我尝试创建文件夹并生成文件时,我都会收到访问路径“路径”被拒绝的错误
我试图禁用文件夹上的只读选项,但没有用
我试过这种方法,但没有用。
将文件夹保存在 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
您必须授予特定目录的权限(用户读、写)。 例如,如果您在应用程序中创建目录,则必须对应用程序设置权限(读、写)
在这里试试这个不要忘记进口。
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
问题是在前几行代码中您创建了临时目录。 这将相对于程序的执行路径创建。 在我的测试中,它在 /bin/Debug/ 文件夹中创建了它。
然后您尝试更改为默认的直接和临时文件夹名称。 该文件夹不是在此默认目录下创建的,因此这是错误的来源。
在创建目录之前,您需要将 defaultDir 与临时目录结合起来
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()
您必须使用此例程:'递归 function 不断向下移动目录树直到给定深度。
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
然后您必须以这种方式启动它:示例:GetFiles("*.txt", "C:\Docu", 1, 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.