I think the simplest code is this:
ClientContext context = new ClientContext("http://MySiteUrl");
using (FileStream fs = new FileStream(@"x:\MyFile.docx", FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/MyLibName/MyFolderName/MyFile.docx", fs, true);
}
So you just need to provide sub-folder url.
References:
File.SaveBinaryDirect Method
Uploading files using Client Object Model in SharePoint 2010
ADDITION:
The The remote server returned an error: (409) Conflict.
when running above code will occur if target folder doesn't exist.
To check if folder exist one can use:
ClientContext context = new ClientContext("http://MySiteUrl");
Folder f = web.GetFolderByServerRelativeUrl(listUrl + "/" + newDocSetName);
context .Load(f);
bool alreadyExists = false;
try
{
clientContext.ExecuteQuery();
alreadyExists = true;
}
catch { }
Code borrowed from Matt Rankin blog post:
SharePoint 2010 : Using The Client Object Model To Move Files And Folders Across Site Collections And Subsites Check it out!