当我在上载文件后尝试移动目录时,出现错误。我认为发生这种情况是因为上传后文件没有关闭,但是由于我使用的是httppostedfileBase
,所以找不到关闭它的方法。
以下是上传代码:
public void Create(httppostedfileBase fileToUpload)
{
using (var context = new MyContext2())
{
UserManager<ApplicationUser> manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); ;
ApplicationUser currentUser;
currentUser = manager.FindById(HttpContext.Current.User.Identity.GetUserId());
if (currentUser != null)
{
string projectPath = Configurationmanager.AppSettings["ProjectSavingDirectory"];
string fullPath = "Files/Hotels/";
string completePath = Path.Combine(projectPath,fullPath);
if (fileToUpload != null && fileToUpload.ContentLength > 0)
{
var fileName = Path.GetFileName(fileToUpload.FileName);
var tmppath = Path.Combine(completePath + currentUser.snew + "/Images",fileName);
fileToUpload.SaveAs(tmppath);
}
}
}
}
还有我要移动目录的部分:
public void accountEdit(ApplicationUser theUser,string sOld)
{
using (var context = new MyContext2())
{
theUser.username = theUser.Email;
theUser.LockoutEnabled = true;
if (theUser.snew != sOld)
{
int i = 0;
string projectPath = Configurationmanager.AppSettings["ProjectSavingDirectory"];
string fullPath = "Files/Hotels/";
string completePath = Path.Combine(projectPath,fullPath);
while (Directory.Exists(completePath + theUser.snew + "/" + i))
{
i++;
}
if (!Directory.Exists(completePath + derUser.snew))
Directory.CreateDirectory(completePath + derUser.snew);
try
{
// throws IO Exception
Directory.Move(completePath + sOld + "/" + i + "/",completePath + theUser.snew + "/" + i + "/");
}
catch (IOException e)
{
Console.Write(e);
}
}
}
}
我遗漏了一些不重要的代码,不得不翻译一些;路径起作用,并且找到正确的路径,这不是问题。 重新启动项目后,只要不事先上传任何内容,我就可以移动目录。
我不知道如何解决此问题,我们将不胜感激!