[英]Trouble deleting a file from c:\windows\system32 using C#
Not quite sure why I can't get this file to delete. 不太确定为什么我无法删除该文件。 I'm logged in as Admin, tried "Run as Admin", tried running in the same folder, tried setting permissions on the file, tried creating a test 1.txt file to delete and no luck.
我以Admin身份登录,尝试“以Admin身份运行”,尝试在同一文件夹中运行,尝试在文件上设置权限,尝试创建要删除的测试1.txt文件,但没有运气。 It is acting like the file isn't there.
它的行为就像文件不存在。 I can see it in Windows Explorer.
我可以在Windows资源管理器中看到它。 Please any help is welcome.
欢迎任何帮助。 Thank you for your time.
感谢您的时间。
public void deleteFile(string FileToDelete)
{
//sets system32 to system32 path
string system32 = Environment.SystemDirectory + @"\";
//File.SetAttributes(@system32 + FileToDelete, FileAttributes.Normal);
try
{
//check if file exists
if (!File.Exists(@system32 + @FileToDelete))
{
//if it doesn't no need to delete it
Console.WriteLine("File doesn't exist or is has already been deleted.");
//Console.WriteLine(system32 + FileToDelete);
} //end if
//if it does, then delete
else
{
File.Delete(system32 + FileToDelete);
Console.WriteLine(FileToDelete + " has been deleted.");
} //end else
} //end try
//catch any exceptions
catch (Exception ex)
{
Console.WriteLine(Convert.ToString(ex));
} //end catch
} //end DeleteFile
Try this one out 试试这个
check if file exist on 64 bits system using File.Exists 使用File.Exists检查文件是否在64位系统上存在
I created a test file "test.txt" and it worked no problem. 我创建了一个测试文件“ test.txt”,它没有问题。 I should not that I didn't use the method you posted, but rather used the contents of your supplied method and used them within the main() method of a console application.
我不应该不使用您发布的方法,而是使用您提供的方法的内容,并在控制台应用程序的main()方法中使用它们。
ou should also add ReadLine() to display any messages that are returned. 您还应该添加ReadLine()以显示所有返回的消息。
This is what I used, not that it's much different from what you supplied. 这是我使用的,并不是与您提供的内容有很大不同。 If this code doesn't work for you then it must be a system privileged issue.
如果此代码对您不起作用,则必须是系统特权问题。
static void Main(string[] args)
{
string FileToDelete = "test.txt";
//sets system32 to system32 path
string system32 = Environment.SystemDirectory + @"\";
try
{
//check if file exists
if (!File.Exists(system32 + FileToDelete))
{
//if it doesn't no need to delete it
Console.WriteLine("File doesn't exist or is has already been deleted.");
//Console.WriteLine(system32 + FileToDelete);
Console.ReadLine();
} //end if
//if it does, then delete
else
{
File.Delete(system32 + FileToDelete);
Console.WriteLine(FileToDelete + " has been deleted.");
Console.ReadLine();
} //end else
} //end try
//catch any exceptions
catch (Exception ex)
{
Console.WriteLine(Convert.ToString(ex));
Console.ReadLine();
} //end catch
}
If you're using Vista / Windows 7, maybe you're running into file virtualization issues. 如果您使用的是Vista / Windows 7,则可能会遇到文件虚拟化问题。 Have you tried adding a manifest with a
<requestedExecutionLevel level="requireAdministrator"/>
line in it ? 您是否尝试过添加带有
<requestedExecutionLevel level="requireAdministrator"/>
行的清单?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.