不太确定为什么我无法删除此文件。我以管理员身份登录,尝试“以管理员身份运行”,尝试在同一文件夹中运行,尝试设置文件权限,尝试创建一个测试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
答案 0 :(得分:0)
答案 1 :(得分:0)
我创建了一个测试文件“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
}
答案 2 :(得分:0)
如果您使用的是Vista / Windows 7,可能会遇到file virtualization问题。您是否尝试添加包含<requestedExecutionLevel level="requireAdministrator"/>
行的清单?