如何返回字符串的路径?

时间:2011-12-04 18:19:57

标签: c# string return

我想问一下如何返回字符串的路径?我有这个错误消息

  

'_ Default.uploadfile(string,string)':并非所有代码路径都返回值

private string uploadfile(String filetype, String Uploadfilename)
{
    String csFileUploadName;
    if (filetype == "FiUptrack")
    {
        csFileUploadName = "C:\\MusicStore\\uploads\\promos\\" + Uploadfilename;
    }
    else
    {
        csFileUploadName = "C:\\MusicStore\\uploads\\images\\" + Uploadfilename;
    }        

    try
    {
        FiUptrack.SaveAs(csFileUploadName);
        lblRequired.Text = "Upload status: File uploaded!";
        return csFileUploadName.ToString();
    }
    catch (Exception ex)
    {
        lblRequired.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
    }
}

用法:

  csArtworkupload = uploadfile("Artworkupload", FiUptrack.FileName);

csArtworkupload是一个字符串

2 个答案:

答案 0 :(得分:4)

正如错误消息明确指出的那样,您的方法并不总是返回任何内容。

具体而言,如果抛出异常,则不会执行return语句。

答案 1 :(得分:1)

该错误是因为您没有在代码中的所有执行路径中返回 - 即某些分支不会导致返回。

尝试将其放在代码的末尾:

return csArtworkupload;