Monday, December 10, 2012

Often we require to create a folder(directory) on a local drive for certain operations such as copying files, renaming, deleting etc. Once a folder is created, we might require to write or copy a file to it.

Below are the sample codes for both operation :

Folder Creation
----------------------

Required Namespaces:
System.IO


    string path="C:\Mypath";
           
    if (!Directory.Exists(path))

    Directory.CreateDirectory(path);
Writing or Copying a File
-------------------------
Required Namespaces :

System
System.IO
Microsoft.SharePoint
Microsoft.SharePoint.Administration

//fold is an SPFolder object that contains document.
               foreach (SPFile file in fold.Files)
                    {
                        byte[] binFile = file.OpenBinary();

                        System.IO.FileStream fstream =System.IO.File.Create(path);

                        
                        fstream.Write(binFile, 0, binFile.Length);

                        //Console.WriteLine(file.Name);
                    }


// We can also use  foreach (SPItem item in myList.items) approach.