Rename TFS Branch Programaticly c# (TFS2017) - c#

I'm trying to rename a branch programaticly using "PendRename" method from Microsoft.TeamFoundation.VersionControl.Client.
Moves working, so, if I use this to move everything to a new location, it works, but what I need is to rename a branch.
It is possible to do this by commandline "tfs.exe rename " (even this is given me errors if I have more than one workspace mapped for the same server url. the ... could not be found in your workspace, or you do not have permission to access it.)
So, could you please help to understand why rename a branch is not working?
Thank you,

To Rename or move files and folders in TFVC, you must be one of the Contributors for your team project. See Team Foundation Server default groups, permissions, and roles.
I tested that and renamed the branch correctly with the "PendRename" method.
Below code sample for your reference:
using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace RenameBranch
{
class Program
{
static void Main(string[] args)
{
string oldPath = #"E:\andy\0418Scrum\web0418-0823";
string newPath = #"E:\andy\0418Scrum\web0418-1020";
string collection = #"http://server:8080/tfs/DefaultCollection";
var tfsServer = new Uri(collection);
var tpc = new TfsTeamProjectCollection(tfsServer);
var vcs = tpc.GetService<VersionControlServer>();
Workspace workspace = vcs.GetWorkspace("YourWorkspaceName", vcs.AuthorizedUser);
workspace.PendRename(oldPath, newPath);
}
}
}
Then you need CheckIn it of course. Use a "workspace.GetPendingChanges()" and "workspace.CheckIn()" methods to do it.

Thank you,
The problem was, there are different processes that are generating workspaces to the directory, and then, he can't remove it.
So, I solve this issue doing this steps:
1. reserve a workspace local location for my appĀ“
2. see if this workspace is mapped. If yes, I remove it
3. create a workspace to the previous folder in path and create the map
4. get latest
5. rename
Thank you Andy-MSFT for your support.

Related

Uncheckout tfs file programmatically if not modified using C#

After searching the googles for couple hours I found an answer to my question. I know this post Undo checkout TFS answers my question, however it doesn't answer all the questions I have. I want to achieve the same objective that the post asked about. How to only revert files that have been checked out if nothing was modified in that file? The answer to my question shouldn't be too hard to answer.
So what I'm doing is copying files from a server and overwriting them in my local workspace. I am checking out all the files being copied. However, if a file that was copied is not modified in anyway(server file and destination file are exact same), I'd like to undo the checkout of that file.
I know I'm to use the workspace.Undo() method and the gentleman said it worked for him. However he didn't show how he implemented it.
Here is the code I have with help from the link:
public static void CheckOutFromTFS(string filepath)
{
var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(filepath);
if (workspaceInfo == null)
{
return;
}
var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
var workspace = workspaceInfo.GetWorkspace(server);
workspace.PendEdit(filepath);
}
The answer given was to use the workspace.Undo() method. Do I add this method as the last line in CheckOutFromTFS() like so?
public static void CheckOutFromTFS(string filepath)
{
var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(filepath);
if (workspaceInfo == null)
{
return;
}
var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
var workspace = workspaceInfo.GetWorkspace(server);
workspace.PendEdit(filepath);
workspace.Undo();
}
Or is it done differently? I'm not sure if this Undo() will only revert files if there are no changes or just revert the checkout entirely and render the PendEdit() useless. Can someone help clarify this for me?
If you use a local workspace then all file that have no changes will automatically revert to not checked-out. You don't need to do anything at all. This works with VS 2012 or better with TFS 2012 or better. You'll need to convert you workspace to a local workspace first like this
So I found the answer to my question in various posts. I kinda took bits an pieces and combined them together to get my working solution. The use of the Undo() function with passing in the filepath actually does uncheckout the file regardless if it was modified or not. My workspace was also local but VS and TFS couldn't automatically revert those unmodified files for me so I took the below approach.
So what I decided to do was to just use the Team Foundation Power Tools "uu" command to undo the changes to unchanged files in the workspace. I created a batch file and entered the following command: echo y | tfpt uu . /noget /recursive. Since we will not show the shell during execution, I used the "echo y" command to automatically answer the question, "Do you wish to undo these redundant pending changes? (Y/N)". Including /noget is highly recommended since it prevents a forced 'get latest' of all your project's files which depending on the total number can take a extremely long time.
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WorkingDirectory = projectRoot,
FileName = projectRoot + #"\undoUnchanged.bat",
UseShellExecute = false,
CreateNoWindow = true
};
Process process = Process.Start(startInfo);
process.WaitForExit();
process.Close();
After the script runs and the process.Close() executes you and double check if your unmodified files actually were unchecked out by hitting the refresh button on the Team Explorer window in your project. Hope someone else can find some use in this.
If I understand the question well and you actually need undo through C# code behind, I believe this shoul help you:
Undo checkout TFS

