Saving contents of a listBox to program settings C# - c#

So what I am trying to do is save the contents from a listBox to the application Properties.Settings.Default
I have no idea where to start, nor do I know if it is even possible. Thanks in advance.

any settings you have setup in Properties-> settings tab should show up like
[your namespace].Properties.Settings.Default.yoursetting = "change";
after you edit your properties always call
[your namespace].Properties.Settings.Default.Save();
the save part got me at first.
for list types use :
it will help if your objects can be converted to and from strings anything more not really sure for anything more complex but I hope this gets you started.
foreach(string s in listbox.Items){[settingscode].add(s);}
something like that anyways.

Related

ListBox sorting by files creation time

My listBox1 is filled with names of txt files. It is sorting them automatically alphabetically. But I need to sort these files by creation time, from the newest to the oldest. Can someone help me?
you are not giving us much to go on :P
But I am going to give it my best shot to help you. Because I do not know how you have populated the List (by hand, or with code) I cant really guide you there.
But lets say you did it by code. The following snippet I typed might help you find what you need
//array of file names (these need to be paths to files, so if you do not prefix it with ./path/ or anything, then the executable needs to be in the same folder)
//If you are debugging, make sure the paths are correct if you expect them to be in the same folder, it should then be in the debug folder.
//If you included some test files in the solution and want them copied to your debug folder. Click on them, go to the properties pane, and set build action to content, and one of the other properties should read copy of newer instead of never copy
var fileNames = new string[]{"1.txt", "2.txt", "3.txt"};
var fileInfos = fileNames.Select(f => new FileInfo(f));
var orderedFileInfos = fileInfos.OrderBy(f => f.CreationTime);
orderedFileInfos is now a list of type FileInfo ordered by ascending on their creationtime.
This is what you can use to populate your ListBox
I hope i helped you progress in your project.

Set a permanent value of a variable without using a database

I don't know how to describe it thoroughly in the title, but I need to set a permanent value of a variable/flag once a process has return true and maybe set some flag in the program itself the value rather than saving it to database. And once that variable/flag has already have that value then the program won't run the process again and just use the value. Is it possible? I'm using VB.Net. I can't use the database because database can be overridden and change values by using query. Thanks in advance!
You can simply use binary/XML serialization in a file to save the state of that variable through your program. Every time you restart your app you can access the value from that file to get its current state.
You can look at this example - http://www.centerspace.net/examples/nmath/csharp/core/binary-serialization-example.php
Basically, you will not save the value in the database but in a file. Anyways you need to persist the value somewhere.
Some ways below
You did not specify if you are afraid that your application or another one could change the value
How I would do it
My ideas below
1)You could use an xml file for example and zip a copy of it with a strong password. Every time you update the first xml you will update also the encrypted zipped xml.You can use a FileSystemWatcher and capture any file change, so if something/someone has changed the file you just get a new copy from the zip
2)You can store the value in the DB and add a trigger to prevent delete/update
for example
-- delete trigger
CREATE TRIGGER Function_Value_Deleted
ON [dbo].[FunctionsValueTb]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (
SELECT [Flag] FROM deleted
)
BEGIN
ROLLBACK;
RAISERROR ('Record deletion is not allowed...', 16, 1);
END
END
*You can use also use THROW rather than RAISERROR
**Do the same for the insert and update actions
***You can also store the value into a log table or send an email
I found myself in a situation quite similar to yours a couple of days ago.
In the end, I decided to use the settings functionaly provided by .NET: it is easy to use and maintain, and so far it has given me good results.
Yo can see here what I am talking about:
Best practice to save application settings in a Windows Forms Application
That thread refers to C# but is easily applicable for VB.NET: I just had to follow the same steps in order to add the Settings file:
Right click on the project in Solution Explorer, choose Properties.
Select the Settings tab, click on the hyperlink if settings doesn't
exist. Use the Settings tab to create application settings. Visual
Studio creates the files Settings.settings and
Settings.Designer.settings that contain the singleton class Settings
inherited from ApplicationSettingsBase
And then, from my code, I use the settings like this:
Dim lastExecDate As Date = My.Settings.LastSuccessfulExecution
lastExecDate = lastExecDate.AddDays(1)
// Perform my next execution and do other stuff
My.Settings.LastSuccessfulExecution = lastExecDate
My.Settings.Save()
Next time you retrieve the parameter LastSuccessfulExecution, it will have the updated value.
One more remark, as stated in the post that I linked above:
Note that you need to set the scope property of your settings. If you
select Application scope then Settings.Default.< your property > will
be read-only
Finally, I see that you are using this to store the expiration date of a product, so you don't want the user messing around with it. According to this post, the actual values of the parameters are stored in an Application Data user folder. It is somehow obfuscated since it is not that easy to find and besides it contains a hash on its name... I don't know if that is well hidden enough for you.
If you want the value only to exist in memory when the application is running then you can use the main thread of the application and use:
int slotData = randomGenerator.Next(1, 200);
//to set the data
Thread.SetData(Thread.GetNamedDataSlot("SomeDataKey"), slotData);
//to get the data
int newSlotData = (int)Thread.GetData(Thread.GetNamedDataSlot("SomeDataKey"));
Or you can use the Windows Registry if your app only runs on Windows, if not then you would have to write the value/object to a file and read it from there.

