Barbfrog

Hi! all

I have pissed moaned in the past about software that will not work on vista. And from the way I explained the issues I was told that the programmers should have use the system environment stuff.

Searching the net is about useless cause the keywords are just to common. So here I am with a couple of question about the system environment stuff cause I'm in my play mode with C.

Hard drive folder question

1-> This get the startup folder for the logged on user profile

public string ProfileStartup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

How do I get the startup for all users

the below works but is there a shorter way like the one above,,,

public static string System_AllUsers = Environment.ExpandEnvironmentVariables("%AllUsersProfile%") + "\\Start Menu\\Programs\\Startup";

------------------------------------------------------------------------------------------

I know I can hit up this key too

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

for the Common Startup key that gives

C:\Documents and Settings\All Users\Start Menu\Programs\Startup

But I'm still looking for the non hard wired way!!!!!!

Registry key location Question

2 -> This string is used for both HKCU and HKLM

"Software\\Microsoft\\Windows\\CurrentVersion\\Run";

Is there any environment syntax that will pull this info for the registry key locations....

I am trying to learn how not to hard wiring the stuff.

Thanks any help will be appreciated



Re: Visual C# General Environment allusers startup folder and registry run keys

Peter Ritchie

You'll have to PInvoke SHGetFolderPath to the the All Users Startup folder:

static class NativeMethods

{

public const int CSIDL_COMMON_STARTUP = 0x18;

[DllImport("shfolder.dll", CharSet = CharSet.Auto)]

internal static extern int SHGetFolderPath ( IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath );

}

//...

StringBuilder lpszPath = new StringBuilder(260);

NativeMethods.SHGetFolderPath(IntPtr.Zero, NativeMethods.CSIDL_COMMON_STARTUP, IntPtr.Zero, 0, lpszPath);

string path = lpszPath.ToString();

//...

I'm not sure what your second question is. Are you looking for a way to modify the entries in the Run registry keys without manually modifying them or querying them If so, no there's nothing that wraps those. They're weren't originally designed to be used directly by application programmers, there's usually better ways of the things that the Run registry key does for you.






Re: Visual C# General Environment allusers startup folder and registry run keys

boban.s

SpecialFolder enumeration shows just small set of windows special folders. Security is probably reason for this, but some Microsoft person can better describe the reason.
That force you to use P/Invoke and api function SHGetFolderPath to get to some other special folder paths as Peter has described. Also, if your application scenario is to show a Folder Browser dialog but you want that dialog startup folder is some of these special folders, you can using Reflection to set that root folder to some other one not included in SpecialFolder enumeration:

Type t = folderBrowserDialog.GetType();

FieldInfo fi = t.GetField("rootFolder", BindingFlags.Instance | BindingFlags.NonPublic);

fi.SetValue(folderBrowserDialog, 0x0018); //0x0018 is a value for All Users\Startup folder

folderBrowserDialog.ShowDialog();






Re: Visual C# General Environment allusers startup folder and registry run keys

Barbfrog

Fine example

The keys words you used for question 1 helped me find more info on the internet to work with...

That is what I was after for question one.... That was great for the hard drive folders

Now on to question 2 you said there is no wrapper for the run keys... Bummer,,,,

Does M$ have better way to add my program to the run key when windows starts up...

What I am after is something like what the answer was for question one...

but it will return

string path = HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

I have already hard wired my program to read and write to the Registry run key...

I just need what you called a wrapper to get the key location





Re: Visual C# General Environment allusers startup folder and registry run keys

Peter Ritchie

Barbfrog wrote:

Does M$ have better way to add my program to the run key when windows starts up...

What I am after is something like what the answer was for question one...

but it will return

string path = HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

I have already hard wired my program to read and write to the Registry run key...

I just need what you called a wrapper to get the key location

Nope, it's safe to hard-code that value.

It's better to use the Startup folder because it's more visible to the user. If you need something running all the time, I would suggest a service instead.

Why do you need to add something to the Run key






Re: Visual C# General Environment allusers startup folder and registry run keys

Barbfrog

Why do you need to add something to the Run key

Cause that is where I want it.....

It will be my program only cause most folks would not understand what is doing or how to use it....

And it doesn't need to be a service cause it starts up when windows starts then shuts down when it gets done...

lets put is this way I didn't find any start up delayers on the net that where worth a hill of beans so I'm making my own...

I did finish the program but now it is dress up time......

I made it so it has multi backup buckets,,, as you move stuff around it backups where they came from...

validates the paths.. Looks for duplicate entries.... you can change the default delay times to what you want...

the tray icon has a count down icon of how many are left to start up.. etc etc,,,

LOl if it was not for all the errors traps that you have to included the program would be a 10% of the size it is now..

checking for nulls and empty strings are a killer ..........

I may let my friend in aussie use it... I know he understand the registry....

so anyway if there is no wrapper for the run keys I'll call the cards,,,