标题:[求助]如何取系统文件夹的路径呀?
取消只看楼主
ggyy66
Rank: 1
等 级:新手上路
帖 子:427
专家分:0
注 册:2007-8-14
结帖率:86.25%
 问题点数:0 回复次数:1 
[求助]如何取系统文件夹的路径呀?
如何取系统文件夹的路径呀?
比如 windowsxp的 系统文件夹路径是 c:\windows
但是不一定的,比如用户系统装在D盘,或者不是windows,有可能是别的名称,我在程序中如何取呢?
我现在知道一种办法,用Environ("windir"),不过这个方法是依附于系统的环境变量的,如果用户修改了自已机器的环境变量,用这个函数就取不到了,请大家指教,谢谢.
搜索更多相关主题的帖子: 系统文件 路径 windows 环境变量 
2007-09-05 14:43
ggyy66
Rank: 1
等 级:新手上路
帖 子:427
专家分:0
注 册:2007-8-14
得分:0 
找到了,分享一下吧。

Option Explicit
Dim ret As String
Dim jj As String
Dim winsys As String
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Const max_path = 260
'Private Const max_path1 = 261


Private Sub Command1_Click()
Dim ssave As String
ssave = Space(255)
ret = GetSystemDirectory(ssave, 255)
jj = Len(Trim(ssave))
winsys = Trim(Mid$(ssave, 1, jj - 1))
MsgBox winsys
End Sub

Private Sub Command2_Click()
Dim sysdir As String
Dim chrlen As Long
Dim windir As String

'sysdir变量要给足了空格
sysdir = Space(max_path)
chrlen = GetSystemDirectory(sysdir, max_path) '将系统路径保存在字符串sysdir中,字符串最大长度为260


If chrlen > max_path Then chrlen = GetSystemDirectory(sysdir, chrlen)

'chrlen是返回字符串的实际长度
sysdir = Left(sysdir, chrlen)

'-------------------------------------------------
'取windows路径
windir = Space(max_path)
chrlen = GetWindowsDirectory(windir, max_path)
If chrlen > max_path Then chrlen = GetWindowsDirectory(windir, chrlen)
windir = Left(windir, chrlen)
MsgBox "system的路径为: " & sysdir
MsgBox "windows的路径为: " & windir
MsgBox "当前系统的盘符为:" & Left(windir, 1)
End Sub

2007-09-05 16:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-167851-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.503140 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved