Solved

Getting Deskstop Path

Posted on 2003-11-04
9
397 Views
Last Modified: 2008-02-26
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
Comment
Question by:zhongxi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 52

Accepted Solution

by:
Ryan Chong earned 150 total points
ID: 9684256
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
 
LVL 9

Assisted Solution

by:bhagyesht
bhagyesht earned 150 total points
ID: 9684276
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9684449
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9684624
ryancys mentioned the same in the VB code.
Rosh :)
0
 
LVL 1

Expert Comment

by:vkaushik
ID: 9684639
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
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9684655
vkaushik: can you please tell me how is your answer different from mine?
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question