hi,
i am using cacls command to grant full permission to the user.But it's not working i am posting here source code please help me to identify the error.I also try this code by using Administrator account but it's not changing the file permission.

code is

ProcessStartInfo psiOpt = new ProcessStartInfo(@"cacls.exe", @"""" + strDir + @""" /E /G """+Environment.UserName+@""":F");//i also tried here by giving user name manually 

                // We don't want to show the Command Prompt window and we want the output redirected

                psiOpt.WindowStyle = ProcessWindowStyle.Hidden;
                psiOpt.RedirectStandardOutput = true;
                psiOpt.UseShellExecute = false;
                psiOpt.CreateNoWindow = true;
                // Create the actual process object
                Process procCommand = Process.Start(psiOpt);
                // Receives the output of the Command Prompt

                // StreamReader srIncoming = procCommand.StandardOutput;

                // Show the result

                //   Console.WriteLine(srIncoming.ReadToEnd());


                // Close the process

                procCommand.WaitForExit();

There are usually two things that go wrong when starting processes. First is command arguments and second is the command itself i.e. the command isn't found as expected.

Testing arguments is easy, just dump them to screen. In order to test if the command can be found, I usually try to start it from the command prompt. If the command can't be found it needs the full path.

So here's the code

// DEBUG: My test file
string strDir = @"D:\test.bat";

string procArgs = @"""" + strDir + @""" /E /G """ + Environment.UserName + @""":F";

// DEBUG: dump arglist
MessageBox.Show(procArgs);

// cacls.exe should be found in System32 folder
ProcessStartInfo psiOpt = new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) +  @"\cacls.exe", procArgs );

// We don't want to show the Command Prompt window and we want the output redirected

psiOpt.WindowStyle = ProcessWindowStyle.Hidden;
psiOpt.RedirectStandardOutput = true;
psiOpt.UseShellExecute = false;
psiOpt.CreateNoWindow = true;
// Create the actual process object
Process procCommand = Process.Start(psiOpt);
// Receives the output of the Command Prompt

StreamReader srIncoming = procCommand.StandardOutput;

// Show the result

Console.WriteLine(srIncoming.ReadToEnd());


// Close the process

procCommand.WaitForExit();

At least in my comp, the path to cacls.exe needed to be fully qualified (C:\Windows\System32\cacls.exe).

One more thing. Cacls.exe is depreciated, try to use ICacls.exe instead.

HTH

Thanks it really truth.But it's have some problem it's not giving permission i have tried it now.Please help me to figure out this problem

Ok. Could you please post your current code. Also, what OS-version you're using? And to what folder/files you're trying to give permissions? Is there something else to take into account, like AD and GPOs?

it's not giving permission i have tried

If you compare permissions before and after, does anything change?

No,Sir it's not changing

I also asked:

Could you please post your current code. Also, what OS-version you're using? And to what folder/files you're trying to give permissions? Is there something else to take into account, like AD and GPOs?

Now I'll add one more question :twisted: If you try the same thing from the command line i.e. dump variable procArgs and use it as a param, open cmd.exe and try the command "CACLS <file/folder> /E /G <user>:F", does it work then?

yes then it works but when i run from the program as u give it give error command not found

then it works but when i run from the program as u give it give error command not found

More debugging then:

- move cursor to the line 19 (where's that "Process.Start...")
- set a breakpoint (press F9, toggles on/off breakpoint)
- execute code until it stops to breakpoint
- open immediate window if it's not open (press Ctrl+D,I or menu Debug->Windows->Immediate)
- write this to immediate window and press ENTER:
? psiOpt.FileName + " " + psiOpt.Arguments
- now you should get a line similar to:
"C:\\Windows\\system32\\cacls.exe \"D:\\test.bat\" /E /G \"Teme64\":F"
- copy that line to Notepad or your editor and remove extra characters until you have a line similar to:
C:\Windows\system32\cacls.exe "D:\test.bat" /E /G "Teme64":F"
- now copy that line
- open command prompt (i.e. cmd.exe), paste the line and press ENTER. Your command window should be similar to:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Teme64>C:\Windows\system32\cacls.exe "D:\test.bat" /E /G "Teme64":F"
processed file: D:\test.bat

C:\Users\Teme64>

- the text "processed file: D:\test.bat" indicates that everything was fine i.e. user got full control to file D:\test.bat

To spot your error, check carefully text: C:\Windows\system32\cacls.exe. No typos, no extra backslashes etc. If you have exactly same text, then open Explorer and go to folder C:\Windows\system32 to check that cacls.exe command is there. If it isn't, check folders C:\Windows\system and C:\Windows for it. If still not found make a global search for cacls.exe. Once you find it, copy it to system32 folder. If you don't find it at all, look for it from your original OS installation disk.

HTH

commented: Helpful! +10

Thanks sir

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.