WPF Saving UI settings to .txt file [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a window with configurable controls where I can reposition them or hide them during runtime. Now my goal is to save current state of controls so whenever user closes an app and opens it again window shows last configuration of controls. I'm saving XAML to .txt file and then reading that file. Is it good/safe approach to use that kind of method? Are there better methods?

This question will most likely be closed for being opinion-based, but I would recommend saving your control states to the built-in WPF User Settings file that comes with your project, instead of creating your own. There's nothing wrong with creating your own, but why build your own parser when Visual Studio has already provided one for you. You will find this file under your project's Properties->Settings.settings file. This file gets automatically loaded when ever the user launches your WPF app. This file is stored in your %LocalAppData% folder, under the appropriate Company name and Product name, if you specified one.
Let's say the Settings.settings file contains the following states that you want to save:
Then in your code, find the appropriate time to load these settings into your app's variables:
// Load
string lastUser = Properties.Settings.Default.LastUser;
bool detailsEnabled = Properties.Settings.Default.ShowDetails;
int timeout = Properties.Settings.Default.FormTimeoutSeconds;
And when you want to save the control states, such as when the user closes your app, or changes a state value, do this:
// Save settings
Properties.Settings.Default.LastUser = lastUser;
Properties.Settings.Default.ShowDetails = detailsEnabled;
Properties.Settings.Default.FormTimeoutSeconds = timeout;
Properties.Settings.Default.Save();

Related

That TextBox should retain the same value even after closing the form? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have three text boxes for input values, I wish to retain the values even after closing the form. Is it possible?
You might want to use User Settings. They're different from Application Settings because they can be read and write between different sessions of the same applications.
You can create a new setting at design time:
Solution Explorer > Properties
Double-click on the .settings file (this creates a new set in the default settings).
Set name and type of your settings, plus an initial value in the value form. The scope is "user";
At this point, assuming you created a mySetting1 setting of int type, you load this value in the textbox
int myValue = myNamespace.Properties.Settings.Default.mySetting1;
myTextBox1.Text = myValue.ToString();
When closing the form, be sure to save the new value (I assume you checked for integrity):
int myvalue = int.Parse(myTextBox1.Text);
myNamespace.Properties.Settings.Default.mySetting1 = myValue;
myNamespace.Properties.Settings.Default.Save();
More of this on MSDN.
Bye!
Davide.

Is it better to store a reference to a file in a DB table column or just check the file exists [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm building a .Net/Sql Server site that has a User profile page that can show a profile photo of the person if it exists. I'm storing the photos on a local folder on the server. Each user has their own sub folder which is named using their DB generated User ID. and the photo file is always called profile.jpg
In the DB I have a User table to store the profile data and I was thinking of adding a column called profile_photo_exists to indicate if a profile photo exists.
My question is, is this profile_photo_exists column redundant when I could just get some server-side code to check the user's folder to see if a photo file is there or not. I was thinking that it would be better to have a DB column for this as checking the file system each time might not be that performant. My issue with this though is that it seems like a duplication of data, and I'm afraid of it going out of synch. E.g File is missing from the folder but the DB says there is one.
Are there any other good reasons to keep the column?
The performance impact of using System.IO to check if a file exists is very small. Like you mentioned, the problem with storing the flag "profile_photo_exists" is having to keep this flag in sync with your database - seems like an unnecessary complication.
What I would probably do is save uploaded profile pictures using the user's user id. Ex. user-id.jpg "65d476eb-f52c-4faf-bec2-93073daad6ec.jpg". That'll help to normalize the names of the profile pics.
Then have some simple logic that checks for a custom profile picture and if it doesn't exist load the "default.jpg".
I'd also think about whether you need to store a default.jpg in each user folder - seems redundant? Why not just save one default.jpg in case you can't find a custom profile pic for a user? Having one default.jpg makes it easier if you ever want to change the default profile image and takes up less space on your servers.
Just my $.02. I hope this helps.

How to give path properly in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a folder in my project named Export, I save files to that folder using this code :
document.Save(#"D:\workspace\folder1\Solution.Application.DataExporter\Export\mydocument.pdf");
But when others use this code, they complain that they don't have that path. How can I give path to my code so that it works everywhere? Thanks.
Option 1: Use the Environment.SpecialFolder to get the physical location of a special folder. See here for an overview of possible special folders.
For example, if you want to put the document in 'my documents' folder, then Environment.SpecialFolder.MyDocuments would give you the location to the my documents folder on the current machine.
Code:
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
This way you are sure you always have the correct and existing location. If needed, you can always first create an export folder into this special folder with Directory.CreateDirectory(), if it does not exist yet.
Option 2: Of course, you can always ask for a location to the user if you don't want to use a predefined one, by using the SaveFileDialog class, for example.
Create the folder 'Export' in MyDocuments, and then save the file to that directory. As many others have pointed out. You need to save to a directory, that the executing user has access rights to.
string documentFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), #"Export");
if (!Directory.Exists(documentFolder))
{
Directory.CreateDirectory(documentFolder);
}
document.Save(Path.Combine(documentFolder, "mydocument.pdf"));

C# - Drag and Drop files in win form [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make like a container application where you can drag and drop files of any kind on the form and afterwards to be able to open it from there. I found some solutions where you can drag and drop files to a list view and you get it's path.. but is not how I want.. I want to have on my form in a panel or what ever is better like a shortcut of the file, an image or something to be able to see the file icon like is in explorer.
Have someone ever done something like this or point me to the right direction?
Set "Allow drop" property to "true" on your control and make use of Control.DragDrop event - it's exist on all controls, and it's invoked after drag'n'drop anything on anything(if "Allow drop" is true of course).
It this event-handler you can add new item to this or another control(ListView fits nicely to your needs), and for example to some "Dictionary" where you will store "Item and filename mapping".
Also you need to make handler for item click'ing - for ListView there a ItemActivate event. Inside this handler you can click execute default shell-action for this file by using Process.Start

How to sync system clock with global date/time? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My system clock is going crazy randomly at any moment and changing the system clock's date/time to a random one. It's not the lithium battery nor a virus because I checked. Also it's not something from the Windows.System.Time itself.
I want to create a process that will, on an interval, check to see if the system clock's date/time matches the global date/time and if not, it would sync.
I need this to run in the background. I am not even sure if a Windows process is correct way to accomplish this. I am open to any other solutions as well.
Create a new c# empty project. Click on the project and go to Properties change the output type to Windows Application (This will remove the console).
Create a new class example: Example.cs
Write the static entry point eg:
public class Example
{
static void Main(string[] args)
{
}
}
Insert your code in the Main routine.
This will create a process that contains no console/window/service.
I'm guessing this is what you want.

Categories