Solved

How do I change the 'Desktop' directory programmaticly

Posted on 1998-02-14
35
295 Views
Last Modified: 2010-04-04
I want to change the default directory of the 'Desktop' (normally C:\windows\desktop) to another directory, so that the desktop will display all files in this new directory on the desktop instead. I want to do this WITHOUT rebooting.
I know how to modify the registry (if this is neccecary).

Please Help,

  Dirk Manders

   
0
Comment
Question by:dms@
  • 11
  • 9
  • 7
  • +3
35 Comments
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Hi dms@,

 I don't know, but have you already tried to change this directory in the registry? Maybe you can try, but I suggest you first make a backup of your registry, before doing it. Besides I think this is what windows does when you've multiple users on one PC.

You can find this directory under :

HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\WINDOWS\SHELL FOLDERS\DESKTOP

or for multiple users :

HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\WINDOWS\USER SHELL FOLDERS\DESKTOP

Good luck and let me know something.

I'll try to find another solution.
c.u. Zif.
0
 
LVL 4

Expert Comment

by:itamar
Comment Utility
Hi Zif,

I tried it, but nothing happened. It looks like there is something missing to make desktop "refresh". Reboot could be the answer but dms@ said (and I guess why) he doesn't want to force a  restart.
Any ideas ?

Bye
Itamar
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Thinking....
0
 
LVL 2

Expert Comment

by:Waldek
Comment Utility
It is a procedure ResetDisplay in win32.hlp but I can't find it in user32 lib
0
 

Author Comment

by:dms@
Comment Utility
Yeah, that was my problem in the first place. I already figured out the registry modifying thing. But there is something else: After changing the registry entry you mensioned and restarting Windows (wich eventually is NOT the idea - but anyhow), the registry entry is set back to its original value ! How can this be ? Maybe because Windows saves it's current desktop settings when shutting down ? Any suggestions ?

0
 
LVL 4

Expert Comment

by:itamar
Comment Utility
Hi all,

I got the same results of you dms@ ! I think it's due to another entry of registry that is:
HKEY_USERS\.Default\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

Note the .Default in the patch. I have a piece of code to change wallpaper bitmap that rids of the set back, but I couldn't find smth similar to desktop directory. This is  the code:

//change the Wallpaper key in the registry, after that: SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, nil, SPIF_SENDWININCHANGE);

where SPIF_SENDWININCHANGE makes the new setting persistent. I think that changing desktop directory need smth like that.

Bye (still thinking...)
Itamar

0
 

Author Comment

by:dms@
Comment Utility
Yeah, you're right: it does need something like that, but the question remains 'WHAT ???'.
BTW: a little of subject, yet related: Does anyone know where Windows stores the postion of the icons on the Desktop ? It's probable somewhere in the registry, but where ?

-- still hoping someone can find a problem to my initial    problem....
0
 

Author Comment

by:dms@
Comment Utility
erhm, find a SOLUTION that is :-)
0
 
LVL 4

Expert Comment

by:itamar
Comment Utility
Give us some time to search for a complete answer. My intention in the previous comment is to raise a proccess called "brain storm". Very usefull to achieve solutions.

P.S.: What did you mean with "erhm, find a SOLUTION that is :-)"
0
 

Author Comment

by:dms@
Comment Utility
Ok.

(that last comment was because I said: '..find a problem to my problem', while I meant to say '..find a solution to my problem'.
 Anyhow....)
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Hi all, sorry but I've had some work to do in the field. Now, on the really problem, haven't had luck yet. But I know where you can find the icons on the desktop, here is some code. It's quit easy :

procedure TForm1.Button1Click(Sender: TObject);
var
PIDL: Pointer;
Path: LPSTR;
const
CSIDL_RECENT = $0008;
begin
Path := StrAlloc(MAX_PATH);
SHGetSpecialFolderLocation(Handle, CSIDL_DESKTOP, @PIDL);
if SHGetPathFromIDList(PIDL, Path) then // returns false if folder isn't
part of file system
  begin
  OpenDialog1.InitialDir := Path;
  OpenDialog1.Execute;
  end;
StrDispose(Path);
end;

