Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

Getting Deskstop Path

I am trying to copy a file from one location to the desktop. But I am facing problem to get the path of desktop since the path of desktop is different from each other.
This Window function is used.
(CopyFile ("File.txt", "Special\\NewFile.txt", FALSE))

Can anyone help?
thanx
0
zhongxi
Asked:
zhongxi
2 Solutions
 
Ryan ChongCommented:
In vb, try:

Public Type SHITEMID
    cb As Long
    abID As Byte
End Type
Public Type ITEMIDLIST
    mkid As SHITEMID
End Type

Public Const NOERROR = 0

'Public Const ERROR_SUCCESS As Long = 0
'Public Const MAX_PATH As Long = 260
Public Const CSIDL_DESKTOP = &H0                   '(desktop)
Public Const CSIDL_INTERNET = &H1                  'Internet Explorer (icon on desktop)
Public Const CSIDL_PROGRAMS = &H2                  'Start Menu\Programs
Public Const CSIDL_CONTROLS = &H3                  'My Computer\Control Panel
Public Const CSIDL_PRINTERS = &H4                  'My Computer\Printers
Public Const CSIDL_PERSONAL = &H5                  'My Documents
Public Const CSIDL_FAVORITES = &H6                 '(user)\Favourites
Public Const CSIDL_STARTUP = &H7                   'Start Menu\Programs\Startup
Public Const CSIDL_RECENT = &H8                    '(user)\Recent
Public Const CSIDL_SENDTO = &H9                    '(user)\SendTo
Public Const CSIDL_BITBUCKET = &HA                 '(desktop)\Recycle Bin
Public Const CSIDL_STARTMENU = &HB                 '(user)\Start Menu
Public Const CSIDL_DESKTOPDIRECTORY = &H10         '(user)\Desktop
Public Const CSIDL_DRIVES = &H11                   'My Computer
Public Const CSIDL_NETWORK = &H12                  'Network Neighbourhood
Public Const CSIDL_NETHOOD = &H13                  '(user)\nethood
Public Const CSIDL_FONTS = &H14                    'windows\fonts
Public Const CSIDL_TEMPLATES = &H15
Public Const CSIDL_COMMON_STARTMENU = &H16         '(all users)\Start Menu
Public Const CSIDL_COMMON_PROGRAMS = &H17          '(all users)\Programs
Public Const CSIDL_COMMON_STARTUP = &H18           '(all users)\Startup
Public Const CSIDL_COMMON_DESKTOPDIRECTORY = &H19  '(all users)\Desktop
Public Const CSIDL_APPDATA = &H1A                  '(user)\Application Data
Public Const CSIDL_PRINTHOOD = &H1B                '(user)\PrintHood
Public Const CSIDL_LOCAL_APPDATA = &H1C            '(user)\Local Settings
                                                   '\Application Data (non roaming)
Public Const CSIDL_ALTSTARTUP = &H1D               'non localized startup
Public Const CSIDL_COMMON_ALTSTARTUP = &H1E        'non localized common startup
Public Const CSIDL_COMMON_FAVORITES = &H1F
Public Const CSIDL_INTERNET_CACHE = &H20
Public Const CSIDL_COOKIES = &H21
Public Const CSIDL_HISTORY = &H22
Public Const CSIDL_COMMON_APPDATA = &H23           '(all users)\Application Data
Public Const CSIDL_WINDOWS = &H24                  'GetWindowsDirectory()
Public Const CSIDL_SYSTEM = &H25                   'GetSystemDirectory()
Public Const CSIDL_PROGRAM_FILES = &H26            'C:\Program Files
Public Const CSIDL_MYPICTURES = &H27               'C:\Program Files\My Pictures
Public Const CSIDL_PROFILE = &H28                  'USERPROFILE
Public Const CSIDL_PROGRAM_FILES_COMMON = &H2B     'C:\Program Files\Common
Public Const CSIDL_COMMON_TEMPLATES = &H2D         '(all users)\Templates
Public Const CSIDL_COMMON_DOCUMENTS = &H2E         '(all users)\Documents
Public Const CSIDL_COMMON_ADMINTOOLS = &H2F        '(all users)\Start Menu\Programs
                                                   '\Administrative Tools
