English 中文(简体)
如何让我的方案查阅系统32中的档案?
原标题:How to let my program access a file in System32?
  • 时间:2017-08-08 23:32:52
  •  标签:
  • c#

I want to make a C# program that deletes a file in system32. The program can delete a file in an normally accessed area such as the desktop but won t find a file in system32, how would I give the program access to system32? Here s my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;


namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string filepath = @"C:WindowsSystem32New.txt";
            if (File.Exists(filepath))
                {
                     File.Delete(filepath);
                }
            else
                {
                Console.WriteLine("File not found");
                Console.ReadLine();
                }
        }
    }
}
问题回答

首先,请SOULD NOT删除系统32倍的档案,这些档案通常属于监督厅,不应受到诱惑。

不管怎么说,我不问,为什么你有这一要求,但窗户用户账户控制(UAC)将不允许你像现在这样开展这项行动。

//take ownership of the file, code assumes file you want to delete is toBeDeleted.txt
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", @"/k takeown /f C:WindowsSystem32	oBeDeleted.txt && icacls C:WindowsSystem32	oBeDeleted.txt /grant %username%:F");
processInfo.UseShellExecute = true;
processInfo.Verb = "runas";
processInfo.FileName = fileName;//path of your executable
try
{
    Process.Start(processInfo);
    // a prompt will be presented to user continue with deletion action
    // you may want to have some other checks before deletion
    File.Delete(@"C:WindowsSystem32	oBeDeleted.txt");
    return true;
}
catch (Win32Exception)
{
    //Do nothing as user cancelled UAC window.
} 

如果你想避免这种情况,就必须在上申请“Manifest”(UAC),要求““highestAvailable/codelement”执行水平:这将造成UAC在你开始申请时立即出现,并引起儿童升级。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...