ShankarSangoli
ShankarSangoli

Reputation: 69915

How to checkout a file in TFS in Visual Studio Single File Generator?

I am writing a VS Single File Generator for VS 2013. In the generator I am trying to create/modify a new file in the parent folder. If the file which I am trying to create already exists and is read only(source controlled in TFS) then I am getting an error while trying to write anything into it.

I think I need to programmatically checkout the file before editing it. Can someone please provide any pointer on how can I achieve it?

I am also add this new file into the project file, I believe I will have to checkout the project file also before I can add to it.

Upvotes: 0

Views: 828

Answers (1)

ShankarSangoli
ShankarSangoli

Reputation: 69915

I solved it by checking if the file being modified is in the Source Control. If it is in the Source Control and not checked out then I checkout the file before trying to modify it. I created a below method in which dte is EnvDTE.DTE.

    private void CheckoutFileIfRequired(string fileName)
    {
        if (dte.SourceControl == null
            || !dte.SourceControl.IsItemUnderSCC(fileName)
                || dte.SourceControl.IsItemCheckedOut(fileName))
        {
            return;
        }
        Action<string> checkOutAction;
        checkOutAction = file => dte.SourceControl.CheckOutItem(file);
        // run on worker thread to prevent the tool calling back into VS
        checkOutAction.EndInvoke(checkOutAction.BeginInvoke(fileName, null, null));
    }

Upvotes: 1

Related Questions