Public Const CSIDL_ADMINTOOLS = &H30               '(user)\Start Menu\Programs
                                                   '\Administrative Tools
Public Const CSIDL_FLAG_CREATE = &H8000&           'combine with CSIDL_ value to force
                                                   'create on SHGetSpecialFolderLocation()
Public Const CSIDL_FLAG_DONT_VERIFY = &H4000       'combine with CSIDL_ value to force
                                                   'create on SHGetSpecialFolderLocation()
Public Const CSIDL_FLAG_MASK = &HFF00              'mask for all possible flag values

'not used
'Public Const CSIDL_SYSTEMX86 = &H29               'x86 system directory on RISC
'Public Const CSIDL_PROGRAM_FILESX86 = &H2A        'x86 C:\Program Files on RISC
'Public Const CSIDL_PROGRAM_FILES_COMMONX86 = &H2C 'x86 Program Files\Common on RISC

'public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
Public Declare Function SHGetSpecialFolderLocationNew Lib "shell32.dll" Alias "SHGetSpecialFolderLocation" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
'Public Declare Sub CoTaskMemFree Lib "ole32" (ByVal pv As Long)

Public Function GetSpecialfolder(CSIDL As Long) As String
    Dim r As Long, path As String
    Dim IDL As Long
    'Get the special folder
    r = SHGetSpecialFolderLocationNew(100, CSIDL, IDL)
    If r = NOERROR Then
        'Create a buffer
        path$ = Space$(512)
        'Get the path from the IDList
        '<Note>: Opposite function of SHSimpleIDListFromPath
        r = SHGetPathFromIDList(ByVal IDL, ByVal path$)
        'Remove the unnecessary chr$(0)'s
        GetSpecialfolder = Left$(path, InStr(path, Chr$(0)) - 1)
        Exit Function
    End If
    GetSpecialfolder = ""
End Function

Use like:
GetSpecialfolder(CSIDL_DESKTOP)

Hope this helps
0
 
bhagyeshtCommented:
the following code does it it uses windows scripting to achive the task you can add it in vb directly.



Dim myWSh
Set myWSh = CreateObject("WScript.Shell")
MsgBox myWSh.SpecialFolders.Item("Desktop")
Set myWSh = Nothing


other special folders are

Folder Windows version
AllUsersDesktop Windows 2000
AllUsersStartMenu Windows 2000
AllUsersPrograms Windows 2000
AllUsersStartup Windows 2000
Desktop Windows 98/ME, Windows 2000
Favorites Windows 98/ME, Windows 2000
Fonts Windows 98/ME, Windows 2000
My Documents Windows 98/ME, Windows 2000
NetHood Windows 98/ME, Windows 2000
PrintHood Windows 98/ME, Windows 2000
Programs Windows 98/ME, Windows 2000
Recent Windows 98/ME, Windows 2000
SendTo Windows 98/ME, Windows 2000
Start Menu Windows 98/ME, Windows 2000
StartupB Windows 2000
Templates Windows 2000
0
 
Roshan DavisCommented:
LPITEMIDLIST pidl;
char buf[520];

SHGetSpecialFolderLocation(hwnd, CSIDL_DESKTOP, &pidl);
SHGetPathFromIDList(pidl, buf);

MultiByteToWideChar(CP_ACP, 0, buf, -1, wsz, MAX_PATH);

Good luck
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Roshan DavisCommented:
ryancys mentioned the same in the VB code.
Rosh :)
0
 
vkaushikCommented:
Use this

Dim WSHShell as Object
dim DesktopPath as string

  Set WSHShell = CreateObject("WScript.Shell")
  if (not WSHShell is nothing) then
      DesktopPath = WSHShell.SpecialFolders("Desktop")
  end if

You can retrieve other windows folders also

AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates
0
 
bhagyeshtCommented:
vkaushik: can you please tell me how is your answer different from mine?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now