使用 C# 从 c:\windows\system32 删除文件时遇到问题
不太清楚为什么我无法删除该文件。我以管理员身份登录,尝试“以管理员身份运行”,尝试在同一文件夹中运行,尝试设置文件权限,尝试创建要删除的测试 1.txt 文件,但没有成功。它的表现就像该文件不存在一样。我可以在 Windows 资源管理器中看到它。欢迎任何帮助。谢谢您的宝贵时间。
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
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. It is acting like the file isn't there. I can see it in Windows Explorer. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我创建了一个测试文件“test.txt”,它工作没有问题。我不应该说我没有使用您发布的方法,而是使用您提供的方法的内容并在控制台应用程序的 main() 方法中使用它们。
您还应该添加 ReadLine() 以显示返回的任何消息。
这是我用的,和你提供的没什么不同。如果此代码对您不起作用,那么它一定是系统特权问题。
I created a test file "test.txt" and it worked no problem. 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.
ou should also add ReadLine() to display any messages that are returned.
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.
尝试一下
检查文件是否存在于 64 上使用 File.Exists 的位系统
Try this one out
check if file exist on 64 bits system using File.Exists
如果您使用的是 Vista / Windows 7,也许您会遇到 文件虚拟化问题。您是否尝试过添加其中包含
行的清单?If you're using Vista / Windows 7, maybe you're running into file virtualization issues. Have you tried adding a manifest with a
<requestedExecutionLevel level="requireAdministrator"/>
line in it ?