Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中移动后无法删除文件#_C#_File - Fatal编程技术网

C# 在c中移动后无法删除文件#

C# 在c中移动后无法删除文件#,c#,file,C#,File,我试图删除一个文件,但收到错误消息(拒绝访问),即使我有完全权限。最初,我的文件将位于我的根文件夹中。首先我重命名文件,然后将文件移动到另一个文件夹(根文件夹之外),如下所示 System.IO.File.Move(strPhysicalFolder+ tpfile,strPhysicalFolder+fName); System.IO.File.Move(strPhysicalFolder + fName, filePath + fName); System.IO.File.SetAttrib

我试图删除一个文件,但收到错误消息(拒绝访问),即使我有完全权限。最初,我的文件将位于我的根文件夹中。首先我重命名文件,然后将文件移动到另一个文件夹(根文件夹之外),如下所示

System.IO.File.Move(strPhysicalFolder+ tpfile,strPhysicalFolder+fName);
System.IO.File.Move(strPhysicalFolder + fName, filePath + fName);
System.IO.File.SetAttributes(filePath + fName, FileAttributes.Normal);
现在,每当我试图删除该文件时,就会出现一个错误(访问被拒绝)。 下面是我的代码:

string strFileFullPath = srcPath + filename;
if (System.IO.File.Exists(strFileFullPath))
{
    System.IO.File.Delete(strFileFullPath);
}

strFileFullPath
包含我无法删除的文件的路径。在删除(设置属性)之前是否需要执行任何操作?任何帮助都将不胜感激。多谢各位

听起来很愚蠢,但可能是文件夹的权限问题

可能是以下情况:

System.IO.File.SetAttributes(strFileFullPath, FileAttributes.Normal);
System.IO.File.Delete(strFileFullPath);

谢谢你,汤姆。这起作用了。但是,我可以知道设置文件属性的原因吗?这是我前一段时间错误发现的东西,我假设您也像我一样,可能正在将此文件移动到您的'C:'上的某个位置,如果是这种情况,那么文件属性将被提升,您不能(通过您的应用程序)直接进入并删除。因此,您需要在删除之前更改此项。是的,确切地说,我正在移动到c,因为每当我将文件保存在根文件夹中时,用户都可以直接访问它。因此,我正在将所有文件移出根文件夹。如果在资源管理器中转到该文件,会发生什么情况。您真的可以从代码之外的新位置删除它吗?旁注,但一定要查找
System.IO.Path.Combine()
没有足够的信息。你在移动和删除之间做了什么?如果是:什么。另外:重命名和移动可以一步完成。另外:最好使用
System.IO.Path.combined
instad字符串连接。您确定所有路径(包括路径分隔符)都正确吗?谢谢。根据汤姆的回答,它正在工作。我现在就放联合收割机。谢谢这个问题不是很清楚。我无法判断您要删除的文件是否/如何与Move()相关。是否要删除原始目标?关于下一个问题,请先看这一页。