I'm a C# noob, and I've been looking for info on best practices for C# application development.
I want to have an application setting that defines where the application will write its log files & reporting output. I want this to default to %USERPROFILE%\Documents\<Vendor>\<Tool>, since these files are created for the user to actually look at and dispose of at their leisure. But, if the user wants the files to be written to some other location, I want to persist their preferences.
My research so far indicates that:
It's not generally a good idea to use environment variables directly. Instead, it's recommended to use the Environment.GetFolderPath method and the Environment.SpecialFolders enum to get access to special locations in Windows like the user profile.
But, it's recommended to use user-scope .NET Application Settings to persist settings from one session to another.
Is there any good way to reconcile best practice 1 and best practice 2? Is it actually possible to store this default location in the Application Settings in a compatible way without referencing the %USERPROFILE% environment variable?
A related question:
Assume I keep %USERPROFILE%\Documents\<vendor>\<tool> as the default value for the Setting. I want the Setting's value to be bound to a textbox on the main application form (with a Browse button to select a new path). How can I bind the Setting to a textbox in such a way that %USERPROFILE% is resolved to a conventional filesystem path (eg C:\Users\<username>\Documents\<vendor>\<tool>) for purposes of display to the user?
I think that storing paths with environment variables is actually the best way to reference a special folder as a string and still make the path easily editable by the user, should s/he decide to do so.
Environment variables can be resolved to a simple string in .NET by calling the Environment.ExpandEnvironmentVariables method. You can use this method just before presenting the path in your TextBox and thus make it easily editable for the user. Once the user customizes the path, you can store it as an absolute path from there on, I suppose.
Related
I am trying to represent environment variables in the Salesforce codebase and came across Custom Metadata Types. So based on which Sandbox I am in, I want to vary the baseURL of an external service that I am hitting from my apex class. I want to avoid hard coding anything in the class, and hence trying to find out an environment variable like solution.
How would you represent the URL as a custom metadata type? Also, how can I access it in the class? What happens when a qa sandbox is refreshed from prod? Do they custom metadata type records get overridden?
How are you calling that external service? If it's truly a base url you might be better of using "named credential" for it. It'll abstract the base url away for you, include authentication or certificate if you have to present any...
Failing that - custom metadata might be a poor choice. They're kind of dictionary objects, you can add more (but not from apex) but if you deploy stuff using Git/Ant/SFDX CLI rather than changesets it'd become bit pain, you'd need different custom metadata value for sandbox vs prod. Kinda defeats the purpose.
You might be better off using custom setting instead (hierarchy is enabled by default, list you'd have to flip a checkbox in setup. List is useful if you need key-value kind of pairs, similar to custom metadata): https://salesforce.stackexchange.com/questions/74049/what-is-the-difference-between-custom-settings-and-custom-metadata-types
And you can modify them with Apex too. Which means that in ideal world you could have a "postcopy" class running as soon as sandbox is refreshed that overwrites the custom setting with the non-prod value. For named credential I don't think you can pull it off, you'd need a mini deployment that changes it or manual step (have you seen https://salesforce.stackexchange.com/q/955/799 ?)
I am currently building a WPF application and have some system wide settings. Where is the best place to store these settings? App.Config file, Database or some other sort of XML file? (need to read and write).
My other issue is the application has two states (i.e. Admin Mode, Client Mode) and depending on the state the application behaves differently i.e. (Shut Down, Restart, Lock, Unlock etc.) This seems like a good case to implement the State design pattern... but the issue I am facing is that the running application is itself the context class. I am a bit confused... should I be implementing the Singleton Pattern? How is this best implemented?
In general it is often easiest to store application settings using the Settings page of the Project Designer in Visual Studio. These settings can either be stored per user or per application. Either way, they can be saved easily by calling:
Properties.Settings.Default.Save();
They can be accessed similarly:
Properties.Settings.Default.FirstUserSetting = "abc";
You can find out full details by taking a look at the Managing Application Settings page on MSDN.
Regarding your second question, I would recommend using the State Design Pattern. Unfortunately, I didn't understand your problem with that, so please let me know what your issue is and I will try to address it.
Could you please tell me is there any way to display Isolated-storage path using In-browser type silverlight5 application.
The full path to the isolated storage is normally not programmatically retrievable, at least not in Silverlight.
It is not possible to access m_AppFilesPath member in the IsolatedStorageFile instance through reflection, since the field is private. If you try to do this, you are likely to receive FieldAccessException.
If you are looking for the file system location of the isolated storage files and directories, please read this SO answer.
If you want to find out the file paths relative to the isolated storage root when running your application, AFAIK there is only the GetFileNames methods in the IsolatedStorageFile class, preferably in combination with GetDirectoryNames.
It is in principle possible to request the relative file path of a specific file as well by accessing the Name property of the IsolatedStorageFileStream object, however this property is only available in trusted applications.
Would it be possible to get the isolated storage location in a WPF app? I'm having problems with a customer using VDI (Virtual Desktop Infrastructure). Sometimes the isolated storeage will work and sometimes it doesn't. Seems like it's random and I want to try to debug this.
They're using a roaming user account.
Is there any way i can load a silverlight application from isolated storage and replace the current application ?
Short answer: no, you cannot do this. You could however store a dll, read and load it via reflection if you are in a trusted out-of-the-browser application.
Anyway I don't believe this to be a very good practice, it's very likely there is a different better way to solve your particular problem.
You can't replace the currently running application from within Silverlight code.
However you could create a single application to act as a shell. You could store assemblies or entire Xaps in isolated storage. Using the AssemblyPart class allows you to load an assembly from this storage, create an instance of an entry type and execute an entry point method.
The big caveat would be that this "stored application" would have to be coded specifically to work within your shell. You would need to provide your ways in particular to allow for "application" level state to be accessed and a means of loading content assets such as images.
In my WinForms app, I have a few textboxes that the user typed some data into. I want to store the data, but I don't want to use a database. In my stone-age C++ days, I'd just use a .ini. Somehow, app.config doesn't seem like the right place to store this data, though.
What are my other options?
I would say the .config file is the right place. Just be sure to use the User scoped area of the Settings.settings file rather than the Application scope.
This works well for simple data types and when you have fixed values that will need to be saved because you need to define what variables you want to store at design time. So if your textboxes are dynamically created and you don't know many values you need to store it is not very useful.
Using IsolatedStorage might be another good option. You can create your own file in any format you want (keeping any values you need) and store it to the local machine in "IsolatedStorage".
You can create a folder somewhere on the disk and simply write a file in any suitable format (XML, plain text, your choice). You could for instance do this under the path pointed out by Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).
I would look into isolated storage. It is easy to set up per-user. And since it requires only partial trust, it will work for any deployment scenario.
Check this introduction.
Depending on how many variables/data you're looking to save the app.config/settings file can be the ideal place.
Check out the Settings Tab in the Project properties and note that you can set both Application settings and User settings. Application settings affect the entire application. User settings are stored per user.
The section of the app.config that contains user settings will be saved to the user directory when they are saved and reincorporated when they restart the app.
Check this url for an introduction to Application/user settings on MSDN and also this SO url for a similiar question.:
You could also look into storing your familiar old .ini files in a per user .ini by checking out the Special Folders enum as per this url.
I believe the proper place to store user settings in WinForms 2.0 would be in the settings file (not the config file). Here's a quick article for explanation.
Create a .config or other data file (e.g. xml) in the application data for the specific user.
use system.environment.specialfolder to get the ApplicationData folder, add a subfolder with your company name, within this a subfolder with your application name, within this your data file for this specific user. Thus,
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\YourCompany\\YourApplication\\YourData.config"