如何在Windows 7中取得程序文件的所有权 [英] How to Take ownership of Program Files in windows 7

查看:117
本文介绍了如何在Windows 7中取得程序文件的所有权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码获取"C:\ Program Files(x86)\ Windows Sidebar \ sidebar.exe"此文件的所有权,但是它不起作用
我以以管理员身份运行"身份运行此代码.当我通过命令提示符
并编写如下命令:
takeown/f"C:\ Program Files(x86)\ Windows Sidebar \ sidebar.exe"

有用.但是当我在C#代码中使用它时,它不起作用.以下是我正在使用的tha代码.它给出了错误
无效的参数/选项-文件".
请给我一些想法帮助我.解决方法.

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();
            
     
        }

推荐答案

您在这里犯了几个错误.

这里无效的文件"的问题仅与程序文件"中的空白空间有关..您应将文件名包含在此目录中的"中.但是暂时不要这样做,首先要修复另一个错误(但是两个问题都很好理解).

第二个错误是:您绝对不需要"cmd.exe" .谁告诉你应该在那儿?这只是另一个程序,一个命令解释器,与您的进程相比,它与您的程序相比没有什么特别的.换句话说,您只需要应用程序文件"takeown".

请执行以下操作:
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);



现在,关于以管理员身份运行.您最好为运行此代码的应用程序请求提升的特权.您可以使用应用程序清单来做到这一点.我在最近的回答中通过引用在拘留中解释了这个问题:
获取我的WPF应用程序的权限 [



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


[回答注释中提出的另一个问题:如何在隐藏控制台的情况下运行控制台应用程序:]

这很简单:
[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



—SA


朋友,

这可能对您有帮助,
经历一次...

http://victorhurdugaci.com/using-uac-with-c-part-1/ [ ^ ]
Hi friend,

this may help you,
go through once...

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


这篇关于如何在Windows 7中取得程序文件的所有权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