adinas
asked on
Hide/Show File extesion through VB code
Hi,
I need to know how to show or hide the file extensions on a windows 98 machine. I know how it is done through Windows Explorer (Click View -> Folder Options, check or uncheck the 'Hide file extensions for known file types' in the View tab)
But I need to be able to do it in Visual Basic 6. I guess its an API call...
Thanks
A
I need to know how to show or hide the file extensions on a windows 98 machine. I know how it is done through Windows Explorer (Click View -> Folder Options, check or uncheck the 'Hide file extensions for known file types' in the View tab)
But I need to be able to do it in Visual Basic 6. I guess its an API call...
Thanks
A
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
But why do this through VB!?
How-to info:
I would say the answer to your question is basically a two-
step process.
1) The first step is exactly what you said. You got to open your
Windows Explorer and change the settings so as to hide/show file
extensions. To do so, Click View -> Folder Options, check or
uncheck the 'Hide file extensions for known file types' in the
View tab.
2) The second step is to open the Windows Explorer application.
This you can do using the VB 'Shell' function. As you open the
Windows Explorer, it will take the settings you set in step 1 by
default.
I am providing the source code for the same(tested it to be
working) and also attaching some useful info on the VB 'Shell'
function.
Source Code:
' code starts here
Private Sub eRoot(rootpath As String, fldrs As Boolean)
' Call Explorer with certain directory open
' fldrs is the folders switch, toggle it and see the difference
On Error Resume Next
Dim EX, ARGU, path, X
If fldrs = True Then
EX = "explorer.exe"
' ARGU = " /e,/root, " ' Makes Path the Root
ARGU = " /e, " ' Gives Path w/open folders
path = rootpath$
X = Shell(EX & ARGU & path, 1)
ElseIf fldrs = False Then ' Not Used - Folder View
EX = "explorer.exe"
ARGU = " n/e,/,root, "
path = rootpath$
X = Shell(EX & ARGU & path, 1)
End If
End Sub
Private Sub WinExplorer_Click()
eRoot "c:\temp", True
End Sub
' code ends here
(Create the command button "WinExplorer" on your form first and
then execute the code.The path is the path where the explorer
opens by default.)
This is your answer. Check the 'shell' function help for more
info.
The other way which could have been possible was directly
enabling/disabling the setting in Windows Explorer but then you
ought to have one explorer object, which unfortunately does not
exist, at least as far as I am aware of.
Check out the attachment..
%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%
Shell Function..
Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.
Syntax
Shell(pathname[,windowstyl e])
The Shell function syntax has these named arguments:
Part Description
pathname Required; Variant (String). Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive.
windowstyle Optional. Variant (Integer) corresponding to the style of the window in which the program is to be run. If windowstyle is omitted, the program is started minimized with focus.
The windowstyle named argument has these values:
Constant Value Description
vbHide 0 Window is hidden and focus is passed to the hidden window.
vbNormalFocus 1 Window has focus and is restored to its original size and position.
vbMinimizedFocus 2 Window is displayed as an icon with focus.
vbMaximizedFocus 3 Window is maximized with focus.
vbNormalNoFocus 4 Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus 6 Window is displayed as an icon. The currently active window remains active.
Remarks
If the Shell function successfully executes the named file, it returns the task ID of the started program. The task ID is a unique number that identifies the running program. If the Shell function can't start the named program, an error occurs.
Note By default, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed.
I would say the answer to your question is basically a two-
step process.
1) The first step is exactly what you said. You got to open your
Windows Explorer and change the settings so as to hide/show file
extensions. To do so, Click View -> Folder Options, check or
uncheck the 'Hide file extensions for known file types' in the
View tab.
2) The second step is to open the Windows Explorer application.
This you can do using the VB 'Shell' function. As you open the
Windows Explorer, it will take the settings you set in step 1 by
default.
I am providing the source code for the same(tested it to be
working) and also attaching some useful info on the VB 'Shell'
function.
Source Code:
' code starts here
Private Sub eRoot(rootpath As String, fldrs As Boolean)
' Call Explorer with certain directory open
' fldrs is the folders switch, toggle it and see the difference
On Error Resume Next
Dim EX, ARGU, path, X
If fldrs = True Then
EX = "explorer.exe"
' ARGU = " /e,/root, " ' Makes Path the Root
ARGU = " /e, " ' Gives Path w/open folders
path = rootpath$
X = Shell(EX & ARGU & path, 1)
ElseIf fldrs = False Then ' Not Used - Folder View
EX = "explorer.exe"
ARGU = " n/e,/,root, "
path = rootpath$
X = Shell(EX & ARGU & path, 1)
End If
End Sub
Private Sub WinExplorer_Click()
eRoot "c:\temp", True
End Sub
' code ends here
(Create the command button "WinExplorer" on your form first and
then execute the code.The path is the path where the explorer
opens by default.)
This is your answer. Check the 'shell' function help for more
info.
The other way which could have been possible was directly
enabling/disabling the setting in Windows Explorer but then you
ought to have one explorer object, which unfortunately does not
exist, at least as far as I am aware of.
Check out the attachment..
%%%%%%%%%%%%%%%%%%%%%%%%%%
Shell Function..
Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.
Syntax
Shell(pathname[,windowstyl
The Shell function syntax has these named arguments:
Part Description
pathname Required; Variant (String). Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive.
windowstyle Optional. Variant (Integer) corresponding to the style of the window in which the program is to be run. If windowstyle is omitted, the program is started minimized with focus.
The windowstyle named argument has these values:
Constant Value Description
vbHide 0 Window is hidden and focus is passed to the hidden window.
vbNormalFocus 1 Window has focus and is restored to its original size and position.
vbMinimizedFocus 2 Window is displayed as an icon with focus.
vbMaximizedFocus 3 Window is maximized with focus.
vbNormalNoFocus 4 Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus 6 Window is displayed as an icon. The currently active window remains active.
Remarks
If the Shell function successfully executes the named file, it returns the task ID of the started program. The task ID is a unique number that identifies the running program. If the Shell function can't start the named program, an error occurs.
Note By default, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed.
You can call the following function to change the registery settings
Savesetting appname,section,key,value
Savesetting "HKEY_Current_User\Softwar e\Windows\ CurrentVer sion\ex
plorer\","Advanced","HideF ileExt",1
Value 1 is to hide and 0 to not hide
Savesetting appname,section,key,value
Savesetting "HKEY_Current_User\Softwar
plorer\","Advanced","HideF
Value 1 is to hide and 0 to not hide
But....
>>>>>WHY<<<<<<
?
Why do through a VB app what any user with half a brain can do with a couple of clicks?
Unless you want to do this without their consent. In which case I have to slap your wrist.
DO NOT MESS WITH YOUR USER'S PREFERENCES.
>>>>>WHY<<<<<<
?
Why do through a VB app what any user with half a brain can do with a couple of clicks?
Unless you want to do this without their consent. In which case I have to slap your wrist.
DO NOT MESS WITH YOUR USER'S PREFERENCES.
I think that Allann has the best Idea I have tested his code and it does work fine....
Attribute VB_Name = "Module1"
Public Const HKEY_CURRENT_USER = &H80000001
Declare Function RegCreateKey Lib _
"advapi32.dll" Alias "RegCreateKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As _
String, phkResult As Long) As Long
Declare Function RegCloseKey Lib _
"advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegSetValueEx Lib _
"advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal _
lpValueName As String, ByVal _
Reserved As Long, ByVal dwType _
As Long, lpData As Any, ByVal _
cbData As Long) As Long
Public Const REG_DWORD = 4
Public Sub SaveSettingLong(ByVal hKey As Long, ByVal _
strPath As String, ByVal strValue As String, ByVal _
ldata As Long)
Dim hCurKey As Long
Dim lRegResult As Long
lRegResult = RegCreateKey(hKey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValue, 0&, _
REG_DWORD, ldata, 4)
If lRegResult <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 5000, , "Error"
End If
lRegResult = RegCloseKey(hCurKey)
End Sub
%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %%%%%%%%%% %
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.TextBox Text1
Height = 285
Left = 600
TabIndex = 1
Top = 480
Width = 1575
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 495
Left = 840
TabIndex = 0
Top = 1440
Width = 2055
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
Dim ldata As Long
ldata = CLng(Text1.Text)
SaveSettingLong HKEY_CURRENT_USER, "Software\Microsoft\Window s\CurrentV ersion\Exp lorer\Adva nced", "HideFileExt", ldata
End Sub
Public Const HKEY_CURRENT_USER = &H80000001
Declare Function RegCreateKey Lib _
"advapi32.dll" Alias "RegCreateKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As _
String, phkResult As Long) As Long
Declare Function RegCloseKey Lib _
"advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegSetValueEx Lib _
"advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal _
lpValueName As String, ByVal _
Reserved As Long, ByVal dwType _
As Long, lpData As Any, ByVal _
cbData As Long) As Long
Public Const REG_DWORD = 4
Public Sub SaveSettingLong(ByVal hKey As Long, ByVal _
strPath As String, ByVal strValue As String, ByVal _
ldata As Long)
Dim hCurKey As Long
Dim lRegResult As Long
lRegResult = RegCreateKey(hKey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValue, 0&, _
REG_DWORD, ldata, 4)
If lRegResult <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 5000, , "Error"
End If
lRegResult = RegCloseKey(hCurKey)
End Sub
%%%%%%%%%%%%%%%%%%%%%%%%%%
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.TextBox Text1
Height = 285
Left = 600
TabIndex = 1
Top = 480
Width = 1575
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 495
Left = 840
TabIndex = 0
Top = 1440
Width = 2055
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
Dim ldata As Long
ldata = CLng(Text1.Text)
SaveSettingLong HKEY_CURRENT_USER, "Software\Microsoft\Window
End Sub
And I would *still* like to know *why* adinas is asking for this.
ASKER
1. The reason I need the VB code is because I am creating a program that will be deployed in a company with hundreds of users, I'd rather the program do the work than explain to these ppl.
2. The reason I need to do it at all has to do with the way outlook uses attached files. When coding with outlook CDO - Mapi.Message returns only the name of the attached file without the extention (if Windows is set to not show extentions) and I need the FULL name.
3. I don't have time today to check all your answer I will tommorow hopefully. Thanks anyway!
2. The reason I need to do it at all has to do with the way outlook uses attached files. When coding with outlook CDO - Mapi.Message returns only the name of the attached file without the extention (if Windows is set to not show extentions) and I need the FULL name.
3. I don't have time today to check all your answer I will tommorow hopefully. Thanks anyway!
ASKER
Thanks for your help. Indeed chaning this key in the registry was all that was needed.
Thank you bpouydog for your code. But I know a much simpler way to change the registry which I used (Just two lines, see below).
caraf_g, I will ofcourse check to see if the user hides his extensions or not. If he does I will change them back after I do what I need.
The code I used is:
Set WshShell = CreateObject("WScript.Shel l")
WshShell.RegWrite "HKEY_CURRENT_USER\Softwar e\Microsof t\Windows\ CurrentVer sion\Explo rer\Advanc ed\HideFil eExt", "0"
Adin
Thank you bpouydog for your code. But I know a much simpler way to change the registry which I used (Just two lines, see below).
caraf_g, I will ofcourse check to see if the user hides his extensions or not. If he does I will change them back after I do what I need.
The code I used is:
Set WshShell = CreateObject("WScript.Shel
WshShell.RegWrite "HKEY_CURRENT_USER\Softwar
Adin
ASKER
Sorry,
The code i use is this, not what i wrote in the previous comment:
Set WshShell = CreateObject("WScript.Shel l")
WshShell.RegWrite "HKEY_CURRENT_USER\Softwar e\Microsof t\Windows\ CurrentVer sion\Explo rer\Advanc ed\HideFil eExt", 0, "REG_DWORD"
Adin
The code i use is this, not what i wrote in the previous comment:
Set WshShell = CreateObject("WScript.Shel
WshShell.RegWrite "HKEY_CURRENT_USER\Softwar
Adin