Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting “Could not find part of the path” error

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));
like image 644
Enovyne Avatar asked Dec 14 '22 16:12

Enovyne


1 Answers

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));
like image 168
Khanh TO Avatar answered Dec 31 '22 13:12

Khanh TO