PDA

Click to See Complete Forum and Search --> : [Resolved]Help


Visionary
Oct 1st, 2006, 01:54 AM
Hey. I need help on making a program in C# that would edit some of the registry. I have trouble starting it out. Can anyone help me? Like with a layout would be nice. I want upon press of "OK" the code will go and change or edit the registry that I will indicate. Thanks~

jmcilhinney
Oct 1st, 2006, 06:39 AM
You use the Microsoft.Win32.RegistryKey and .Registry classes to edit the registry. They're pretty straightforward. Just read the MSDN help topic for each and their member listings and you'll know all you need to know. It's basically CreateSubKey, OpenSubKey, GetValue and SetValue you'll be using. Once you've read those topics and given it a go you should post back if you have issues. Make sure to use a test system or at least stay away from any existing registry keys while you're testing. Create and edit your own keys only or you might end up breaking an existing application or, worse still, Windows itself.

Visionary
Oct 1st, 2006, 01:15 PM
Yes, I did read the MSDN help topic on it. But I still dont' really get how to start it. Can you give me the layout for something like RegCreateKey ? Like how would I put like HKEY_CURRENT_USER >> SOFTWaRE >> MICROSOFT >> and ect into the code as well?

ComputerJy
Oct 1st, 2006, 01:45 PM
Take a look at this sample:Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true).SetValue("Value name", "Value value");
It allows you to edit or add a start-up program from CurrentUser Registry

Visionary
Oct 1st, 2006, 03:56 PM
Thanks!

Visionary
Oct 1st, 2006, 04:13 PM
I need a little more help. What is the code to restart or shutdown? and Also what is the code to create a # of folders in the desktop. Thanks!

ComputerJy
Oct 1st, 2006, 04:59 PM
System.Diagnostics.Process.Start("shutdown", "-r -t 05");//To rebootSystem.Diagnostics.Process.Start("shutdown", "-s -t 05");//To Shutdown for (int i = 0; i < numberOfFolders; i++)
{
System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirec tory) + i.ToString());
}

Visionary
Oct 1st, 2006, 05:26 PM
The create folder code doesn't seem to work for me. I think it is because I didn't specify that I wish to work when I press "OK". If so can you plz fix? Also is it possible to give the folders a base name? Test1 .. Test2 .. Thanks!

ComputerJy
Oct 1st, 2006, 06:10 PM
for (int i = 0; i < 5; i++)
{
System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Test" + i.ToString());
}
You won't always find people to write the code for you, so someday you have to learn it

jmcilhinney
Oct 1st, 2006, 07:11 PM
How about we keep each thread to a single topic? Filling a thread with multiple unrelated questions makes information more difficult for others searching the forum to find. Also, giving your thread a descriptive title would be a good idea too. Can you imagine if everyone named their threads "Help"? One thread per topic, one topic per thread and descriptive thread titles keeps the forum ordered and easy to use for everyone.

Visionary
Oct 1st, 2006, 07:25 PM
Yea, sorry thanks. I have a few more quesitons but I don't want to annoy you. If its ok, i will post them if u agree. Thanks Though!

ComputerJy
Oct 1st, 2006, 08:54 PM
How about we keep each thread to a single topic? Filling a thread with multiple unrelated questions makes information more difficult for others searching the forum to find. Also, giving your thread a descriptive title would be a good idea too. Can you imagine if everyone named their threads "Help"? One thread per topic, one topic per thread and descriptive thread titles keeps the forum ordered and easy to use for everyone.
You scared the man. :D
Yea, sorry thanks. I have a few more quesitons but I don't want to annoy you. If its ok, i will post them if u agree. Thanks Though!
You can ask as many questions as you want but what jmcilhinney meant to say is that other people search for information in this Forum, so you might want to make your threads useful to others as well

Visionary
Oct 1st, 2006, 09:41 PM
ah, well sorry again. I will keep that in mind now. but one last quesiton.

for (int i = 0; i < 5; i++)
{
System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Test" + i.ToString());
}

As I can see, this works but only once. Say if you press the ok, in my case will create the folders, it will create the foldres right, but wat if I press it again? I want it to be able to keep creating. Thanks~

