VB 磁盘信息,文件夹,文件操作

 
  1. '文件,文件夹操作类
  2. '
  3. '/工程==>引用==>Microsoft Scripting Runtime
  4. Option Explicit
  5. Private Const HKEY_CLASSES_ROOT = 
  6. Private Const HKEY_CURRENT_USER = 
  7. Private Const HKEY_LOCAL_MACHINE = 
  8. Private Const HKEY_USERS = 
  9. Private Const HKEY_PERFORMANCE_DATA = 
  10. Private Const HKEY_CURRENT_CONFIG = 
  11. Private Const HKEY_DYN_DATA = 
  12. Private Const REG_NONE = 0
  13. Private Const REG_SZ = 1
  14. Private Const REG_EXPAND_SZ = 2
  15. Private Const REG_BINARY = 3
  16. Private Const REG_DWORD = 4
  17. Private Const REG_DWORD_BIG_ENDIAN = 5
  18. Private Const REG_MULTI_SZ = 7
  19. Private Const MAX_PATH = 255
  20. '/磁盘信息结构
  21. Public Type SmDriveInfo
  22.      DriveName As String       '代号或路径
  23.      DriveType As String       '类型
  24.      DriveVolume As String     '卷标
  25.      DriveNumber As String     '序列号
  26.      DriveFileSystem As String '文件系统
  27.      DriveSize As String       '驱动器大小
  28.      DriveFree As String       '可用空间
  29.      DriveIsReady As String    '是否可用
  30. End Type
  31. '/文件夹信息结构
  32. Public Type SmFoldInfo
  33.      Attr As String             '属性
  34.      Size As String             '大小
  35.      DateCreated As String      '建立日期
  36.      DateLastAcce As String     '最后一次存取日期
  37.      DateLastModified As String '最后一次修改日期
  38. End Type
  39. '/文件信息结构
  40. Public Type SmFileInfo
  41.      Attr As String             '属性
  42.      Size As String             '大小
  43.      DateCreated As String      '建立日期
  44.      DateLastAcce As String     '最后一次存取日期
  45.      DateLastModified As String '最后一次修改日期
  46. End Type
  47. '/常量定义
  48. '/程序的显示方式
  49. Private Const SW_SHOWNORMAL = 1
  50. Private Const SW_SHOW = 5
  51. Private Const SW_HIDE = 0
  52. Private Const SW_MINIMIZE = 6
  53. Private Const SW_MAXIMIZE = 3
  54. Private Const SW_RESTORE = 9
  55. Private Const WM_CLOSE = 
  56. '/Synchronize
  57. Private Const INFINITE = 
  58. Private Const NORMAL_PRIORITY_CLASS = 
  59. Private Const SYNCHRONIZE = 
  60. Private Const REALTIME_PRIORITY_CLASS = 
  61. '/结构体
  62. Private Type SECURITY_ATTRIBUTES
  63.         nLength As Long
  64.         lpSecurityDescriptor As Long
  65.         bInheritHandle As Long
  66. End Type
  67. Private Type PROCESS_INFORMATION
  68.         hProcess As Long
  69.         hThread As Long
  70.         dwProcessId As Long
  71.         dwThreadId As Long
  72. End Type
  73. Private Type STARTUPINFO
  74.         cb As Long
  75.         lpReserved As String
  76.         lpDesktop As String
  77.         lpTitle As String
  78.         dwX As Long
  79.         dwY As Long
  80.         dwXSize As Long
  81.         dwYSize As Long
  82.         dwXCountChars As Long
  83.         dwYCountChars As Long
  84.         dwFillAttribute As Long
  85.         dwFlags As Long
  86.         wShowWindow As Integer
  87.         cbReserved2 As Integer
  88.         lpReserved2 As Long
  89.         hStdInput As Long
  90.         hStdOutput As Long
  91.         hStdError As Long
  92. End Type
  93. Private M_AttrRHSA(3, 1) As String
  94. Private Declare Function WritePrivateProfileString _
  95. Lib "kernel32" Alias "WritePrivateProfileStringA" _
  96. (ByVal lpApplicationname As StringByVal _
  97. lpKeyName As Any, ByVal lsString As Any, _
  98. ByVal lplFilename As StringAs Long
  99. Private Declare Function GetPrivateProfileString Lib _
  100. "kernel32" Alias "GetPrivateProfileStringA" _
  101. (ByVal lpApplicationname As StringByVal _
  102. lpKeyName As StringByVal lpDefault As _
  103. StringByVal lpReturnedString As String, _
  104. ByVal nSize As LongByVal lpFileName As _
  105. StringAs Long
  106. '/启动一个应用程序(资源管理器方式)
  107. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
  108.                        (ByVal hWnd As LongByVal lpOperation As String, _
  109.                         ByVal lpFile As StringByVal lpParameters As String, _
  110.                         ByVal lpDirectory As StringByVal nShowCmd As LongAs Long
  111. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
  112.                     ByVal bInheritHandle As LongByVal dwProcessId As LongAs Long
  113. '/建立一个新的进程或线程
  114. Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _
  115.                                         (ByVal lpApplicationname As String, _
  116.                                          ByVal lpCommandLine As String, _
  117.                                          ByVal lpProcessAttributes As Long, _
  118.                                          ByVal lpThreadAttributes As Long, _
  119.                                          ByVal bInheritHandles As Long, _
  120.                                          ByVal dwCreationFlags As Long, _
  121.                                          lpEnvironment As Any, _
  122.                                          ByVal lpCurrentDirectory As String, _
  123.                                          lpStartupInfo As STARTUPINFO, _
  124.                                          lpProcessInformation As PROCESS_INFORMATION) As Long
  125. '/参数说明:
  126. '/lpApplicationName       可执行文件或模块的名字.如果lpCommandLine是NULL,则CreateProcess _
  127.                          执行这个参数中指定的程序.
  128. '/lpCommandLine           如果lpApplicationName是NULL,那么这个参数中程序要启动的程序名字 _
  129.                          如果lpApplicationName和lpCommandLine都是NULL , 那么程序的名字在 _
  130.                          lpApplicationName中指定 , 参数在lpCommandLine中指定.
  131. '/lpProcessAttributes     SECURITY_ATTRIBUTES结构,它定义进程是否能够被继承,一般设为0, _
  132.                          但一般将其定义为ByVal as Long,    (**这与API浏览器中不同)
  133. '/lpThreadAttributes      SECURITY_ATTRIBUTES结构,它定义线程是否能够被断承.一般设为0, _
  134.                          但一般将其定义为ByVal as Long,    (**这与API浏览器中不同)
  135. '/bInheritHandles         布尔值,它表示新创建的进程能否继承调用进程的句柄.
  136. '/dwCreationFlags         为进程指定附加配置参数的标志.
  137. '/lpEnvironment           包含一个以NULL结束的环境参数列表的字符串.如果参数被设为NULL, _
  138.                          则当前的环境被用在新的进程中.
  139. '/lpCurrentDriectory      指向新进程的默认目录的字符串.
  140. '/lpStartupInfo           STARTUPINFO结构,,它配置新进程的外观.
  141. '/lpProcessInformation    PROCESS_INFORMATION结构,返回新建进程的有关信息.
  142. '/返回值                  如果调用成功,则返回一个非零值.如果返回值为0,则调用失败,调用函 _
  143.                          GetLastError可以获取更多的错误信息.
  144. '/---------------------------------------------------------------------------
  145. '等待进程返回或者发生超时.负责调用CreateProcess这个同步操作.
  146. Private Declare Function WaitForSingleObject Lib "kernel32" _
  147.                         (ByVal hHandle As LongByVal dwMilliseconds As LongAs Long
  148. '/参数说明:
  149. '/hHandle                等待进程的句柄
  150. '/dwMilliseconds         等待的时间段,以毫秒为单位.;如果它被设为INFINITE,则这个API将无限期的 _
  151.                         等待的进程返回.
  152. '/返回值                 如果调用成功,则返回引起这个函数返回的值,它可以是以下几个值: _
  153.                         WAIT_ABANDONED,WAIT_OBJECT_O,或者WAIT_TIMEOUT.如果发生了错误,则返回 _
  154.                         WAIT_FAILED.
  155. '/---------------------------------------------------------------------------
  156. '/取临时文件名.
  157. Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As StringByVal lpPrefixString As StringByVal wUnique As LongByVal lpTempFileName As StringAs Long
  158. '
  159. '/关闭以前打开的进程.
  160. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As LongByVal uExitCode As LongAs Long
  161. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As LongAs Long
  162. Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
  163. '/参数说明:
  164. '/hObject                被关闭的句柄.
  165. '/返回值                 如果调用成功,返回一个非零值.若失败则返回值0.
  166. '/---------------------------------------------------------------------------
  167. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As LongByVal lpSubKey As String, phkResult As LongAs Long
  168. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As LongByVal lpSubKey As String, phkResult As LongAs Long
  169. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As LongAs Long
  170. Private Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As LongByVal lpSubKey As StringByVal dwType As LongByVal lpData As StringByVal cbData As LongAs Long
  171. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As LongByVal lpValueName As StringByVal Reserved As LongByVal dwType As Long, lpData As Any, ByVal cbData As LongAs Long
  172. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As StringByVal nSize As LongAs Long
  173. Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias _
  174.         "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, _
  175.         lpFreeBytesAvailableToCaller As Any, lpTotalNumberOfBytes _
  176.         As Any, lpTotalNumberOfFreeBytes As Any) As Long
  177. '
  178. 'RelatFile
  179. '文件关联.
  180. '函数:RelatFile
  181. '参数: FilePath 关联的Exe文件路径,ExpName 关联的扩展名(如.DOC), IcoName图标路径, ShowName 在属性中显示的名称
  182. '返回值:无
  183. Public Function RelatFile(FilePath As String, ExpName As String, IcoName As String, ShowName As String)
  184.     Dim hKey As Long
  185.     Dim IconDir As String
  186.     Dim PathName As String
  187.     
  188.     PathName = FilePath
  189.     IconDir = IcoName & ",0"    '图标路径
  190.     PathName = PathName & " %1" '关联的文件名
  191.     
  192.     hKey = HKEY_CLASSES_ROOT
  193.     RegSetValue hKey, "." & ExpName, REG_SZ, ExpName & "File", 7
  194.     RegSetValue hKey, ExpName & "File", REG_SZ, ShowName, 9
  195.     RegSetValue hKey, ExpName & "File/shell", REG_SZ, "open", 5
  196.     RegSetValue hKey, ExpName & "File/shell/open/command", REG_SZ, _
  197.                 PathName, LenB(StrConv(PathName, vbFromUnicode)) + 1
  198.     RegSetValue hKey, ExpName & "File/DefaultIcon", REG_SZ, _
  199.                 IconDir, LenB(StrConv(IconDir, vbFromUnicode)) + 1
  200.     RegCloseKey hKey
  201. End Function
  202. '
  203. '取文件扩展名.
  204. '函数:FileExpName
  205. '参数: Fname 文件绝对路径.
  206. '返回值:文件名.
  207. '如:"C:/PROMAS/AA.EXE",则返回 "EXE"
  208. Public Function FileExpName(Fname As StringAs String
  209.     On Error Resume Next
  210.     
  211.     Dim I As Integer
  212.     
  213.     I = InStrRev(Fname, ".")
  214.     If I = 0 Then Exit Function
  215.     FileExpName = Mid$(Fname, I + 1, 12)
  216. End Function
  217. '
  218. '取文件名,没有扩展名
  219. '函数:FileName
  220. '参数: Fname 文件绝对路径.
  221. '返回值:文件名.
  222. '如:"C:/PROMAS/AA.EXE",则返回 "AA"
  223. Public Function Filename(Fname As StringAs String
  224.       Dim A As Integer
  225.       Dim B As Integer
  226.       Dim JlStr As String
  227.       Filename = "":  B = 0
  228.       For A = Len(Fname) To 1 Step -1
  229.           If Mid$(Fname, A, 1) = "/" Then
  230.              B = A: GoTo 100
  231.           End If
  232.       Next A
  233.     
  234. 100:
  235.     JlStr = Right$(Fname, Len(Fname) - B)
  236.     B = 1
  237.     For A = Len(JlStr) To 1 Step -1
  238.         If Mid$(JlStr, A, 1) = "." Then
  239.            B = A: GoTo 200
  240.         End If
  241.     Next A
  242. 200:
  243.       Filename = Left$(JlStr, B - 1)
  244. End Function
  245. '
  246. '取文件名,有扩展名
  247. '函数:FileNameExp
  248. '参数: Fname 文件绝对路径.
  249. '返回值:文件名.
  250. '如:"C:/PROMAS/AA.EXE",则返回 "AA.EXE"
  251. Public Function FileNameExp(Fname As StringAs String
  252.     Dim A As Integer
  253.     Dim B As Integer
  254.     Dim JlStr As String
  255.     FileNameExp = ""
  256.     B = 0
  257.     For A = Len(Fname) To 1 Step -1
  258.         If Mid$(Fname, A, 1) = "/" Then
  259.            B = A: GoTo 100
  260.         End If
  261.     Next A
  262. 100:
  263.     JlStr = Right$(Fname, Len(Fname) - B)
  264.     FileNameExp = JlStr
  265. End Function
  266. '
  267. '取路径名
  268. '函数:FilePath
  269. '参数: Fname 文件绝对路径.
  270. '返回值:路径名.
  271. '如:"C:/PROMAS/AA.EXE",则返回 "C:/PROMAS/"
  272. Public Function FilePath(Fname As StringAs String
  273.     Dim A As Integer
  274.     Dim B As Integer
  275.     Dim JlStr As String
  276.     FilePath = ""
  277.     B = 0
  278.     For A = Len(Fname) To 1 Step -1
  279.         If Mid$(Fname, A, 1) = "/" Then
  280.            B = A: GoTo 100
  281.         End If
  282.     Next A
  283.     
  284. 100:
  285.     JlStr = Left$(Fname, B)
  286.     FilePath = JlStr
  287. End Function
  288. '
  289. '建立文件夹
  290. '函数:CreateDir
  291. '参数: DirPath 新建文件夹路径.
  292. '返回值:=T 成功,=F 失败.
  293. Public Function CreateDir(DirPath As StringAs Boolean
  294.      Dim c As String
  295.      Dim A As Long
  296.      Dim LeftName As String
  297.      
  298.      On Error Resume Next
  299.      
  300.      c = Trim$(DirPath)
  301.      If Len(c) < 2 Then Err.Number = -1: GoTo Errhan
  302.      If Dir$(Left$(c, 2), vbDirectory) = "" Then Err.Number = -1: GoTo Errhan '根目录是否存在
  303.      '/-------------------------------------------------------
  304.      If Right$(c, 1) <> "/" Then c = c & "/"
  305.      For A = 1 To Len(c)
  306.          If Mid$(c, A, 1) = "/" Then
  307.             LeftName = Left$(c, A)
  308.             If Dir$(LeftName, vbDirectory + vbHidden) = "" Then MkDir LeftName: DoEvents
  309.          End If
  310.      Next A
  311. Errhan:
  312.      If Err.Number = 0 Then
  313.         Err.Clear
  314.         CreateDir = True
  315.      Else
  316.         Err.Clear
  317.         CreateDir = False
  318.      End If
  319. End Function
  320. '
  321. '取磁盘信息
  322. '函数:GetDiskInfo
  323. '参数: DriveName 目标驱动器.
  324. '返回值:SmDriveInfo 结构.
  325. Public Function GetDiskInfo(DriveName As StringAs SmDriveInfo
  326.       Dim C1 As Currency
  327.       Dim C2 As Currency
  328.       Dim C3 As Currency
  329.       Dim A1 As Long
  330.       Dim Fs As New FileSystemObject
  331.       Dim Dr As Drive
  332.       Dim ReturnValue As SmDriveInfo
  333.       On Error Resume Next
  334.       Set Dr = Fs.GetDrive(DriveName)
  335.     '/------------------------------------------
  336.       ReturnValue.DriveName = Dr.Path                 '代号或路径
  337.       GetDiskFreeSpaceEx DriveName, C1, C2, C3
  338.       ReturnValue.DriveIsReady = Dr.IsReady           '是否可用
  339.       ReturnValue.DriveType = Dr.DriveType            '类型
  340.       ReturnValue.DriveVolume = Dr.VolumeName         '卷标
  341.       ReturnValue.DriveNumber = Hex(Dr.SerialNumber)  '序列号
  342.       ReturnValue.DriveFileSystem = Dr.FileSystem     '文件系统
  343.       ReturnValue.DriveSize = C2 * 10000              '驱动器大小
  344.       ReturnValue.DriveFree = C1 * 10000              '可用空间
  345.       GetDiskInfo = ReturnValue
  346.       Set Fs = Nothing
  347. End Function
  348. '
  349. '文件夹属性.
  350. '函数:GetFolderInfo
  351. '参数: FolderPath 目标文件夹路径.
  352. '返回值:SmFoldInfo 结构.
  353. Public Function GetFolderInfo(FolderPath As StringAs SmFoldInfo
  354.     Dim Fs As New FileSystemObject
  355.     Dim Fd As Folder
  356.     Dim RetuAttr As Long
  357.     Dim FdAttr As String
  358.     Dim A As Long
  359.     Dim Fsize As Long
  360.     Dim ReturnValue As SmFoldInfo
  361.     On Error Resume Next
  362.      
  363.     If Len(FolderPath) = 0 Then Exit Function
  364.     Set Fd = Fs.GetFolder(FolderPath)
  365.     If Fd.IsRootFolder Then               '根目录
  366.        ReturnValue.Size = ""              '大小
  367.        ReturnValue.DateCreated = ""       '建立时间
  368.        ReturnValue.DateLastAcce = ""      '最后一次存取日期
  369.        ReturnValue.DateLastModified = ""  '最后一次修改时间
  370.        ReturnValue.Attr = ""              '属性
  371.     Else
  372.        Call FAattr
  373.        RetuAttr = Fd.Attributes
  374.        For A = 0 To 3
  375.            If (RetuAttr And CInt(M_AttrRHSA(A, 0))) <> 0 Then
  376.                FdAttr = FdAttr & M_AttrRHSA(A, 1)
  377.            End If
  378.        Next A
  379.        Fsize = Fd.Size
  380.        ReturnValue.Size = Fsize
  381.        ReturnValue.DateCreated = Fd.DateCreated             '建立时间
  382.        ReturnValue.DateLastAcce = Fd.DateLastAccessed       '最后一次存取日期
  383.        ReturnValue.DateLastModified = Fd.DateLastModified   '最后一次修改时间
  384.        ReturnValue.Attr = FdAttr                            '属性
  385.     End If
  386.     GetFolderInfo = ReturnValue
  387.     Set Fs = Nothing
  388. End Function
  389. '
  390. '文件信息
  391. '函数:GetFileInfo
  392. '参数: FileName 目标文件名.
  393. '返回值:FileInfo结构.
  394. Public Function GetFileInfo(Filename As StringAs SmFileInfo
  395.         Dim Fs As New FileSystemObject
  396.         Dim F As File
  397.         Dim RetuAttr As Long
  398.         Dim FdAttr As String
  399.         Dim A As Long
  400.         Dim Fsize As Long
  401.         Dim ReturnValue As SmFileInfo
  402.         On Error Resume Next
  403.         Fsize = 0
  404.         Set F = Fs.GetFile(Filename)
  405.         RetuAttr = F.Attributes
  406.         For A = 0 To 3
  407.             If (RetuAttr And CInt(M_AttrRHSA(A, 0))) <> 0 Then
  408.                FdAttr = FdAttr & M_AttrRHSA(A, 1)
  409.             End If
  410.         Next A
  411.         Fsize = F.Size: DoEvents
  412.         ReturnValue.Size = Fsize
  413.         ReturnValue.DateCreated = F.DateCreated            '建立时间
  414.         ReturnValue.DateLastAcce = F.DateLastAccessed      '最后一次存取日期
  415.         ReturnValue.DateLastModified = F.DateLastModified  '最后一次修改时间
  416.         ReturnValue.Attr = FdAttr
  417.         GetFileInfo = ReturnValue
  418.         Set Fs = Nothing
  419. End Function
  420. '
  421. '文件复制
  422. '函数:FileCopy
  423. '参数: SourFile 源文件名,ObjFile 目标文件名
  424. '返回值:=T 成功,=F 失败.
  425. Function FileCopy(SourFile As String, ObjFile As StringAs Boolean  '文件复制
  426.     Dim Fs As New FileSystemObject
  427.     On Error Resume Next
  428.     Fs.CopyFile SourFile, ObjFile, True
  429.     If Err.Number <> 0 Then
  430.        Err.Clear
  431.        FileCopy = False
  432.     Else
  433.        FileCopy = True
  434.     End If
  435. End Function
  436. '
  437. '文件移动
  438. '函数:FileRename
  439. '参数: SourFile 源文件名,ObjFile 目标文件名
  440. '返回值:=T 成功,=F 失败.
  441. Function FileMove(SourFile As String, ObjFile As StringAs Boolean '文件移动
  442.     Dim Fs As New FileSystemObject
  443.     On Error Resume Next
  444.     Fs.MoveFile SourFile, ObjFile
  445.     If Err.Number <> 0 Then
  446.        Err.Clear
  447.        FileMove = False
  448.     Else
  449.        FileMove = True
  450.     End If
  451.     Set Fs = Nothing
  452. End Function
  453. '
  454. '文件更名
  455. '函数:FileRename
  456. '参数: SOURFILE 源文件名.OBJFILE 更改后的名字(绝对路径)
  457. '返回值:=T 成功,=F 失败.
  458. Function FileRename(SourFile As String, ObjFile As StringAs Boolean '文件改名
  459.   Dim Fs As New FileSystemObject
  460.   On Error Resume Next
  461.   SetAttr SourFile, 0
  462.   Call FileCopy(SourFile, ObjFile)
  463.   Call FileDel(SourFile)
  464.   If Err.Number <> 0 Then
  465.      Err.Clear
  466.      FileRename = False
  467.   Else
  468.      FileRename = True
  469.   End If
  470.   Set Fs = Nothing
  471. End Function
  472. '
  473. '文件删除
  474. '函数:FileDel
  475. '参数: SOURFILE 删除的文件名称
  476. '返回值:=T 成功,=F 失败.
  477. Function FileDel(SourFile As StringAs Boolean
  478.    Dim Fs As New FileSystemObject
  479.    On Error Resume Next
  480.    SetAttr SourFile, 0       '取消一切属性
  481.    Fs.DeleteFile SourFile, True
  482.    If Err.Number <> 0 Then
  483.       Err.Clear
  484.       FileDel = False
  485.    Else
  486.       FileDel = True
  487.    End If
  488.    Set Fs = Nothing
  489. End Function
  490. '
  491. '文件夹的复制
  492. '函数:FolderCopy
  493. '参数: SOURFOLDER 源文件夹名,OBJFILDER 目标文件夹名
  494. '返回值:=T 成功,=F 失败.
  495. Function FolderCopy(SourFolder As String, ObjFolder As StringAs Boolean
  496.     Dim Fs As New FileSystemObject
  497.     On Error Resume Next
  498.     Fs.CopyFolder SourFolder, ObjFolder, True
  499.     If Err.Number <> 0 Then
  500.        Err.Clear
  501.        FolderCopy = False
  502.     Else
  503.        FolderCopy = True
  504.     End If
  505. End Function
  506. '
  507. '文件夹的移动
  508. '函数:FolderMove
  509. '参数: SOURFILDER 源文件夹名, OBJFOLDER 目标文件夹名
  510. '返回值:=T 成功,=F 失败.
  511. Function FolderMove(SourFolder As String, ObjFolder As StringAs Boolean
  512.     Dim Fs As New FileSystemObject
  513.     On Error Resume Next
  514.     Fs.MoveFolder SourFolder, ObjFolder
  515.     If Err.Number <> 0 Then
  516.        Err.Clear
  517.        FolderMove = False
  518.     Else
  519.        FolderMove = True
  520.     End If
  521.     Set Fs = Nothing
  522. End Function
  523. '
  524. '文件夹的删除
  525. '函数:FolderDel
  526. '参数:SourFolder 删除的文件夹名称
  527. '返回值:=T 成功,=F 失败.
  528. Function FolderDel(SourFolder As StringAs Boolean
  529.     Dim Fs As New FileSystemObject
  530.     On Error Resume Next
  531.     Fs.DeleteFolder SourFolder, True
  532.     If Err.Number <> 0 Then
  533.        Err.Clear
  534.        FolderDel = False
  535.     Else
  536.        FolderDel = True
  537.     End If
  538.     Set Fs = Nothing
  539. End Function
  540. '
  541. '文件夹更名
  542. '函数:FolderRename
  543. '参数:SourFolder 原文件夹名称,ObjFolder 更改后的文件夹名称.
  544. '返回值:=T 成功,=F 失败.
  545. Function FolderRename(SourFolder As String, ObjFolder As StringAs Boolean
  546.     Dim Fs As New FileSystemObject
  547.     On Error Resume Next
  548.     If Right(SourFolder, 1) = "/" Then SourFolder = Left(SourFolder, Len(SourFolder) - 1)
  549.     If Right(ObjFolder, 1) = "/" Then ObjFolder = Left(ObjFolder, Len(ObjFolder) - 1)
  550.     Fs.MoveFolder SourFolder, ObjFolder
  551.     If Err.Number <> 0 Then
  552.        Err.Clear
  553.        FolderRename = False
  554.     Else
  555.        FolderRename = True
  556.     End If
  557.     Set Fs = Nothing
  558. End Function
  559. '
  560. '设置驱动器卷标
  561. '函数:SetVolume
  562. '参数:DriveName 驱动器名称,NewVolueName 新的卷标名.
  563. '返回值:无
  564. Public Function SetVolume(DriveName As String, NewVolueName As String)
  565.         Dim Fs As New FileSystemObject
  566.         Dim Dr As Drive
  567.         On Error Resume Next
  568.         Set Dr = Fs.GetDrive(DriveName)
  569.         Dr.VolumeName = NewVolueName
  570.         Set Fs = Nothing
  571. End Function
  572. Private Sub FAattr()
  573.     M_AttrRHSA(0, 0) = 1: M_AttrRHSA(0, 1) = "R"
  574.     M_AttrRHSA(1, 0) = 2: M_AttrRHSA(1, 1) = "H"
  575.     M_AttrRHSA(2, 0) = 32: M_AttrRHSA(2, 1) = "A"
  576.     M_AttrRHSA(3, 0) = 4: M_AttrRHSA(3, 1) = "S"
  577. End Sub
  578. '
  579. '打开一个文件.
  580. '函数: ShellFile
  581. '参数:
  582. '    FileName 要打开的文件名 _
  583. '    Wait     布尔值,是否同步启动: =TRUE 同步, =FALSE 异步 _
  584. '    DoPath   文件的所在目录
  585. '返回值:  =1 成功,=0 失败
  586. '*注:如果是打开一个关联文件,必须用异步方式,即Wait=False,
  587. '    例如要直接打开某个 ".DOC" 文档,则 WAIT=FALSE.
  588. Public Function ShellFile(ByVal Filename As String, _
  589.                           Optional WinHwnd As Long = 0, _
  590.                           Optional Wait As Boolean = FalseAs Long
  591.                           
  592.   Dim Proc As PROCESS_INFORMATION
  593.   Dim Start As STARTUPINFO
  594.   Dim Rc As Long
  595.   Dim Mdriv As String
  596.   Dim OpenName As String
  597.   Dim OpenPath As String
  598.   
  599.   OpenName = FileNameExp(Filename): OpenPath = FilePath(Filename)
  600.   If Wait Then '如果T,同步启动
  601.      Start.cb = Len(Start)
  602.      '/建立一个新的进程
  603.      Rc = CreateProcess(Filename, OpenPath, _
  604.                         ByVal 0, ByVal 0, 1, _
  605.                         NORMAL_PRIORITY_CLASS, _
  606.                         ByVal 0, OpenPath, _
  607.                         Start, Proc)
  608.      '/等待,直到进程结束
  609.      Rc = WaitForSingleObject(Proc.hProcess, INFINITE)
  610.      '/关闭进程
  611.      Rc = CloseHandle(Proc.hProcess)
  612.   Else
  613.      '/否则,异步启动,(资源管理器启动)
  614.      Rc = ShellExecute(WinHwnd, "Open", OpenName, "", OpenPath, SW_MAXIMIZE)
  615.   End If
  616.   ShellFile = Rc
  617. End Function
  618. '
  619. '启动EXE文件
  620. '函数:OpenExe
  621. '参数:FileName EXE文件名,WorkPath 工作目录.
  622. '返回值:该EXE的进程句柄
  623. Public Function OpenExe(ByVal Filename As String, _
  624.                         Optional WorkPath As String = ""As Long
  625.   Dim Proc As PROCESS_INFORMATION
  626.   Dim Start As STARTUPINFO
  627.   Dim Rc As Long
  628.   Dim Mdriv As String
  629.   Dim A As String
  630.   Dim B As String
  631.   Dim ExeName As String
  632.   
  633.   On Error Resume Next
  634.   
  635.   ExeName = FileNameExp(Filename)
  636.   WorkPath = FilePath(Filename)
  637.   ChDrive Left$(WorkPath, 2)
  638.   ChDir WorkPath
  639.   Rc = CreateProcess(ExeName, WorkPath, _
  640.                      ByVal 0, ByVal 0, 1, _
  641.                      NORMAL_PRIORITY_CLASS, _
  642.                      ByVal 0, vbNullString, _
  643.                      Start, Proc)
  644.   OpenExe = Proc.hProcess
  645. End Function
  646. '
  647. '关闭一个EXE文件
  648. '函数:CloseExe
  649. '参数:ProID 该EXE的进程句柄
  650. '返回值:无
  651. Public Function CloseExe(ProID As Long)
  652.      Dim Rc As Long
  653.      '/中断一个进程
  654.       Call TerminateProcess(ProID, 0)
  655.      '/关闭该进程
  656.       Call CloseHandle(ProID)
  657. End Function
  658. '
  659. '运行一个程序或文档.
  660. '函数:RunFile
  661. '参数:FilePath 要打开的文件路径
  662. '返回值:无
  663. '注:实际上是 [开始]==>[运行]
  664. Public Function RunFile(FilePath As String)
  665.        Call Shell("rundll32.exe url.dll,FileProtocolHandler " & FilePath, 1)
  666. End Function
  667. Private Sub Class_Initialize()
  668. '    Dim T As New ClsRev
  669. '    Call T.GetIniVal
  670. '    Set T = Nothing
  671. End Sub
  672. '
  673. '取临时文件名.
  674. '函数:GetTempName
  675. '参数:TmpNameDir 创建临时文件目录,FilePreFix 临时文件前缀.
  676. '返回值:一件临时文件名.
  677. Public Function GetTempName(Optional TmpDir As String = ""Optional FilePreFix = "TmpFile"As String
  678.    Dim TempFileName As String * 256
  679.    Dim X As Long
  680.    Dim DriveName As String
  681.    Dim SysTmpDir As New SmSysCls
  682.       
  683.    If Len(Trim$(TmpDir)) = 0 Then
  684.       TmpDir = SysTmpDir.GetFolder(SmWinTempDirectory)
  685.    End If
  686.    If Len(Trim$(TmpDir)) = 0 Then TmpDir = "C:/"
  687.    If Dir$(TmpDir, vbDirectory + vbAlias + vbSystem + vbHidden) = "" Then TmpDir = "C:/"
  688.    If Right$(TmpDir, 1) <> "/" Then TmpDir = TmpDir & "/"
  689.    
  690.    DriveName = TmpDir
  691.    X = GetTempFileName(DriveName, FilePreFix, 0, TempFileName)
  692.    GetTempName = Left$(TempFileName, InStr(TempFileName, Chr$(0)) - 1)
  693.    Set SysTmpDir = Nothing
  694. End Function
  695. '
  696. '判断某个文件是否存在.
  697. '函数:FileCheck
  698. '参数:FileName 目标文件名.
  699. '返回值:=TRUE 存在,=FALSE 不存在.
  700. Public Function FileCheck(Filename As StringAs Boolean
  701.      Dim FileID As Long
  702.      
  703.      On Error Resume Next
  704.      
  705.      FileID = FreeFile()
  706.      Open Filename For Input As #FileID
  707.      Close #FileID
  708.      FileCheck = (Err.Number = 0)
  709.      Err.Clear
  710. End Function
  711. '
  712. '读INI文件.
  713. '函数:GetIniStr
  714. '参数:AppName 项目名.In_Key 键名,sFileName 文件名
  715. '返回值:成功:对应的键值.失败或不存在:""
  716. Public Function GetIniStr(ByVal AppName As StringByVal In_Key As StringByVal sFileName As StringAs String
  717.     
  718.     On Error GoTo GetIniStrErr
  719.     
  720.     If VBA.Trim$(In_Key) = "" Then
  721.        GoTo GetIniStrErr
  722.     End If
  723.     Dim GetStr As String
  724.     GetStr = VBA.String(128, 0)
  725.     GetPrivateProfileString AppName, In_Key, "", GetStr, 256, sFileName
  726.     GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
  727.     If GetStr = "" Then
  728.        GoTo GetIniStrErr
  729.     Else
  730.        GetIniStr = GetStr
  731.        GetStr = ""
  732.     End If
  733.     Exit Function
  734. GetIniStrErr:
  735.        Err.Clear
  736.        GetIniStr = ""
  737.        GetStr = ""
  738. End Function
  739. '
  740. '写INI文件.
  741. '函数:WriteIniStr
  742. '参数:AppName 项目名.In_Key 键名,In_Data 键值,sFileName 文件名
  743. '返回值:成功=TRUE.失败=FALSE
  744. Public Function WriteIniStr(ByVal AppName As StringByVal In_Key As StringByVal in_data As StringByVal sFileName As StringAs Boolean
  745.     On Error GoTo WriteIniStrErr
  746.     WriteIniStr = True
  747.     If VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
  748.        GoTo WriteIniStrErr
  749.     Else
  750.      WritePrivateProfileString AppName, In_Key, in_data, sFileName
  751.     End If
  752.     Exit Function
  753.     
  754. WriteIniStrErr:
  755.        Err.Clear
  756.        WriteIniStr = False
  757. End Function
  758. '
  759. '读TEXT文件
  760. '函数:RedTextFile
  761. '参数:FileName 打开的TXT文件名.
  762. '返回值:成功 返回文件内容.失败  返回""
  763. Public Function RedTextFile(Filename As StringAs String
  764.      Dim FileID As Long
  765.      Dim InputStr As String
  766.      Dim LineStr As String
  767.      
  768.      On Error Resume Next
  769.      
  770.      InputStr = "": LineStr = ""
  771.      FileID = FreeFile()
  772.      Open Filename For Input As #FileID
  773.           Do While Not EOF(FileID)           ' 循环至文件尾。
  774.              LineStr = ""
  775.              Line Input #FileID, LineStr
  776.              InputStr = InputStr & LineStr
  777.           Loop
  778.      Close #FileID
  779.      RedTextFile = IIf(Err.Number = 0, InputStr, "")
  780.      Err.Clear
  781. End Function
  782. '
  783. '写TEXT文件
  784. '函数:WritTextFile
  785. '参数:FileName 目标文件名.WritStr 写到目标的字符串.
  786. '返回值:成功 返回文件内容.失败  返回""
  787. '注:如果同名,目标字符串将覆盖原文件内容.
  788. Public Function WritTextFile(Filename As String, WritStr As StringAs Boolean
  789. '/保存文件
  790.     Dim FileID As Long, ConTents As String
  791.     Dim A As Long, B As Long
  792.     
  793.     On Error Resume Next
  794.     
  795.     FileID = FreeFile
  796.     Open Filename For Output As #FileID
  797.          Print #FileID, WritStr
  798.     Close #FileID
  799.     WritTextFile = (Err.Number = 0)
  800.     Err.Clear
  801. End Function
  802. '
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值