Click here to Skip to main content
13,459,013 members
Rate this:
Please Sign up or sign in to 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 gives Error
"Invalid argument/options -Files".
please help me by giving some idea . how to resolve it.
private void button1_Click(object sender, EventArgs e)
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;
Posted 3-Oct-12 20:04pm
Updated 3-Oct-12 20:10pm
Why taking ownership at all?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:
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.

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
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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

This is simple:
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

pradeep rasara 4-Oct-12 4:47am
Thank U so much for Help.......
My pleasure.
Good luck, call again.
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.
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:

Check up the path names, make sure the path names with blank spaces are in ""...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi friend,

this may help you,
go through once...[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180323.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100