Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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.
Posted
Updated 3-Dec-13 21:32pm
v7
Comments
Put Try Catch inside each method and try to handle the Exceptions.
Sergey Alexandrovich Kryukov 3-Dec-13 9:16am    
Tadit, "each method" is really, really bad advice. Do you know how to use exception handling yourself? Maybe not quite.

Exception handling is created to reduce handling and separate it from "regular" code. For debugging purposes, you only need to handle all exception in one point per thread, somewhere closer to the top stack frame if each. The methods where the exception is thrown and propagated is seen by using Exception.StackTrace.

Using the debugger can also solve this problem.

—SA
Maciej Los 3-Dec-13 10:44am    
Good advice, my virtual 5!
Sergey Alexandrovich Kryukov 3-Dec-13 11:27am    
Thank you, Maciej. Now I added more detailed advice in a formal answer. Please see.
After all, I was the very early implementor of the fully-fledged structured exception handling before it was introduced in C++ and Delphi (was available in CLU and then Ada) :-).
—SA
Yeah, you are right. Headbang. :)
In a hurry, I just commented that.

1 solution

 
Share this answer
 
v3
Comments
Maciej Los 3-Dec-13 11:33am    
WOW!
Sergey Alexandrovich Kryukov 3-Dec-13 11:36am    
:-)
Maciej Los 3-Dec-13 11:42am    
Starting at this moment i'll call you: Mister Treasury of Knowledge ;)
Sergey Alexandrovich Kryukov 3-Dec-13 11:45am    
Please, don't be such a shameless flatterer :-).
—SA
Maciej Los 3-Dec-13 12:07pm    
Who, me? :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900