2015-02-01 3 views
2

웹 응용 프로그램에서 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 

내 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)); 

답변

10

시도 :

당신은 Server.MapPath(filePath);에 따라 디렉토리를 확인하고 작성해야
//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)); 

하지 filePath (나는 당신의 srcbb이 파일 이름은 어떤 않고 있다고 가정 하위 디렉토리 경로).

이 합치 문자열 대신 Path.Combine를 사용하는 것이 좋습니다 :

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

나는 Path.Combine를 사용에 동의합니다. 잘 작동한다. 감사. – Enovyne

+1

디렉토리가 있는지 확인할 필요가 없습니다. '디렉토리가 이미 존재하면이 메소드는 새로운 디렉토리를 작성하지는 않지만 기존 디렉토리의 DirectoryInfo 오브젝트를 리턴합니다. 'https://msdn.microsoft.com/en-us/library/54a0at6s(v=vs.110).aspx – Necronomicron

+0

@Necronomicron : 코드를 복사 할 때 잘 알지 못했습니다. –

관련 문제