ComputerJy
Oct 1st, 2006, 09:46 PM
ah, well sorry again. I will keep that in mind now. but one last quesiton.

for (int i = 0; i < 5; i++)
{
System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Test" + i.ToString());
}

As I can see, this works but only once. Say if you press the ok, in my case will create the folders, it will create the foldres right, but wat if I press it again? I want it to be able to keep creating. Thanks~
It's because you can't create the folder with the same name twice. This is what will happen. The program will try to create folder named Test0-4, and if they already exist, then the mission is done.
If you want it to create 5 new folders every time, you only have to make some simple changes.

1- Declare an "int" variable as a class attribute (if you don't know what that is read about Object Oriented programming)
2- instead of [i.ToString()] use ([(i+ the variable from above).ToString()]

jmcilhinney
Oct 1st, 2006, 10:26 PM
Yea, sorry thanks. I have a few more quesitons but I don't want to annoy you. If its ok, i will post them if u agree. Thanks Though!Whether I'm annoyed need not be anyone's consideration. If I see someone using the forums incorrectly (as I perceive it) then I'll point it out. You can ignore my advice if you want and there'll be no repercussions, but if everyone makes an effort to use the forums as sensibly as possible then using them becomes as smooth as possible for everyone, yourself included. Let's say that you want to revisit this thread at some point in the future. With a title like "Help" finding it by subject matter will be all but impossible. If you address each separate topic in a separate thread with a title that describes the topic then finding it later, for you and everyone else, becomes simple. I point out these "mistakes" because I know that noone bothers to read the forum guidelines and if someone doesn't tell you you're doing the wrong thing then how are you to know? I am merely a crusader for sensible forum use for everyone's benefit. ;)

Visionary
Oct 1st, 2006, 11:50 PM
For the:
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true).SetValue("Value name", "Value value");

How would I set it as a Dword Value instead? At the moment its just a regular value.

jmcilhinney
Oct 2nd, 2006, 01:52 AM
Take notice of what Intellisense tells you. Reading the help documentation can also give you this sort of information very quickly.

Visionary
Oct 2nd, 2006, 09:09 PM
Yes, but I dont get the "valuekind" part.. this is wat a I did..

Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer", true).SetValue("StartMenuLogoff", "1", "Microsoft.Win32.RegistryValueKind DWORD");

jmcilhinney
Oct 2nd, 2006, 09:45 PM
Of course you do. As soon as you type the second comma Intellisense knows that the first overload with two arguments is not appropriate so it shows you the signature and provides help fo the second overload. Also, when Intellisense shows the first tooltip it specifically states that there are more overloads, as I indicated. All you have to do is press the down arrow key or click the little arrows on the tooltip and it will show you the other signatures. Delete everything after "SetValue", i.e. from the last opening parenthesis onwards, and start typing. Watch what Intellisense provides as you type. When you type the second comma the appropriate method signature will be displayed. If you then type a space Intellisense will provide a popup menu of types with the appropriate type selected. Press the Tab key to select that type and then enter a dot and you'll be presented with another popup menu of all the possible values of that type. Intellisense starts with "Intelli" for a reason. It will try to do half the work for you if you only let it.

Note that in the C# IDE, once you've left that line Intellisense will not provide help for the method your entering unless you delete the entire argument list, as I suggested above. This is why typing yourself is preferable to copy and paste. Once you've pasted something you can't get Intellisense again without deleting all the method arguments.

Visionary
Oct 2nd, 2006, 10:41 PM
Yes, I know but for ValueType I don't know what do put there. I mean I know valuetype has to be there but what is the C# code for "DWORD" in the registry. You can set new values in registry right: Binary, Dword, and String values. Whats the C# code for Dword value?

ComputerJy
Oct 2nd, 2006, 11:25 PM
How about Microsoft.Win32.RegistryValueKind.DWord for DWord :confused:

Visionary
Oct 3rd, 2006, 08:26 PM
Oh!! Well when it said "RegistryValueKind ValueKind" I thought there was a space between the ValueKind and the value... thanks though +++ in your rep from me.