【问题标题】:File.Delete() not working in run mode but only works in debug modeFile.Delete() 不能在运行模式下工作,但只能在调试模式下工作
【发布时间】:2012-04-09 11:36:18
【问题描述】:

我遇到了一个奇怪的问题。 我将上传的文件保存到数据库,然后尝试删除上传的文件 从上传文件夹。

这在调试模式下工作正常,但在运行模式下,文件保持未被删除。

有人遇到过这个问题吗?

这是 .NET 4

下面的代码sn-p:

private string SaveFiles(string rootFolder)
{
  var uploadedPhotos = GetAllFilesUploaded();
foreach (var file in uploadedFiles)
{
                string path= Path.Combine(rootFolder, "userfile", file.FileName);

                FileService.SaveUploadedFile(fileName, GetBytesFromLocalFile(path));

                File.Delete(path); <-- this only works in debug mode!!

                }
    }

    public static byte[] GetBytesFromLocalFile(string filePath)
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    byte[] bytes = new byte[fs.Length];
                    fs.Read(bytes, 0, (int)fs.Length);
                    return bytes;
                }
            }

【问题讨论】:

  • 我遇到了同样的问题。你解决过这个问题吗?

标签: .net c#-4.0 file-io


【解决方案1】:

IMO 因为它在调试模式下工作,所以这不是编码问题。问题在于为 File.Delete(path) 提供的路径。由于符合http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx

If the file to be deleted does not exist, no exception is thrown.

在发布模式下检查路径。可能与 bin 文件夹中的 Release 和 Debug 文件夹有关。

【讨论】:

    【解决方案2】:

    为了补充 Nikhil 的答案,我建议将 MessageBoxpath 置于发布模式并手动检查路径是否正确。

    注意:不要忘记删除之后的MessageBox

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多