Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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_File_File Upload_Directory - Fatal编程技术网

C# 获取“;找不到路径的一部分”;错误

C# 获取“;找不到路径的一部分”;错误,c#,asp.net,file,file-upload,directory,C#,Asp.net,File,File Upload,Directory,我正在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_Projec

我正在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
我相信我的文件路径有问题。 这是我代码的一部分:

 //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));
尝试:

您需要基于
Server.MapPath(filePath)检查并创建目录
,而不是
filePath
(我假设您的
src
bb
是没有任何子目录路径的文件名)

最好使用而不是串联字符串:

f_sourceCode.SaveAs(Path.Combine(severFilePath,src));
f_poster.SaveAs(Path.Combine(severFilePath,bb));

我同意使用Path.Combine。它工作得很好。谢谢。不需要检查目录是否存在
如果目录已经存在,此方法不会创建新目录,但会为现有目录返回DirectoryInfo对象
@Necronomicron:很好,我在复制代码时没有注意到这一点。
f_sourceCode.SaveAs(Path.Combine(severFilePath,src));
f_poster.SaveAs(Path.Combine(severFilePath,bb));