I am making a UWP app and I need to store some settings for my app in roaming AppData.
I am using this code to save it:
public bool[] options =
{
true
};
public bool[] saveCBState =
{
true,
true,
true
};
ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;
// Some not important code...
roamingSettings.Values[nameof(options)] = options;
if (options[0])
roamingSettings.Values[nameof(saveCBState)] = saveCBState;
else
roamingSettings.Values[nameof(saveCBState)] = null;
Where can I find the settings that I just saved on my computer?
You could directly read the values form RoamingSettings whenever you want.
Like this:
ApplicationDataContainer myroamingSettings = ApplicationData.Current.RoamingSettings;
// load a setting that is local to the device
var optionValue = myroamingSettings.Values[nameof(options)];
var CBStateValue = myroamingSettings.Values[nameof(saveCBState)];
Also, please check the document that #Raymond Chen posted- https://learn.microsoft.com/en-us/windows/uwp/get-started/settings-learning-track#what-do-you-need-to-know, ApplicationData.Current.RoamingSettings gets the application settings container from the roaming app data store. Settings stored here no longer roam (as of Windows 11), but the settings store is still available.
Related
I'm using this code to Init a CefSharp browser with a specific cache path(i have a specific cache path because my app use a lot of login options):
browser = new ChromiumWebBrowser("");
var requestContextSettings = new RequestContextSettings { CachePath = UsersManager.GetFullCachePath(userName) };
browser.RequestContext = new RequestContext(requestContextSettings, new CustomRequestContextHandler());
browser.Dock = DockStyle.Fill;
browser.FrameLoadEnd += webViewFrameLoadEnd;
browser.LoadError += getFromBrowser_LoadError;
BrowserPanel.Controls.Add(browser);
The issue here is that for every user the cache folder is taking something like 300 MB, There is an option to make a limitation on this folder? I only need it to save the cookies for the logins and O think it saves a lot of information that I don't need.
my project has been an extremely fun journey so far but I am looking to save the configuration of the Server settings that it will connect (using MySQL Net/Connector).
When the application has loaded up, by default it connects to a server named 'sqlserver05' but I want the user/admin to be able to configure the server settings in a menustrip. So I navigate to the menustrip and you can click 'Configure' where another form pops up asking for server details.
I can do this just by a global string but I have to change the settings everytime the application runs. Can I not create an XML file to read the configuration settings that I just changed?
Sorry if I am not being clear. Many thanks,
Brandon
Yes, you can. An easy way to do this is to use application settings. This is an out-of-the-box implementation of (user and program) settings that is serialized to XML.
Please take a look at the ancient, but still applicable Using Settings in C#.
Effectively what you have to do:
Add a settings file to your project. Go the the Solution Explorer, right click on your project and select Properties. Then select Settings. Follow the steps there.
Create a setting. (In the following code it has the name PropertyName)
Get and set that setting in code.
string value = Properties.Settings.PropertyName; // get
Properties.Settings.Default.PropertyName = value; // set
Save the settings when you have changed anything:
Properties.Settings.Default.Save()
I think in your case it's better to use the Settings class that came with C#, take a look at these links.
1 , 2
First of all, create a simple POCO object to handle the value you wish to set, then read / write this object through a serializer.
You could use a Javascript serializer to generate a JSON file (which is more "trendy" than XML, but if you prefer XML, the mechanism remains the same) :
class DatabaseSettings
{
// Settings file path
private const string DEFAULT_FILENAME = "settings.json";
// Server name or IP
public string Server { get; set; } = "127.0.0.1";
// Port
public int Port { get; set; } = 3306;
// Login
public string Login { get; set; } = "root";
// Password
public string Password { get; set; }
public void Save(string fileName = DEFAULT_FILENAME)
{
File.WriteAllText(
fileName,
(new JavaScriptSerializer()).Serialize(this));
}
public static DatabaseSettings Load(string fileName = DEFAULT_FILENAME)
{
var settings = new DatabaseSettings();
if (File.Exists(fileName))
settings = (new JavaScriptSerializer()).Deserialize<DatabaseSettings>(File.ReadAllText(fileName));
return settings;
}
}
Usage is then the following :
// Read
var settings = DatabaseSettings.Load(/* your path */);
// update
settings.Server = "10.10.10.2";
// save
settings.Save(/* your path */);
I am uploading / creating file on Google Drive using .NET SDK for google drive api. Everything works fine and I can give permission to user as per my business logic like writer,reader,commenter or owner. But I want to hide the Share button from everybody except Owner as my business logic should decide which file should be shared with whom and when.
Here is the code for sharing the document:
try
{
Google.Apis.Drive.v2.Data.Permission permission = new Google.Apis.Drive.v2.Data.Permission();
switch (role)
{
case GoogleRoles.WRITER:
case GoogleRoles.READER:
case GoogleRoles.OWNER:
{
permission.Role = role;
permission.Value = userEmail;
permission.Type = "user";
break;
}
case GoogleRoles.COMMENTER:
{
permission.Role = GoogleRoles.READER; //Need to assign role before we assign the additional role of commenter.
List<String> additionalRoles = new List<string>();
additionalRoles.Add(GoogleRoles.COMMENTER);
permission.AdditionalRoles = additionalRoles;
permission.Type = "user";
permission.Value = userEmail;
break;
}
}
PermissionsResource.InsertRequest insertRequest = DriveService.Permissions.Insert(permission, fileId);
insertRequest.SendNotificationEmails = true;
insertRequest.Execute();
Where DriveService is an instance of service account. Any pointer would be a great help.
Unfortunately the Drive API doesn't yet support the feature of disabling sharing or disabling downloading. Please file a feature request here: https://code.google.com/a/google.com/p/apps-api-issues/issues/entry?template=Feature%20request&labels=Type-Enhancement,API-Drive
I had raised this as an enhancement, and got the response too. So in Google drive API its not part of permission but these are properties of file itself, so we need to set he properties instead of permissions like:
File.LabelsData labels = new File.LabelsData();
labels.Restricted = true;
File body = new File();
body.Labels = labels;
body.WritersCanShare = false;
It has solved the issue of Share but download issue is not solved it by above changes. More details about this can be found at https://developers.google.com/drive/v2/reference/files
I'm Using Geckfx18.0 and xulrunner18.01. Since Geckofx share cookie and user preferences with others instance so I try to create a new profile directory to make them have unique setting but it seems to be no use. here is my code. Is there any problem with my code?
String profileDir = port.ToString();
string directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Path.Combine("Geckofx", profileDir));
this.Text = directory.ToString();
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
Gecko.Xpcom.ProfileDirectory = directory;
GeckoPreferences.User["network.proxy.type"] = 1;
GeckoPreferences.User["network.proxy.socks"] = "127.0.0.1";
GeckoPreferences.User["network.proxy.socks_port"] = port;
GeckoPreferences.User["network.proxy.socks_version"] = 5;
GeckoPreferences.User["general.useragent.override"] = ua;
Are you initializing the instance of Gecko before setting the ProfileDirectory?
Note that the XpCom.ProfileDirectory is a static property, so if you're trying to start each instance, keep in mind you may be undoing the path you set previously.
Additionally, rather than settings the preferences in code, you save your user preferences out to a file via GeckoPreferences.Save(). Then you can load them back in to support diferent users via GeckoPreferences.Load().
I have made a Win Form and have a few controls like checkboxes, radio buttons etc. What I hope to happen is that the user will choose some settings e.g. tick the box to start the program on startup, then they can quit, when they open it again, how do I ensure that the choices that they made are saved? Thanks.
There are a few ways, but I'd recommend using the .NET user settings method to save their settings in the properties section of the application and reload and set them when they start the application again.
Here's an example:
Save Setting
Properties.Settings.Default.CheckboxChecked = true;
Properties.Settings.Default.Save();
Load Setting
checkBox.Checked = Properties.Settings.Default.CheckboxChecked;
I'd recommend giving them more meaningful names, however.
You can read more, with examples here: MSDN Using Application Settings and User Settings
This is also a nice tutorial on how to implement user settings from start to finish: C# - Saving User Settings - Easy way!
http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx Here is and Article how to use Settings in C# Application .
Where you can check ,if CheckBox is Checked with a Boolean etc.
Perhaps you’re looking for something like this:
Add this:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
Then this to the program:
for_save info = new for_save();
string general_path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string path = general_path + "\\MyApplication";
BinaryFormatter serializer = new BinaryFormatter();
info.check = true;
info.radio = false;
//write
Directory.CreateDirectory(path);
Stream write_stream = File.Create(path + "\\MyFile.txt");
serializer.Serialize(write_stream, info);
write_stream.Close();
//read
Stream read_stream = File.OpenRead(path + "\\MyFile.txt");
for_save read_info = (for_save) serializer.Deserialize(read_stream);
read_stream.Close();
textBox1.Text = read_info.check.ToString() + read_info.radio.ToString();
And this class:
[Serializable()]
class for_save
{
public bool check;
public bool radio;
}