Getting “Could not find part of the path” error
41,554
Try :
//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));
You need to check and create directory based on Server.MapPath(filePath);
, not filePath
(I assume that your src
and bb
are filenames without any sub directory paths).
It's better to use Path.Combine instead of concatenating strings:
f_sourceCode.SaveAs(Path.Combine(severFilePath,src));
f_poster.SaveAs(Path.Combine(severFilePath,bb));
Comments
-
Enovyne almost 2 years
I am using the FileUploader control in my web application. I'd like to upload the file in a specified folder. As the specific folder does not exist yet, I have to create the path it in my codes.
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
I believe there is a problem with my filePath. This is a portion of my codes:
//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));
-
Enovyne about 9 yearsI agree with using Path.Combine. It works well. Thanks.
-
Necronomicron over 6 yearsNo need to check if directory exists.
If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory.
msdn.microsoft.com/en-us/library/54a0at6s(v=vs.110).aspx -
Khanh TO over 6 years@Necronomicron: Good catch, I did not notice that when copying the code.