I have make a project then make setup file .this setup file work properly in my pc but when i setup another pc then give a Error message.
Unhandaled Exception has occurred in you application. If you click Continue the application will ignore this error and attempt to continue. if you cliclk Quit thwe application will close immediatly.
Attempt to perform an unauthorized Operation.
Project code is
using System;
using System.IO;
using System.Windows.Forms;
using System.Security.AccessControl;
using System.Management;
namespace FolderPermisition
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GetUsers();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void GetUsers()
{
SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='"+ System.Environment.UserDomainName.ToString() + "'");
try
{
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
foreach (ManagementObject mObject in mSearcher.Get())
{
comboBox1.Items.Add(mObject["Name"]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog();
myFolderBrowserDialog.ShowDialog();
textBox1.Text =myFolderBrowserDialog.SelectedPath.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
DirectoryInfo myDirectoryInfo = new DirectoryInfo(textBox1.Text);
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
string User = System.Environment.UserDomainName + "\\" + comboBox1.SelectedItem.ToString();
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User,FileSystemRights.Read, AccessControlType.Deny));
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
MessageBox.Show("Permissions Altered Successfully");
}
private void button3_Click(object sender, EventArgs e)
{
DirectoryInfo myDirectoryInfo = new DirectoryInfo(textBox1.Text);
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
string User = System.Environment.UserDomainName + "\\" + comboBox1.SelectedItem.ToString();
myDirectorySecurity.RemoveAccessRule(new FileSystemAccessRule(User, FileSystemRights.Read, AccessControlType.Deny));
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
MessageBox.Show("Permissions Remove Successfully");
}
}
}
Please help me anybody.
I have use try Catch but Error is Show
System.UnauthorizedAccessException : Attempted To Perform an unauthorized operation.
at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType type,String Name,SafeHandle handle,SecurityInfos securityInformation, SecurityIdentifier owner,SecurityIdentifier group,GenericAcl sact, GenericAcl dacl)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String Name.SafeHandle handle,AccessControlSection includeSections,Object exceptionContex)
then more same error---------------------------------
This code work in my pc Properly and there is no Error.
But when create setup file and Install another pc Give above Error.