File.Delete()不能在运行模式下工作,但只能在调试模式下工作 [英] File.Delete() not working in run mode but only works in debug mode

查看:204
本文介绍了File.Delete()不能在运行模式下工作,但只能在调试模式下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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



任何人都遇到过这个问题?

这是.NET 4



下面的代码片段:
$ b

  private string SaveFiles(string rootFolder)
{
var uploadedPhotos = GetAllFilesUploaded();
foreach(var文件在uploadedFiles)
{
字符串路径= Path.Combine(rootFolder,userfile,file.FileName);

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

File.Delete(path); < - 这只能在调试模式下工作!



$ 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);
返回字节;


$ / code $


解决方案

添加到Nikhil的答案,我会建议在释放模式下将 MessageBox 路径并手动检查是否路径是正确的。



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


I am having a weird problem. I save uploaded file to database and then try to delete the uploaded file from upload folder.

This works fine in debug mode but in run mode, the file remains undeleted.

Anyone faced this issue?

This is .NET 4

Code snippet below:

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;
                }
            }

解决方案

To add to Nikhil's answer, I'd suggest to put a MessageBox with the path in release mode and manually check if the path is right.

NOTE: Don't forget to remove the MessageBox after.

这篇关于File.Delete()不能在运行模式下工作,但只能在调试模式下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