C# 如何使用asp.net删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1762157/
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
How to Delete a file using asp.net?
提问by Surya sasidhar
i write the code in asp.net using c# to delete the file in my computer, but it is not deleting please help me thank u. this is my code, i write in button click event
我使用c#在asp.net中编写代码删除我计算机中的文件,但它没有删除请帮助我谢谢你。这是我的代码,我写在按钮点击事件中
string path = "E:\sasi\delt.doc";
FileInfo myfileinf = new FileInfo(path);
myfileinf.Delete();
采纳答案by Mongus Pong
Make sure the ASP user has permissions to this folder. By default this user is not given access to much of the harddrive..
确保 ASP 用户对该文件夹具有权限。默认情况下,此用户无法访问大部分硬盘驱动器。
回答by Darin Dimitrov
In order to delete a file you must ensure that the account has sufficient permissions. In general ASP.NET applications run under limited permission account such as Network Service
. For example if your application runs under IIS 6 you could go to the Administration Console and set a custom account in the application pool properties:
为了删除文件,您必须确保该帐户具有足够的权限。通常 ASP.NET 应用程序在有限权限帐户下运行,例如Network Service
. 例如,如果您的应用程序在 IIS 6 下运行,您可以转到管理控制台并在应用程序池属性中设置一个自定义帐户:
alt text http://i.msdn.microsoft.com/Bb969101.SharePoint_SQL_TshootingFig3%28en-US,SQL.90%29.jpg
替代文字 http://i.msdn.microsoft.com/Bb969101.SharePoint_SQL_TshootingFig3%28en-US,SQL.90%29.jpg
You need to ensure that the account is member of the IIS_WPG
group.
您需要确保该帐户是该IIS_WPG
组的成员。
回答by BJ Patel
public void DeleteFileFromFolder(string StrFilename)
{
string strPhysicalFolder = Server.MapPath("..\");
string strFileFullPath = strPhysicalFolder + StrFilename;
if (IO.File.Exists(strFileFullPath)) {
IO.File.Delete(strFileFullPath);
}
}