gpt4 book ai didi

c# - 获取 “Could not find part of the path” 错误

转载 作者:太空狗 更新时间:2023-10-30 00:10:18 26 4
gpt4 key购买 nike

我在我的 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));

最佳答案

尝试:

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

您需要根据 Server.MapPath(filePath); 检查和创建目录,而不是 filePath(我假设您的 srcbb 是没有任何子目录路径的文件名。

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

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

关于c# - 获取 “Could not find part of the path” 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com