Test performed on SP2010 Enterprise. Documents create using Word 2010 as .docx files.
This is the code used to extract the document in the last test:
using System.IO;
using Microsoft.SharePoint;
namespace ExtractDocument
{
class Program
{
static void Main(string[] args)
{
using (var site =new SPSite("http://sp2010"))
using (var web = site.OpenWeb())
{
var doclib = web.GetList("MyDocLib") as SPDocumentLibrary;
foreach (SPFile source in doclib.RootFolder.Files)
{
File.WriteAllBytes(string.Format("C:\\testdocs2\\{0}", source.Name), source.OpenBinary());
}
}
}
}
}
And this is the code used to create documents based on the content from the file system:
using System;
using System.Collections;
using System.IO;
using Microsoft.SharePoint;
namespace GenerateDocuments
{
class Program
{
static void Main(string[] args)
{
using (var site = new SPSite("http://sp2010"))
using (var web = site.OpenWeb())
{
var doclib = web.GetList("MyDocLib") as SPDocumentLibrary;
foreach (var sourceName in Directory.GetFiles("C:\\testdocs"))
{
var source = new FileInfo(sourceName);
Console.WriteLine(source.Name);
doclib.RootFolder.Files.Add(source.Name, File.ReadAllBytes(sourceName), new Hashtable { { "Property1", "From Create" }, { "Property2", "From Create" } });
}
}
}
}
}