CSIDL_DESKTOP            = $0000;
  CSIDL_PROGRAMS           = $0002;
  CSIDL_CONTROLS           = $0003;
  CSIDL_PRINTERS           = $0004;
  CSIDL_PERSONAL           = $0005;
  CSIDL_STARTUP            = $0007;
  CSIDL_RECENT             = $0008;
  CSIDL_SENDTO             = $0009;
  CSIDL_BITBUCKET          = $000a;
  CSIDL_STARTMENU          = $000b;
  CSIDL_DESKTOPDIRECTORY   = $0010;
  CSIDL_DRIVES             = $0011;  // My Computer
  CSIDL_NETWORK            = $0012;
  CSIDL_NETHOOD            = $0013;
  CSIDL_FONTS              = $0014;
  CSIDL_TEMPLATES          = $0015;
0
 

Author Comment

by:dms@
Comment Utility
Yeah, that gets me the Icons alright, but not the actual POSITION of the icons on the desktop (from the top and left of the screen, I didn't mean the position on the HDD).

An answer to this problem would be OK, But I really hope that someone can find the answer to my initial problem.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Sorry, see that I didn't read your comment correctly.
0
 
LVL 4

Expert Comment

by:itamar
Comment Utility
Hi Zif / dms,

I sugest you to reject Zif's answer otherwise it will be auto-graded, dms will loose the points and other experts cannot help. I'm sure Zif will not care, right ?

Still working on it,
Itamar
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
No, really no problem if you reject my answer.
0
 

Author Comment

by:dms@
Comment Utility
It wasn't THE answer, so I have no choice but to reject (sorry ZifNab !)

As a sidenote:
 I've found an aplication that can do what I want to be able to do: Norton Navigator. This  program alows one to create multiple desktops and switch from one to another. I recon  that is also changes the desktop directory, because copying desktop items each time a  'new desktop' is selected would take to much time (I've tried it).

Still the question remains.... HOW ???
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Yo all!

I found something interesting.... Maybe this can help.... Didn't looked further but, it can be handy...

Getting the Win95 Desktop ListView Handle

                          Question:

                          How do I get a handle to the Windows 95 Desktop to access the
                          Desktop icons?

                          Answer:

                          The Windows 95 Desktop is overlayed with a ListView component.
                          You simply need to get a handle to the ListView. Example:

                           function GetDesktopListViewHandle: THandle;
                           var
                             S: String;
                           begin
                           Result := FindWindow('ProgMan', nil);
                           Result := GetWindow(Result, GW_CHILD);
                           Result := GetWindow(Result, GW_CHILD);
                           SetLength(S, 40);
                           GetClassName(Result, PChar(S), 39);
                           if PChar(S) <> 'SysListView32' then Result := 0;
                           end;

                          Once you have the handle, you can use the list view-related API
                          functions in the CommCtrl unit to manipulate the desktop. See
                          the LVM_xxxx messages in the Win32 online help.

                          For example the following line of code:

                          SendMessage(GetDesktopListViewHandle,LVM_ALIGN,LVA_ALIGNLEFT,0);

                          will align the desktop icons to the left side of the Windows 95 desktop.

I have the strange feeling you can get all your needs with the LVM's.....
What about :

 1. Copie the list
 2. Remove all items
 3. Take another list and copy all to the list (Handle desktop?)
 4. Update the list!!!!!
 5. We got the result! I hope.

 Maybe easier to just change the handle...

As I meant to say, .... this must be the SOLUTION to our problem. If so, let me know,...

Regards, Zif.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 3

Expert Comment

by:KE
Comment Utility
Sorry for interrupting your discussion, why not just move the files in and out of the desktop folder ?
It's not the "perfect" solution, but it's quite fast, as it relatively takes no time for the system to move even huge files and folders wich contains lot's of files.
Well, if you have a few thousand's it might (but i'd like to see a screen-shot of that deskop ;-)
Keep up your good work, this is just a work around !!!
0
 
LVL 3

Expert Comment

by:KE
Comment Utility
Sorry, i forgot to mention that the workaround will only be fast if the individual folders(desktops) are placed on the same drive.

0
 

Author Comment

by:dms@
Comment Utility
KE: This sounds like a simple solution, but it isn't what I want. I want to be able to do it the other way around (I mensioned before that I already tried the thing you mensioned).

ZIF: This looks VERY promising ! I'll try it as soon as possible and will let you know. Man, if this works then you REALLY deserve those 595 points !
0
 
LVL 3

Expert Comment

by:KE
Comment Utility
Sorry, but I read that you was copying the files into the desktop, and not moving them which is significantly faster - have you tried to move them instead ?

0
 

Author Comment

by:dms@
Comment Utility
No, I haven't tried that yet. I'm know that your suggestion works, but it is not what I want - I really want to change the default directory that Windows uses to display the desktop icons. I'm sure it must be possible.

