如果不存在,如何在VB中创建文件夹?
我为自己编写了一个小小的下载应用程序,以便我可以轻松地从服务器上获取一组文件,然后将它们全部放入带有全新安装的Windows的新PC上,而无需实际运行网络。不幸的是,我在创建要放入的文件夹时遇到了问题,不确定如何处理。
我希望我的程序将应用程序下载到"程序文件\此处任何名称"
因此,基本上我需要一个函数来检查文件夹是否存在,如果不存在,它将创建它。
解决方案
回答
试试这个:Directory.Exists(TheFolderName)
和Directory.CreateDirectory(TheFolderName)
(我们可能需要:Imports System.IO
)
回答
VB.NET? System.IO.Directory.Exists(字符串路径)
回答
If(Not System.IO.Directory.Exists(YourPath)) Then System.IO.Directory.CreateDirectory(YourPath) End If
回答
尝试使用System.IO.DirectoryInfo类。
来自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
回答
在System.IO下,有一个名为Directory的类。
请执行下列操作:
If Not Directory.Exists(path) Then Directory.CreateDirectory(path) End If
这将确保该目录在那里。
回答
我们应该尝试使用文件系统对象或者FSO。属于该对象的方法有很多,它们可以检查文件夹是否存在以及创建新文件夹。
回答
Directory.CreateDirectory()应该这样做。
http://msdn.microsoft.com/zh-cn/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
回答
由于问题未指定.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
回答
我知道它是如何工作的,创建对话框的过程将是什么,该对话框允许用户命名文件夹并将其放置在所需的位置。
干杯
回答
If Not Directory.Exists(somePath) then Directory.CreateDirectory(somePath) End If