Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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#_Asp.net Web Api - Fatal编程技术网

C# 如何防止创建多个文件以及删除实体零件文件的最佳方法

C# 如何防止创建多个文件以及删除实体零件文件的最佳方法,c#,asp.net-web-api,C#,Asp.net Web Api,我使用下面的代码通过ASP.NET web api上传zip文件 string root = HttpContext.Current.Server.MapPath("~/uploads"); var provider = new MultipartFormDataStreamProvider(root); var content = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(t

我使用下面的代码通过ASP.NET web api上传zip文件

string root = HttpContext.Current.Server.MapPath("~/uploads");
var provider = new MultipartFormDataStreamProvider(root);
var content = new StreamContent(HttpContext.Current.Request.GetBufferlessInputStream(true));
string newfilename = string.Empty;
foreach (var header in Request.Content.Headers)
{
    content.Headers.TryAddWithoutValidation(header.Key, header.Value);
}

await content.ReadAsMultipartAsync(provider);
foreach (MultipartFileData file in provider.FileData)
{
    string filename = file.Headers.ContentDisposition.FileName.Replace("\"", "");
    newfilename = root + "\\" + filename;
    FileInfo newFi = new FileInfo(newfilename);

    newfilename = root + "\\" + newFi.Name.Replace(newFi.Extension, "") + Guid.NewGuid() +newFi.Extension;
    if (!File.Exists(newfilename))
    {
        try
        {
            File.Move(file.LocalFileName, newfilename);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}
问题是我一直在上传目录上有多个zip文件(在单个zip文件上传上)。有没有办法防止这种情况发生?我使用Guid.NewGuid()来确保如果其他用户上载了相同的文件,它不会出错。另一个问题是什么是清理车身零件文件的最佳方法。有没有办法自动删除它们

并不是说文件上传后,我正在处理文件