Restricting access to certain folders with a FolderBrowserDialog - c#

I want to restrict what folder a person can choose to set their default save path in my app. Is there a class or method which would allow me to check access rights and either limit the user's options or show an error once they have made their selection. Is FileSystemSecurity.AccessRightType a possibility?

Since the FolderBrowserDialog is a rather closed control (it opens a modal dialog, does it stuff, and lets you know what the user picked), I don't think you're going to have much luck intercepting what the user can select or see. You could always make your own custom control, of course ;)
As for testing if they have access to a folder
private void OnHandlingSomeEvent(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result == DialogResult.OK)
{
String folderPath = folderBrowserDialog1.SelectedPath;
if (UserHasAccess(folderPath))
{
// yay! you'd obviously do something for the else part here too...
}
}
}
private bool UserHasAccess(String folderPath)
{
try
{
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
System.Security.AccessControl.DirectorySecurity ds =
System.IO.Directory.GetAccessControl(folderPath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
}
I should note that the UserHasAccess function stuff was obtained from this other StackOverflow question.

Related

Can the Help.ShowHelp API be modified to simply invoke the CHM file?

This is my primary way for displaying help topics from within my WinForm button click handlers:
Handler:
private void buttonHelp_Click(object sender, EventArgs e)
{
CutTools.DisplayHelpTopic(this, "create-new-viewport.htm");
}
Base method:
public static void DisplayHelpTopic(Control parent, string topic)
{
try
{
// Use an empty form as the parent so that the help file will not block the CAD software
Form mHelpParent = new Form();
// Use location of this DLL file
System.Reflection.Module mod = parent.GetType().Module;
string path = Path.GetDirectoryName(mod.FullyQualifiedName);
Help.ShowHelp(mHelpParent,
Path.Combine(path, "cut-tools-help.chm"), HelpNavigator.Topic, topic);
}
catch (System.Exception ex)
{
_AcAp.Application.ShowAlertDialog(
string.Format("\nError: {0}\nStackTrace: {1}", ex.Message, ex.StackTrace));
}
}
The forms are displaid inside AutoCAD, BricsCAD or ZWCAD. The about is fine and great. But if I want to simply display the CHM file itself (so no actual form is available) I have to do this:
[CommandMethod("TS_DisplayHelp")]
public void TS_DisplayHelp()
{
// Use location of this DLL file
System.Reflection.Module mod = GetType().Module;
System.Diagnostics.Process.Start(
Path.Combine(Path.GetDirectoryName(mod.FullyQualifiedName), "cut-tools-help.chm"));
}
It works but has one drawback. It spawns a new instance of the help and does not use the same instance.
For example:
You start one of the other commands and show the help via button click. You cancel.
You start a different command and show the help via button click. Help.ShowHelp uses same instance.
You can command and start help via TS_DISPLAYHELP and it starts new instance.
Given the context of TS_DISPLAYHELP I can't work out how to directly use Help.ShowHelp as I can in my button click handlers.
At the moment I have managed to get around this issue by duplicating the DisplayHelpTopic code directly in the command TS_DISPLAYHELP method:
[CommandMethod("TS_DisplayHelp")]
public void TS_DisplayHelp()
{
try
{
// Use an empty form as the parent so that the help file will not block the CAD software
Form mHelpParent = new Form();
// Use location of this DLL file
System.Reflection.Module mod = GetType().Module;
string path = Path.GetDirectoryName(mod.FullyQualifiedName);
Help.ShowHelp(mHelpParent,
Path.Combine(path, "cut-tools-help.chm"), HelpNavigator.Topic, "command-index.htm");
}
catch (System.Exception ex)
{
_AcAp.Application.ShowAlertDialog(
string.Format("\nError: {0}\nStackTrace: {1}", ex.Message, ex.StackTrace));
}
}
I know that my default topic is "command-index.htm".
I am happy with the above resolution.

Can you change an icon of a notifyIcon programatically?

I have a WinForms app that has a little setup program that writes to Properties.Settings. The user needs to choose his notifyIcon icon from his hard drive. I can't just change it with
notifyIcon1.Icon = Properties.Settings.Default.userIcon;
because it throws up
"Cannot convert from "string" to "System.Drawing.Icon".
Can somebody correct me?
"The user needs to choose his notifyIcon icon from his hard drive".
Is the icon in a filename somewhere? If the user selects an icon from the hard driver, does he in fact select a file that contains an icon?
If that is the case, you should define Properties.Settings.Default.UserIcon as a string and save the name of the filename. Give your window a property that gets and sets the UserIcon.
private string UserIconFileName
{
get => Properties.Settings.Default.UserIcon;
set => properties.Settings.Default.UserIcon = value;
}
private Icon LoadUserIcon
{
string userIconFileName = this.UserIconFileName
if (!File.Exists(userIconFileName))
{
// TODO: decide what to do if there is no such file
}
else
{
return new Icon(userIconFileName);
}
}
Don't forget to Save your properties when closing the program:
private void OnFormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.Save();
}

C# Settings.Default.Save() not saving? [duplicate]

This question already has answers here:
Why are my application settings not getting persisted?
(5 answers)
Closed 5 years ago.
this bug is pretty unusual. Basically my code will change the Settings.Default.Example then save and restart the program. Then when it loads, it shows a message box. However oddly, it shows a empty value when the form loads.
Here is my code:
Main.cs
private void Button1_Click(object sender, EventArgs e)
{
Settings.Default.Example = "Somevalue"; //Sets a value to the settings
Settings.Default.Save(); // Save it
MessageBox.Show(Settings.Default.Example); //Confirming it has been saved
Application.Restart();
}
private void Main_Load(object sender, EventArgs e)
{
MessageBox.Show(Settings.Default.Example); // Here is the weird part, it shows empty.
}
The MessageBox will show "Somevalue" when the button was clicked then the applcation restarts and the MessageBox that showed was empty. However repeating the process by clicking the button once more and restarting it does show the "Somevalue" MessageBox. Please help! Many Thanks!
Maybe you ran the same mistake as I did: setting the setting's scope to Application. Those kind of settings are not saved.
Set it to User to solve the problem.
rene is correct - you need to call Default.Reload after calling the Save method:
Settings.Default.Save();
Settings.Default.Reload();
Possibly a bug - ?
Posting as a reply to increase visibility -
If your AssemblyInfo.cs file has a * in Assembly Version then it's refreshing the file every build so you won't see persistence or reliability until you change that to a hard number and rebuild all, then retest everything.
After a full day of researching and studying the subject, I was able to solve this by putting the Configuration to the user:
Using System.Configuration;
Properties.Settings.Default.strinconn = txt_stringconn.Text;
Properties.Settings.Default.Save ();
Properties.Settings.Default.Upgrade ();
MessageBox.Show ("Saved Settings");
Application.Restart ();
Using Visual Studio 2013 - there is nothing I could do to make it work reliably, I would call Save and it did not save.
Save and then immediately Reload and it still would not retain the values on subsequent runs (probably related to when I stopped debugging could not identify the root cause) - very frustrating, likely there is an underlying bug but I cannot prove it.
To avoid getting crazy with this I decided to use the registry - the most fundamental way to keep app settings for an app.
Recommended for you all. Here is the code:
public static class RegistrySettings
{
private static RegistryKey baseRegistryKey = Registry.CurrentUser;
private static string _SubKey = string.Empty;
public static string SubRoot
{
set
{ _SubKey = value; }
}
public static string Read(string KeyName, string DefaultValue)
{
// Opening the registry key
RegistryKey rk = baseRegistryKey;
// Open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey(_SubKey);
// If the RegistrySubKey doesn't exist return default value
if (sk1 == null)
{
return DefaultValue;
}
else
{
try
{
// If the RegistryKey exists I get its value
// or null is returned.
return (string)sk1.GetValue(KeyName);
}
catch (Exception e)
{
ShowErrorMessage(e, String.Format("Reading registry {0}", KeyName.ToUpper()));
return null;
}
}
}
public static bool Write(string KeyName, object Value)
{
try
{
// Setting
RegistryKey rk = baseRegistryKey;
// I have to use CreateSubKey
// (create or open it if already exits),
// 'cause OpenSubKey open a subKey as read-only
RegistryKey sk1 = rk.CreateSubKey(_SubKey);
// Save the value
sk1.SetValue(KeyName, Value);
return true;
}
catch (Exception e)
{
ShowErrorMessage(e, String.Format("Writing registry {0}", KeyName.ToUpper()));
return false;
}
}
private static void ShowErrorMessage(Exception e, string Title)
{
if (ShowError == true)
MessageBox.Show(e.Message,
Title
, MessageBoxButtons.OK
, MessageBoxIcon.Error);
}
}
Usage:
private void LoadDefaults()
{
RegistrySettings.SubRoot = "Software\\Company\\App";
textBoxInputFile.Text = RegistrySettings.Read("InputFileName");
}
private void SaveDefaults()
{
RegistrySettings.SubRoot = "Software\\Company\\App";
RegistrySettings.Write("InputFileName", textBoxInputFile.Text);
}
Beware of calling
Settings.Default.Reload();
after each
Settings.Default.Save();
Save() function actually saves your changes to the file but it is not reflected to your running code. Thus, your code keeps the copy of the previous version of the file.
When you call Save() at another location in your code, it writes over your first change, effectively reverting your first change back to original value.
Very hard to pin down even when debugging.
Please have a look at this Question
Especially, try out the following code from an answer there:
using System.Configuration; // Add a reference to System.Configuration.dll
...
var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
Also, check out this overview, perhaps you ran into some limitation which is not easy to tell from your question.
This codeproject article might be of some help.
did you try calling ConfigurationManager.RefreshSection before checking that it got saved ad you could try it again after the reload
If you need to test your application how actually working in this case better to run the exe file. When you run on visual studio on debug mode when those settings saving it will take some time. Go to debug folder and run the exe you will get the messages as expected.
I had the same problem.
To sort out the problem.
Go to the custom class in Visual Studio.
Open the class and Check whether the constructor method.
If you have a constructor method, it should be parameterless.
If you have a constructor with parameters, don;t worry. Create another constructor class without an parameters.
Repeat this for all sub classes within the class.
Rebuild and run. Now your settings should save.

C# Passing Arguments Through Context Menu

so I made a program that creates a registry key on the pc, that adds an option to push your file to your device. (The program itself pushes files to your Android sd card.)
Everything works well, or at least should, once I figure out this issue. When I right-click on an item and choose my command (which is to open up with my program) my program just opens up, but no arguments are passed.
A month ago, I designed a program that opens when the user double clicks (just double click, not right-clicking through context menu) a file, it opened the specific program, and from there I was able to grab the arguments.
Why can't I from the context menu?
Thanks, sorry for lengthy post.
And here's part of my code:
private void Form1_Load(object sender, EventArgs e)
{
string c = textBox3.Text;
string[] args = System.Environment.GetCommandLineArgs();
try
{
string location = args[1];
MessageBox.Show(location);
//For testing purposes only
Properties.Settings.Default.thinglocation = location;
Properties.Settings.Default.Save();
Process();
}
catch
{
}
Here's the registry code. BTw string c is "C:\File push.exe" the location of my program.
string MenuName = "*\\shell\\NewMenuOption";
string Command = "*\\shell\\NewMenuOption\\command";
RegistryKey regmenu = null;
RegistryKey regcmd = null;
try
{
regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
if (regmenu != null)
regmenu.SetValue("", "Push to Android");
regcmd = Registry.ClassesRoot.CreateSubKey(Command);
if (regcmd != null)
regcmd.SetValue("", c +"%1");
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
finally
{
if (regmenu != null)
regmenu.Close();
if (regcmd != null)
regcmd.Close();
I assume that you have created a registry entry which creates the context menu item which you are using to launch your program. Likelihood is that you need to add ' %1' after the name of the exe you want to launch. In the context menu registry entries %1 represents the selected file, so adding this should launch your program and provide the name of the selected file as the single argument.
the reason why it worked before with the double click is that windows will launch the default program for the file double clicked, with the double clicked file as the argument.
Did you try looking in the sender or the EventArgs parameters? I would think their might be something in there. That's just a guess though. If its just a double click then why would there be any arguments? Maybe I am just misunderstanding that point. Anyways, maybe that helps. If not maybe you could clarify a little bit where the arguments are supposed to come from.
Erik

C# Windows Forms: OpenFileDialog Strange Issues

This is kinda strange, let me try to explain it as best as possible:
When I create a new file and Save it, it saves correctly (test.xml).
When I make changes to this file and Save it, it saves correctly (to test.xml)
When I make changes again to this file or just choose Save As, it works correctly (newtest.xml)
However, when I do a file open, make changes to a file (test.xml) and click Save it is saving to (newtest.xml).
This is in my MainForm.cs
if (this.openEditorDialog1.ShowDialog(this) == DialogResult.OK && editForm != null)
{
editForm.Close();
editForm = new EditorForm(this);
editForm.OpenFile(this.openEditorDialog1.FileName);
editForm.Closing += new CancelEventHandler(EditorForm_Closing);
editForm.MdiParent = this;
editForm.Show();
}
private void biFileSave_Click(object sender, EventArgs e)
{
if (!editForm.HasFileName)
{
if (this.saveEditorDialog1.ShowDialog(this) == DialogResult.OK)
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
editForm.FileName = this.saveEditorDialog1.FileName;
}
}
else
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
}
This is in my EditorForm.cs
public void OpenFile(string strFileName)
{
diagramComponent.LoadSoap(mainForm.openEditorDialog1.FileName);
this.FileName = mainForm.openEditorDialog1.FileName;
this.tabControl1.SelectedTab = DiagramTab;
}
I'm sure it has to do with the what I'm doing in the EditoForm but I can't seem to figure it out.
else
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
It looks like you want:
this.ActiveDiagram.SaveSoap(editForm.FileName);
It must have to do with mainForm.openEditorDialog1.FileName. Use a FileName property of the form that does the saving. When you open the file, set the fileName to mainForm.openEditorDialog1.FileName. When you SaveAs, set the FileName property there, too. This way, whenever the current file, changes you set the FileName property appropriately. Then, when it comes time to save the file, you always have the correct filename.
In summary, only use the .FileName property of the SaveAs dialog or the FileOpen dialog right after you use them.

Categories

Resources