获取"找不到路径的一部分"错误

Eno*_*yne 4 c# asp.net directory file-upload file

我在我的Web应用程序中使用FileUploader控件.我想将文件上传到指定的文件夹中.由于特定文件夹尚不存在,我必须在我的代码中创建它的路径.

Could not find part of the path.
mscorlib.dll but was not handled in user code

Additional information: Could not find a part of the path
'C:\Users\seldarine\Desktop\PROJ\ED_Project\SFiles\Submissions\blueteam\Source.zip
Run Code Online (Sandbox Code Playgroud)

我相信我的filePath存在问题.这是我的代码的一部分:

 //teamName is a string passed from a session object upon login 
 string filePath = "SFiles/Submissions/" + teamName+ "/";

 //If directory does not exist
 if (!Directory.Exists(filePath))
 { // if it doesn't exist, create

    System.IO.Directory.CreateDirectory(filePath);
 }

 f_sourceCode.SaveAs(Server.MapPath(filePath + src));
 f_poster.SaveAs(Server.MapPath(filePath + bb));
Run Code Online (Sandbox Code Playgroud)

Kha*_* TO 12

试试:

//teamName is a string passed from a session object upon login 
 string filePath = "SFiles/Submissions/" + teamName+ "/";
 string severFilePath = Server.MapPath(filePath);
 // The check here is not necessary as pointed out by @Necronomicron in a comment below
 //if (!Directory.Exists(severFilePath))
 //{ // if it doesn't exist, create

    System.IO.Directory.CreateDirectory(severFilePath);
 //}

 f_sourceCode.SaveAs(severFilePath + src));
 f_poster.SaveAs(severFilePath + bb));
Run Code Online (Sandbox Code Playgroud)

你需要检查和创建基于目录Server.MapPath(filePath);,而不是filePath(我假设你srcbb没有得到任何子目录路径文件名).

最好使用Path.Combine而不是连接字符串:

f_sourceCode.SaveAs(Path.Combine(severFilePath,src));
f_poster.SaveAs(Path.Combine(severFilePath,bb));
Run Code Online (Sandbox Code Playgroud)