tags:

views:

808

answers:

3

I have the following situation.

I programmatically create a temporary workspace using TFS. I then map it to a spot on my local machine so that I can be able to checkin/checkout files. Since the mapping to the local drive through the workspace is what creates the file structure. What is the way to delete the mapping through the workspace object that I created?

Ive tried the following.

WorkingFolder tempFolder = workspace.getWorkingFolderForServerItem(serverItem);
workspace.DeleteMapping(tempFolder);

Stepping through in debug mode, the tempFolder Object I make holds the correct local mapping as well as the correct server mapping. I cant seem to get it to delete the local content though. Is this mostly correct or do you suggest something completely different?

+1  A: 

Since the mapping to the local drive through the workspace is what creates the file structure.

I think you have this wrong. The local folders (and files) are created only when you perform the get after the mapping is created (whether from the Team Explorer GUI, "tf.exe get", or otherwise).

After deleting the workspace mapping, you will need to create code to delete the files and folders yourself.

Richard
Thank you for correcting me on when the local content is actually pulled down.
maleki
A: 

Thanks to Richard, I decided to not try and delete the file through the workspace.

Given:

WorkingFolder tempFolder = workspace.getWorkingFolderForServerItem(serverItem);

I ended up doing:

File.setAttributes(tempFolder.LocalItem, FileAttributes.normal)//Get rid of read-only
File.Delete(tempFolder.LocalItem);

Thanks for the help!

maleki
+3  A: 

In TFS, the trick to deleting files locally and telling the server that you do not have them anymore is to get the files at Changeset 1 (i.e. before they existed). In code that would be something like:

workspace.Get(
    new string[] {"C:\\LocalPath"},
    new ChangesetVersionSpec(1),
    RecursionType.Full,
    GetOptions.None);

See the following blog post where I explain this concept some more:

That said, if the workspace is just temporary and you do not need it anymore then doing a workspace.Delete() followed by a traditional file delete is a perfectly good way of doing things. If you were trying to keep the workspace around you could get into trouble though (because TFS thinks those files are still in your local workspace unless you tell it that they are not)

Martin Woodward