Search code examples
c#visual-sourcesafe

How to write to file in VSS from c# code?


I have a project with Visual Source Safe (VSS) and I need to append lines to a ".sql" file. I am trying to use the obvious code:

using (StreamWriter sw = File.AppendText(lblSourceFile.Text))
{
    sw.WriteLine("text here");
}

But I am getting "File Access" error because the file is not checked out in the VSS. Is there a way to programmaticlly check-out the file for edit and check it back in when finish ? All links I found are using VSSDatabaseClass, and I don't how to add reference for SourceSafetypelib dll and use VSSDatabaseClass...


Solution

  • Thanks to Rachel for the ssapi.dll file. Here is the code that worked for my:

     using SourceSafeTypeLib;
     VSSDatabaseClass vssDatabase = new VSSDatabaseClass();
     vssDatabase.Open("VSS database path", "userName", "password");
     VSSItem item = vssDatabase.get_VSSItem("file path as shown in vss", false);
     item.Checkout("Comments", item.LocalSpec, 0);
     using (StreamWriter sw = File.AppendText(lblSourceFile.Text))
     {
         sw.WriteLine("text");
     }
     item.Checkin("Comments", item.LocalSpec, 0);
     vssDatabase.Close();