C# 使用 System.IO.Delete 从目录中删除某些文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/813030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:11:58  来源:igfitidea点击:

Using System.IO.Delete to remove certain files from a directory?

c#asp.netvb.netimagedirectory

提问by Etienne

I have 2 images inside a folder called Pics..... Image1.jpg and Image2.jpg.

我在名为 Pics..... Image1.jpg 和 Image2.jpg 的文件夹中有 2 张图像。

What code must i place inside my Submit button to just delete Image1.jpg located here "~/Pics/Image1.jpg"

我必须在提交按钮中放置什么代码才能删除位于此处的 Image1.jpg“~/Pics/Image1.jpg”

Any help would be great!!!

任何帮助都会很棒!!!

采纳答案by inspite

You need to use System.IO.File.Deletenot System.IO.Delete

您需要使用System.IO.File.Delete而不是 System.IO.Delete

string path = "~/Pics/Image1.jpg";
System.IO.File.Delete(Server.MapPath(path))

回答by Baget

i would try:

我会尝试:

String FilePath;
FilePath = Server.MapPath("~/Pics/Image1.jpg");
File.Delete(FilePath);

回答by Jason Williams

The syntax is:

语法是:

System.IO.File.Delete(Server.MapPath("~/Pics/Image1.jpg"));

You will need to make sure the user your web app is running as has delete (change) permissions on the file you are deleting, however.

但是,您需要确保您的 Web 应用程序运行的用户对您要删除的文件具有删除(更改)权限。

回答by Pritpal Singh Matharu

Try This:

尝试这个:

String FileName = "Image1.jpg";
System.IO.File.Delete(Server.MapPath(("~/Pics/") + FileName));