VB.NET 对路径的访问被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25049780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET Access to the path is denied
提问by Bob Ma
NET to create folder
NET创建文件夹
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"
将文件夹保存在 Program Files 的文件夹下,我正在将数据复制到“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
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
采纳答案by user3183324
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
回答by Dinesh Mandaviya
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)
您必须为特定目录授予权限(用户的读取、写入)。例如,如果您在应用程序中创建目录,则必须为应用程序设置权限(读、写)
回答by Gridly
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.
问题是在前几行代码中您创建了临时目录。这将相对于程序的执行路径创建。在我的测试中,它在 /bin/Debug/ 文件夹中创建了它。
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
在创建目录之前,您需要将 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()

