Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am using this code to take ownership of "C:\Program Files (x86)\Windows Sidebar\sidebar.exe" this file but it does not work
I am running this code as Run as Administrator. while i go through command prompt
and write the command like:
takeown /f "C:\Program Files (x86)\Windows Sidebar\sidebar.exe"

It works. but when i am using this in c# code it does not work. below is the tha code that i am using.it gives Error
"Invalid argument/options -Files".
please help me by giving some idea . how to resolve it.
C#
private void button1_Click(object sender, EventArgs e)
        {
            //ownership
String FileName = @"C:\Program Files (x86)\Windows Sidebar\sidebar.exe";
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;

            startInfo.FileName = "cmd.exe";
           
            startInfo.Arguments = @" /k takeown /f " + FileName ;
           
            process.StartInfo = startInfo;
            process.Start();
            
     
        }
Posted
Updated 3-Oct-12 20:10pm
v2
Comments
Sergey Alexandrovich Kryukov 4-Oct-12 2:24am    
Why taking ownership at all?
--SA

[Answering a different question asked in comments: how to run a console application with the console hidden:]

This is simple:
C#
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();     
startInfo.FileName = application; // I refer to the code I've shown in my Solution 2
startInfo.Arguments == arguments; // here, too
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // this solves this problem
System.Diagnistics.Process.Start(startInfo);


—SA
 
Share this answer
 
v2
Comments
pradeep rasara 4-Oct-12 4:47am    
Thank U so much for Help.......
Sergey Alexandrovich Kryukov 4-Oct-12 6:59am    
My pleasure.
Good luck, call again.
--SA
pradeep rasara 5-Oct-12 2:27am    
i wanna help from you ,i used this code it work fine for program files directory but not working for windows directory.
Sergey Alexandrovich Kryukov 5-Oct-12 15:45pm    
This is unrelated issue, but could be related to access to files or something like that.
Fist, try to run the application manually with the code line to make sure it works with the permissions you have. If not, try to run as administrator and compare:
http://www.sevenforums.com/tutorials/11841-run-administrator.html

Check up the path names, make sure the path names with blank spaces are in ""...
--SA
You made couple of bugs here.

The problem with invalid "Files" here is simply related to the blank space in "Program Files". You should enclose the file name in this directory in "". But don't do it, just yet, first fix another bug (but both problems are good to understand).

The second bug is: you absolutely don't need "cmd.exe". Who told you it should be there? This is just yet another program, a command interpreter, it does nothing special compared to your, as the starting of the process is concerned. In other words, you need only the application file "takeown".

Do this:
C#
string fileName = //...
string application = "takeown";
string arguments = string.Format(@"/f ""{0}""", fileName); // note "" around file name
System.Diagnostics.Process.Start(application, arguments);


Now, about running as administrator. You should better request elevated privileges for the application running this code. You can do it with the application manifest. I explain this problem in detain via references in my recent answer:
Get rights for my WPF application[^].

That's all you need to resolve your problem. By the way, I doubt making ownership of the file like the one you show makes sense. Why? Just a note not related to your question itself.

—SA
 
Share this answer
 
v2
Comments
pradeep rasara 4-Oct-12 2:45am    
Tanx for your answer. How to make it Hidden....Command prompt seeing for a second.
how to make it hidden
Sergey Alexandrovich Kryukov 4-Oct-12 3:04am    
Not related to your original question much, so I answered in a separate answer. Please combine the code with the one in this solution -- it's clear enough.
--SA
Hi friend,

this may help you,
go through once...

http://victorhurdugaci.com/using-uac-with-c-part-1/[^]
 
Share this answer
 

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