Get TFS mapped folder of a local sub folder of the solution?

Let's say we have a solution in TFS Source Control which has already been mapped to a local folder SolutionFolder.
We are in a sub folder SubFolder of this SolutionFolder.
How can we write C# code to get the mapped path of this SubFolder?
Use the WorkStation.Current to grab the information for the folder in question:
Import the following namespaces:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
and then use you can get to the data you want through:
var workspace = Workstation.Current.GetLocalWorkspaceInfo(solutionFolder);
if (workspace != null)
{
var teamProjectUri = workspace.ServerUri;
// var server = TfsConfigurationServerFactory.GetConfigurationServer(teamProjectUri);
var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(teamProjectUri);
var cssService = projectCollection.GetService<ICommonStructureService4>();
var project = cssService.GetProjectFromName(solutionName);
}
From there you can easily grab the Workspace as well and from there the serverpath: workspace.GetWorkspace().GetServerItemForLocalItem()
To provide credentials, you can use one of the additional overloads that accepts a CredentialsProvider. The default provider is the UICredentialsProvider. Or you can also call server or projectCollection's EnsureAuthenticated.
See also:
https://jessehouwing.net/vsts-tfs-api-auto-detect-connection-details/

Saving image to a folder in IIS localhost

I have to save images to a folder located in "c:\inetpub\wwwroot\" and named as "UploadedImages". Here is my code:
public string SaveImage(string base64,int compno)
{
string res = "";
try
{
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64)))
{
using (Bitmap bm2 = new Bitmap(ms))
{
bm2.Save(Server.MapPath("~/UploadedImages/ID"+compno+".jpg"));
}
}
res = "done";
}
catch (Exception ex) {
res = ex.ToString();
}
return res;
}
but it throws "A generic error occured in GDI+ at System.Drawing.Image.Save" exception. What am I doing wrong? This code works fine when saving image locally as
bm2.Save("D:Embasy\UploadedImages\ID"+compno+".jpg"));
What changes do I need to make to save images in localhost directory?
Your not going to believe this -- the site running v1.1 had a virtual directory set-up which was mapped to the directory in which the image was saved to -- things worked fine.
The v2.0 site also had the same virtual directory name, but the physical path was different -- I changed the path to point to the same directory as the v1.0 site and now the code works.
So in short -- you were right about the "path must exist".
Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. For a supported alternative, see Windows Imaging Components.
and kindly refer this link
http://msdn.microsoft.com/en-us/library/xs6ftd89.aspx
When you are using Server.Mappath
bm2.Save(Server.MapPath("~/UploadedImages/ID"+compno+".jpg"));
"~(tield)" : is point to project/application root folder c:\inetpub\wwwroot\yourproject
then find remaining your path /UploadedImages and then create ID1.jpg.
But your imagefolder "UploadImages" is exist in d: not in c:\inetpub\wwwroot\yourproject
if you want to exist your image folder in D: then you should to create
virtual directory and then you need to apply path as relevant
My problem actually was that every time i published website, I replaced the "UploadedImages" folder too and thus permissions were changed. So, i didnt replaced the folder again after changing its permissions and creating "everyone" group and giving full rights to it. Now code is working perfectly :)

get latest specific project in C#

I have a project that should be get latest to specific date. I have this code:
var serverFolder = pathInTfs;
var localFolder = pathInLocalMachin;
var workingFolder = new WorkingFolder(serverFolder, localFolder);
// Create a workspace mapping
workspace.CreateMapping(workingFolder);
if (!workspace.HasReadPermission)
{
throw new SecurityException(
String.Format("{0} does not have read permission for {1}",
versionControl.AuthorizedUser, serverFolder));
}
// Get the files from the repository
workspace.Get(dateForLatest, GetOptions.Overwrite);
every thing is good but I want to be get latest only directory "pathInTfs" in "pathInLocalMachin" but when program run workspace.Get() every project be get latest.
How I can get latest one path in my project.
There are several overloads of Get which allow you to specify the set of objects you want to get. For what you're doing, I think you want Get(GetRequest, GetOptions).
The GetRequest includes an ItemSpec where you can specify a folder to get, and then indicate RecursionType.Full.

how to specify a different location for Virtual Directories in Web Setup Projects

I want to install my virtual directory other than the default location(c:\inetpub\wwwroot).
I want that the directory will be created in the physical path enntered by the user.
Please suggest any way to do that.
not exactly what you ask but follow the steps described in the main answer here: Virtual Directory in Web Setup Project
...and instead of changing TextBoxes to not visible keep them visible so you can capture user input.
Using the System.EnterpriseServices.Internal.IISVirtualRoot you can create a Virtual direcoty through C#,
System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot();
string sError = "";
vr.Create("IIS://localhost/W3SVC/1/Root", physicalPath, VDName, out sError);

Categories

Resources