C# 使用c从c:\windows\system32删除文件时出现问题#
不太清楚为什么我不能删除这个文件。我以管理员身份登录,尝试“以管理员身份运行”,尝试在同一文件夹中运行,尝试设置文件权限,尝试创建要删除的test 1.txt文件,但没有成功。好像文件不在那里。我可以在Windows资源管理器中看到它。欢迎任何帮助。谢谢你抽出时间C# 使用c从c:\windows\system32删除文件时出现问题#,c#,file-io,file-permissions,C#,File Io,File Permissions,不太清楚为什么我不能删除这个文件。我以管理员身份登录,尝试“以管理员身份运行”,尝试在同一文件夹中运行,尝试设置文件权限,尝试创建要删除的test 1.txt文件,但没有成功。好像文件不在那里。我可以在Windows资源管理器中看到它。欢迎任何帮助。谢谢你抽出时间 public void deleteFile(string FileToDelete) { //sets system32 to system32 path
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
试试这个
我创建了一个测试文件“test.txt”,它工作正常。我不应该说我没有使用您发布的方法,而是使用了您提供的方法的内容,并在控制台应用程序的main()方法中使用了它们
您还应该添加ReadLine()以显示返回的任何消息
这是我用的,并不是说它和你们提供的有很大不同。如果此代码不适用于您,那么它一定是系统特权问题
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
}
如果您使用的是Vista/Windows7,可能会遇到问题。您是否尝试过添加包含
行的清单?我刚刚测试了代码,但它仍然返回“文件不存在”。关于如何检查权限的任何想法。我愿意测试任何东西。您是否100%确定该文件存在于您的System32目录中?您确定尚未事先删除该文件吗?我可以在Windows资源管理器中查看该文件,并看到它位于C:\Windows\system32下。我已经用cmd测试了“del c:\windows\system32\1.txt”,它将删除文件。只是尝试了一下,没有任何帮助。谢谢你的回复。我会记住的,对不起,雷蒙德。我本想把这个答复贴在另一个答复上。我相信这是其中的一部分。我刚刚按照Tony Lee的建议进行了测试,它试图读取C:\windows\SysWOW64\即使我让它打印路径,它仍然显示C:\windows\system32Environment。ExpandEnvironmentVariables(@“%systemroot%\Sysnative”)
解决了这个问题。我发现它是由于你的链接!!!使用sysinternals中的procmon监视对文件系统的i/o请求。谢谢你的信息!!非常好的建议。它正在检查C:\windows\SYSWOW64是的,我正在使用Win 7 64位。我会试试这个建议。谢谢。刚刚测试了这个,没有任何帮助:(我确实遇到了文件虚拟化问题。谢谢!