TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Murad Mahmud
1.2k
559
15.8k
attempted to perform an unauthorized operation in c# windows
Dec 22 2013 5:42 AM
I have make a project that work properly when visual studio run as Administrator.
but this give error when visual studio run without Administrator.
here is Code-------------------------
using System;
using System.IO;
using System.Management;
using System.Security;
using System.Security.AccessControl;
using System.Security.Permissions;
using System.Security.Principal;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GetUsers();
}
public void GetUsers()
{
SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='" + 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 Form1_Load(object sender, EventArgs e)
{
}
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)
{
//try
//{
string User = Environment.UserDomainName + "\\" + comboBox1.SelectedItem.ToString();
AddDirectorySecurity(textBox1.Text,User,FileSystemRights.CreateFiles,AccessControlType.Deny );
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.WriteData, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.Write, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.WriteAttributes, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.WriteExtendedAttributes, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.Delete, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.CreateDirectories, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.AppendData, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.ExecuteFile, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.ReadAttributes, AccessControlType.Deny);
AddDirectorySecurity(textBox1.Text, User, FileSystemRights.Traverse, AccessControlType.Deny);
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// ApplyNewDesktopPath(path, @"D:\\Desktop");
MessageBox.Show("Permissions Altered Successfully");
// Process.Start("shutdown.exe", "-r -t 0");
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.ToString());
//}
}
public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
DirectoryInfo dInfo = new DirectoryInfo(FileName);
DirectorySecurity dSecurity = dInfo.GetAccessControl(AccessControlSections.Owner);
dSecurity.AddAccessRule(new FileSystemAccessRule(Account,Rights,ControlType));
// dSecurity.SetOwner(new SecurityIdentifier(WellKnownSidType.AccountAdministratorSid.ToString()));
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
dSecurity.SetOwner(sid);
dInfo.SetAccessControl(dSecurity);
}
private void button3_Click(object sender, EventArgs e)
{
try
{
string User = Environment.UserDomainName + "\\" + comboBox1.SelectedItem.ToString();
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.FullControl, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.CreateFiles, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.WriteData, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.Write, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.WriteAttributes, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.WriteExtendedAttributes, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.Delete, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.CreateDirectories, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.AppendData, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.ExecuteFile, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.ReadAttributes, AccessControlType.Deny);
RemoveDirectorySecurity(textBox1.Text, User, FileSystemRights.Traverse, AccessControlType.Deny);
MessageBox.Show("Permissions Remove Successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
DirectoryInfo dInfo = new DirectoryInfo(FileName);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));
dInfo.Create(dSecurity);
}
}
}
please help anybody.
Reply
Answers (
3
)
how to send sms using way2sms or......................
How to add footer in existing pdf -iTextSharp