Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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.
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 3-Oct-12 20:04pm
Edited 3-Oct-12 20:10pm
JF201553.1K
v2
Comments
Sergey Alexandrovich Kryukov at 4-Oct-12 2:24am
   
Why taking ownership at all?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi friend,
 
this may help you,
go through once...
 
http://victorhurdugaci.com/using-uac-with-c-part-1/[^]
  Permalink  
Rate this: bad
good
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.
 
—SA
  Permalink  
v2
Comments
pradeep rasara at 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 at 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
Rate this: bad
good
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
System.Diagnistics.Process.Start(startInfo);
 
—SA
  Permalink  
v2
Comments
pradeep rasara at 4-Oct-12 4:47am
   
Thank U so much for Help.......
Sergey Alexandrovich Kryukov at 4-Oct-12 6:59am
   
My pleasure.
Good luck, call again.
--SA
pradeep rasara at 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 at 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

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

  Print Answers RSS
0 OriginalGriff 432
1 Kornfeld Eliyahu Peter 420
2 Yogesh Kumar Tyagi 244
3 Nelek 115
4 Gihan Liyanage 99
0 Kornfeld Eliyahu Peter 420
1 OriginalGriff 303
2 Yogesh Kumar Tyagi 230
3 Gihan Liyanage 99
4 sankarsan parida 90


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2014
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