ZIF: I've tried your idea, and it works - but: It still doesn't actually change the default Desktop directory, it only moves files of another directory to them - wich is NOT the idea.
There MUST be some way to make Windows re-load the icons in the default desktop directory (wich I want to change), without rebooting.
0
 
LVL 4

Expert Comment

by:itamar
Comment Utility
Hi all,

I think there is a serious problem with moving files to desktop directory. What if the system goes down before moving the original files back ? Changing the desktop directory will be more confident.

Don't give up dms. People here are working on it...

Bye,
Itamar
0
 
LVL 3

Expert Comment

by:KE
Comment Utility
I see your point itamar, but you can always get some serious damage when a system crashes, no matter what you're doing (except for doing nothing) :-).

dms: I think that the problem could be solved if you can figure oyt, how the explorer can be restarted. I do not suggest a killtask, there has to be another way - especially on Win95. On NT this task would be straightforward, by stopping and starting the exploerer process, in between you change the registry.
You can "simulate" it on 95 by pressing CTRL-ALT-DELETE and END the explorer task - and select Cancel on the Logoff dialog - wait a few seconds for the task message to appear, and
select end task. This will reload the explorer.
To select the desktop folder, create a registry string in
HKEY_USERS\.Default\Software\Microsoft\Windows\CurrentVersion\Exploerer\User Shell Folders\ with the name Desktop and the value <APath>.
I don't know how you get rid of the logoff message in 95, and neither do i know who's responsible for loading the explorer.
Maybee you can trick the system by making an inplace explorer.exe (or point to a new shell), that is responsible for starting and stopping the original explorer. It's a long time since I moved to NT, so you are on your own here.
I've looked for a function that could reload the explorer while it's running, and there was a promissing one, but it's not implemented yet - maybee in Windows 98. Maybee you can figure out how Norton are reloading the explorer - they could send a Windows Message ore something similar.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Hi dms,

So moving them works. But you want to change the desktop, well instead of changing the desktop change the files in the desktop and always work with the desktop directory. The other 'desktop directorys' are stored in your own defined directorys. Then if you want to change your desktop, just move the files between these directories and that 's it! You've changed the desktop!
Have you tried just changing the handle of the desktop directory to another directory?

Regards, Zif.
0
 

Author Comment

by:dms@
Comment Utility
Wich Handle ? I don't think there is a handle to the actual desktop directory. I think Windows just fills the Desktop 'Listview' with all the items in C:\Windows\Desktop at startup.

So there are 2 solutions:
1) Do what KE said. First changing the Desktop dir in the registry and then restarting     Explorer. // This idea should work but restarting Explorer would take up too much time.
2) Do what ZIF suggested. Simulating what Windows does on startup: loading all files     from a directory and storing them in the Desktop Listview. // Better idea.

Either way there is still one major problem: If I add/remove icons, they will automaticly be aligned, how can I get the original positions from the current desktop icons. I recon there positions must be on stored on disk, 'cause Windows must also know these positions when putting the icons on the desktop on startup.


0
 
LVL 3

Expert Comment

by:KE
Comment Utility
dms@: I disagree on your judgement.of the best solution, however that's up to you which one you decide.
1. If you change the desktop directory in registry, and restart the explorer the icon positions should be retained.
2. The "simulated" restart, as i descibed it, of the Win95 explorer isn't the true restart time - there's a significant delay, because of the OS is waiting for the task to respond.
Try this one instead:
Modify the registry setting as descibed earlier.
Select shutdown from your startmenu. Hold down CTRL-ALT-SHIFT and select NO. Now your explorer is unloaded. Start it again by dbl-click on the blank desktop, and choose run from the taskmanager, select C:\windows\explorer and run it. If you can do these few steps programatically, it's should be fast enough - and besides it WILL retain the icon positions etc., so you don't have to fiddle around with the ListViews etc. - which will take a serious amount of time to implement.
So the only thing that's left to solve, is how to shutdown the explorer without a logoff dialog.

0
 

Author Comment

by:dms@
Comment Utility
I Will give it a try, and I'll let you know

0
 
LVL 4

Accepted Solution

by:
jeurk earned 590 total points
Comment Utility
Hi,
That's my proposed answer, I tryed to well comment the code
So everything should be in there, I tryed it and it does what
you asked (I think)
You have to take care of two things:
-You have to wait a moment before restarting explorer, cause
the old instance is not completly removed, maybe with:
while FindWindow(pchar('Progman'), nil)
end;

-When you restart explorer it launches again the programs in
run, but look at what I say in the code.

