简体   繁体   English

无法以编程方式重命名目录

[英]Unable to programmatically rename directory

I have asp.net web forms project. 我有asp.net网络表单项目。 It is created as "WebSite" and is running on my local iis. 它创建为“ WebSite”,并在我的本地iis上运行。 In this project I've created folder with subfolder, where some picutes are stored.(~/listings/listing1, ~/listings/listing2...) 在这个项目中,我创建了带有子文件夹的文件夹,其中存储了一些图片。(〜/ listings / listing1,〜/ listings / listing2 ...)

I can programmatically add new files to this subfolders, can delete this subfolders, but I can't rename them. 我可以以编程方式将新文件添加到此子文件夹,可以删除此子文件夹,但是无法重命名它们。 I've tried to rename this subfolders manually: when project is not runing - I am able to do so, when i run debuging in Visual Studio, I can't rename them even manually. 我尝试手动重命名此子文件夹:当项目未运行时-我能够这样做,当我在Visual Studio中运行调试时,即使手动也无法重命名它们。

For programmatical renaming I am using this code: DirectoryInfo rootDir = new DirectoryInfo(oldListingFolderPath); 对于编程重命名,我使用以下代码:DirectoryInfo rootDir = new DirectoryInfo(oldListingFolderPath);
rootDir.MoveTo(newListingFolderPath); rootDir.MoveTo(newListingFolderPath); This cause error "Access to the path ... is denied." 这将导致错误“拒绝访问路径...。”。 I've granted full controll to this folder for NETWORK SERVICES, IIS_IUSERS, USER. 我已授予该文件夹对NETWORK SERVICES,IIS_IUSERS,USER的完全控制权。 I've also tried to remove readonly attributes for files/folders in subfolder that should be renamed, but this didn't help. 我也尝试删除应该重命名的子文件夹中文件/文件夹的只读属性,但这无济于事。

Are there any other ways to rename needed subfolder. 还有其他方法可以重命名所需的子文件夹。 Or can you suggest any ways to find, what is blocking renaming of the folder. 或者您可以建议任何查找方法,哪些阻止文件夹重命名。

EDIT1: paths of subfolders (~/listings/listing1) are mentioned just for example. EDIT1:仅提及子文件夹(〜/ listings / listing1)的路径。 When I am renaming directory, I am using full path to file: Server.MapPath("~/listings/listing1"). 重命名目录时,我使用的是文件的完整路径:Server.MapPath(“〜/ listings / listing1”)。 Sorry for not mentioning this before. 抱歉,您之前没有提到此内容。

EDIT2: I've tried to do this in other way: firs I am creating new folder with needed new name, then I am copying existing folder to new folder and then I am deleting old folder. EDIT2:我尝试以其他方式执行此操作:首先,我用所需的新名称创建新文件夹,然后将现有文件夹复制到新文件夹,然后删除旧文件夹。 This works. 这可行。 But I still can't just rename existing folder... 但是我仍然不能只重命名现有文件夹...

The problem seems to be that when the ASP.NET web application executes, it still has a reference to(which means ASP.NET runtime is still using) the folder. 问题似乎在于,当ASP.NET Web应用程序执行时,它仍然具有对该文件夹的引用(这意味着ASP.NET运行时仍在使用)。 Hence the access violation error. 因此,访问冲突错误。

Solution is to move the folder from ~ folder to elsewhere in the filesystem, not inside ~ . 解决方案是将文件夹从~文件夹移动到文件系统中的其他位置,而不是~

~/listings/listing1, ~/listings/listing2 - is a virtual path. 〜/ listings / listing1,〜/ listings / listing2-是虚拟路径。 To work with files you need to work with physical path. 要使用文件,您需要使用物理路径。

Add PhysicalApplicationPath to directory path 将PhysicalApplicationPath添加到目录路径

string dir_path = HttpContext.Current.Request.PhysicalApplicationPath + "listings\listing1\";

or Server.MapPath("/") or HttpRuntime.AppDomainAppVirtualPath - depends on the type of application Server.MapPath("/")HttpRuntime.AppDomainAppVirtualPath取决于应用程序的类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM