题
我自己编写了一个小下载应用程序,这样我就可以轻松地从我的服务器获取一组文件,并将它们全部放到一台全新安装了 Windows 的新电脑上,而无需实际上网。不幸的是,我在创建要放入它们的文件夹时遇到问题,并且不确定如何进行。
我希望我的程序将应用程序下载到 program files\any name here\
所以基本上我需要一个函数来检查文件夹是否存在,如果不存在则创建它。
解决方案
If(Not System.IO.Directory.Exists(YourPath)) Then
System.IO.Directory.CreateDirectory(YourPath)
End If
其他提示
在System.IO下,有一个类叫做Directory。请执行下列操作:
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
它将确保该目录存在。
由于问题没有指定 .NET,因此这应该适用于 VBScript 或 VB6。
Dim objFSO, strFolder
strFolder = "C:\Temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolder) Then
objFSO.CreateFolder(strFolder)
End If
尝试一下 系统.IO.目录信息 班级。
来自MSDN的示例:
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
' Specify the directories you want to manipulate.
Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
Try
' Determine whether the directory exists.
If di.Exists Then
' Indicate that it already exists.
Console.WriteLine("That path exists already.")
Return
End If
' Try to create the directory.
di.Create()
Console.WriteLine("The directory was created successfully.")
' Delete the directory.
di.Delete()
Console.WriteLine("The directory was deleted successfully.")
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
VB.NET?System.IO.Directory.Exists(字符串路径)
尝试这个: Directory.Exists(TheFolderName)
和 Directory.CreateDirectory(TheFolderName)
(你可能需要: Imports System.IO
)
Directory.CreateDirectory() 应该这样做。http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory(VS.71).aspx
另外,在 Vista 中,您可能无法写入 C:除非您以管理员身份运行它,否则您可能只想绕过它并在 C: 的子目录中创建所需的目录:(无论如何,我想说这是一个值得遵循的好习惯。-- 令人难以置信的是,有多少人把垃圾扔到 C:
希望有帮助。
(导入System.IO)
if Not Directory.Exists(Path) then Directory.CreateDirectory(Path) end if
If Not Directory.Exists(somePath) then
Directory.CreateDirectory(somePath)
End If
您应该尝试使用文件系统对象或 FSO。有许多属于该对象的方法可以检查文件夹是否存在以及创建新文件夹。
我了解这是如何工作的,创建一个允许用户命名文件夹并将其放置在您想要的位置的对话框的过程是什么。
干杯
只需这样做:
Dim sPath As String = "Folder path here"
If (My.Computer.FileSystem.DirectoryExists(sPath) = False) Then
My.Computer.FileSystem.CreateDirectory(sPath + "/<Folder name>")
Else
'Something else happens, because the folder exists
End If
我将文件夹路径声明为字符串(sPath),这样,如果您多次使用它,则可以轻松更改它,而且也可以通过程序本身进行更改。
希望能帮助到你!
-nfell2009