-----------------
Make a new app
drop a button on the form
here the buttonclick code follows:
-----------------

procedure TForm1.Button1Click(Sender: TObject);
var
  h: HWND; //the explorer handle
  r: TRegIniFile; //the registry key
  p: string; //the new desktop path
begin
  p := 'c:\windows\temp';
  //we ask the new path we want to use for the desktop path
  if inputQuery('Let''s change the desktop path !!', 'Please enter the new desktop path', p) then
  begin
    //first we change the right registry key that designate the current user
    //desktop path
     //the root is HKEY_CURRENT_USER by default
    r := TRegIniFile.Create('Software\Microsoft\Windows\CurrentVersion\Explorer');
     //if it's the same, nothing is changed
    if p <> 'c:\windows\desktop' then
      r.WriteString('User Shell Folders', 'desktop', p)
    else
      r.DeleteKey('User Shell Folders', 'desktop');
    r.Free;

    //then we kill the explorer, and launch him again for the
    //changes to take effect
    //I tryed all sort of WM_systemChange and WM_UserChanged to ask
    //the explorer to reffresh things, but it did not work
    //
    //I tryed to do the things with winHack 95 v2 but the default
    //desktop change was restored to the normal state after reboot
   
     //here we are looking for the exlporer handle
    h := FindWindow(pchar('Progman'), nil);
     //we are asking explorer to close itSelf
    PostMessage(h, WM_QUIT, 0, 0);
     //we are launching the explorer back again
     //but first we shall wait a second, cause if we do it too fast
     //the old instance is not completly removed, we the new comes up
    sleep(10);
    WinExec('Explorer.exe', SW_SHOW);
    //one side effect is that explorer launches again all the
    //apps that are registered in the run section
    //but either you know a explorer param that avoids that
    //or you read the run section and delete the keys before
    //killing explorer, and after that you restore them
  end;
end;


I hope this does what you want.
Jeurk
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
The position of the Item can be found by LVM_GETITEMPOSITION.

LVM_GETITEMPOSITION
wParam = (WPARAM) (int) i;
lParam = (LPARAM) (POINT FAR *) ppt;
 
// Corresponding macro
BOOL ListView_GetItemPosition(HWND hwnd, int i, POINT FAR* ppt);
 

The LVM_GETITEMPOSITION message retrieves the position of a list view item. You can send this message explicitly or by using the ListView_GetItemPosition macro.

Parameters

hwnd

Handle to the list view control.

i

Index of the list view item.

ppt

Pointer to a POINT structure that receives the position of the item's upper-left corner, in view coordinates.

Return Value

Returns TRUE if successful or FALSE otherwise.


Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
Yo dms@,

Already looked at these functions? (Pitty, only for NT) So you only can use these for NT applications...

CreateDesktop, GetThreadDesktop, OpenDesktop, SetProcessWindowStation

Regards, Zif!
0
 

Author Comment

by:dms@
Comment Utility
Please hang on folks, I'll try these solutions at the end of the week (I'm kinda busy with school at the moment). I will let you know.....
0
 
LVL 3

Expert Comment

by:KE
Comment Utility
Seems like you got the thing to work jeurk. I expect that the explorer will not send a WM_EndSession to any of the running task's, when it's closed by the WM_Quit.
If it actually works, I think that dms@ is better of with this solution, rather than "hand-coding" everything on his own.
No offence zifnab.
Now the last part would be to figure out when the explorer is actually unloaded - i don't think that sleeps is a secure way of doing it. BTW. you code states sleep(10) and you comment that it's a second. I were sure that sleep was specified in ms.
Whichever, it's better to do the job safely, by querying the taskmanager or smth. like that.
0
 
LVL 4

Expert Comment

by:jeurk
Comment Utility
I don't think that explorer send's a WM_EndSession, even if I have not
checked it out. I tryed the prog I wrote and the prog's like delphi and others don't asked for saving and were still here
after the explorer's rebirth.
About the way to figure out when the explorer is unloaded, what's about:
while FindWindow(pchar('Progman'), nil)
end;
?
You are right by saying that sleep(10) is not a second and that
it's specified in ms.
0
 
LVL 3

Expert Comment

by:KE
Comment Utility
If your applications remains open, the WM_EndSession is not send; so it should work safely.
I don't know if FindWindows( etc. ) is a secure way.
It's not for sure which order the OS objects is destroyed, so maybee the function will trigger while the explorer is shut down (which in some cases could take longer, due to errors, and hanging tasks, printers etc.).
I think the best way would be to check if the explorer process is running.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now