Saving Data On C# Wpf

How can I save my added buttons when closing the program ? Each time I hit an add button I'm creating a new title which type is button.And each title have it's own specific words.So I must save the words in buttons at the same time.
Find out what data needs to be stored. > Define a Model
Define how the Data will be stored > e.g. XML-Serialisation, JSON or whatever
Define where Data will be stored, e.g. Database, File
Define when the Configuration is read and saved
eg: read on startup, save on shutdown
Handle your Configuration Data to generate your buttons
Its all up to you. :)
An easy solution might be using XmlSerializer, which is documented here.

TreeListEx, field set using template _StandardValues not being retrieved from sitecore?

I'm having some serious issues with setting an items values be _StandardValues in sitecore. I have a sitecore item and I retrieve it's data simply thus:
Item rawItem = service.Database.GetItem(new ID(id));
I subsequently extended said item's template and added a new TreeListEx. I populated this treelist from standard values:
The problem is the standard values are not getting retrieved by the above code. The field does not exist in the fields collection. What is confusing is if I change the items values, so remove all the standard items and put them back in, it works. The only thing in the UI appears different is that the field goes from:
to
i.e. it's not pulling this data from [standard values] anymore. I can now see my fields in rawItem (above).
If I check the Raw values of the they are identical (i.e. a collection of Guids as you'd expect):
{4E4A364E-16D5-4E09-A4E7-25DB628951FB}|{15E4026A-575A-4787-83B0-A37EB9F0A06D}|{74D1C654-BE53-4FB4-A072-19DA70215F4B}|{C3883CDE-A01E-46B7-B09F-2FB1F4C51C3A}
Has anyone else experienced this issue? Am I missing something?
I think I got a solution, it just came back to life. It appears that I needed to rebuild the link DB. I had tried this previously to no avail, but the difference this time is I rebuilt all 3 (core, web and master) DBs:
Hopefully this will help someone with this issue in the future.

Trying to create a startup manager using .net 2.0

OK so I'm working on a integrated start-up manager with check boxes, so far this is what i have, it does this on form load:
RegistryKey hklm = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
foreach (string Programs in hklm.GetValueNames())
{
string GetValue = hklm.GetValue(Programs).ToString();
ListViewItem item1 = listView1.Items.Add(Programs);
item1.SubItems.Add(hklm.Name.ToString().Replace("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion", "HKLM"));
item1.SubItems.Add(GetValue);
}
RegistryKey hkcu = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
foreach (string Programs in hkcu.GetValueNames())
{
string GetValue = hkcu.GetValue(Programs).ToString();
ListViewItem item2 = listView1.Items.Add(Programs);
item2.SubItems.Add(hkcu.Name.ToString().Replace("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion", "HKCU"));
item2.SubItems.Add(GetValue);
}
i know its probably a bit sloppy but it seems to work. now what id like to know is, if i have check boxes enabled in the list view... how can i have it:
A: Check the ones that are "enabled"/not check the ones that are "Disabled"
B: make it so when you check one it "enables" it or uncheck one it "disables" it?
OR!
(preferred) make a context menu (right click menu) for disabling and re-enabling each entry (and disabling it would mean making a sub key called something like "Disabled Start" that it would move keys to and enabling them would move them back into the previous key)
i know for enabling/disabling you can just move the key to a different subkey for safe keeping, correct? or do i have this all wrong?
I agree with you . and i world do this if i were you . :)
I think you can either store those disabled keys to a file stored in your app directory. you know , sth like ini file. or you can store them in the registry with a key created by yourself. so you can delete them when uninstalling the manager.
Well the recommended procedure would be so actually save those entries into text files. I storngly suggest that you use Isolated Storage and even stronger if you are going to be building this for a network.
I dunno much about this but I think you could also export the entries into files (which could also be IsolatedStorageFiles) just like this guy seems to have done (look for the "ExportKey" method) and then re-import them. Note that in this case you would still need to have some sort of mechanism to remember the entries so you can show them in the list view.
Regarding the context menu I think this might be what you want, but I just actually google it and running Ubuntu so I can't really try it out before.

Categories

Resources