UnauthorizedAccessException when downloading a file using the TFS SDK - c#

When I try to download a file from TFS version control SDK to my computer I receive an 'UnauthorizedAccessException' saying Access to the local path I'm trying to download to is denied. I included a stripped down version of the code I am using below.
var projectCollection = GetProjectCollection();
var versionControl = (VersionControlServer)projectCollection.GetService(typeof(VersionControlServer));
versionControl.DownloadFile('$/path to file', 'local path to download to');
Does anyone know how to resolve this issue?

I found the issue.
The second argument in DownloadFile() needs to be the file name it will be downloaded as and not the parent directory it will be placed in. I thought it just needed the directory name.
So instead of what I originally had
versionControl.DownloadFile("$/Readme.txt", "C:\\Temp");
it needs to be
versionControl.DownloadFile("$/Readme.txt", "C:\\Temp\\Readme.txt");

This is because the process does not have rights to the local path. Make sure the local path has the appropriate right set to the user that is running the process.

Related

Access to Path Denied - Magick.NET.net40.7.4.3.0

I am trying to read text from images using IronOCR. It worked fine on my dev machine but when I try run it through IIS on another machine I am getting the following error
Access to the path 'C:\Windows\TEMP\Magick.NET.net40.7.4.3.0\Magick.NET-Q8-x64.Native.dll' is denied.
there is no Magick.NET.net40.7.4.3.0 folder in the window temp folder location on the dev machine, but it was there on the other machine but it was empty. I deleted the folder and now it is saying there is access denied on the user app_data folder.
I am totally confused as to why it is looking for access to Magick.NET.net40.7.4.3.0, is there some connection between the 2?
In your code you'll need to set :
IronOcrInstallation.InstallationPath = "d:\newpath"
where d:\newpath is a directory on your IIS that your worker process or IIS has access to.
BTW IronOCR is a commercial re-bundling of the open source Magick.NET image and Tesseract OCR libraries.

a file cannot be deleted and copy and move do not work in C# VS2013

I am trying to copy a file in same folder from C# VS2013 on win7.
string myFile = #"C:\Temp\MyFile.txt"
if (File.Exists(myFile))
{
File.Delete(myFile);
}
File.Move(myFileSource, myFile);
I got error:
Additional information: Cannot create a file when that file already exists.
I checked the folder and found that the file "myFile.txt" is still there after deleting.
If i used:
File.Copy(myFileSource, myFile, true);
Error:
Additional information: Access to the path 'C:\Temp\myFile.txt' is denied.
Why ? thanks
Run visual studio as administrator. It's likely a security issue related to UAC.
You may also want to consider writing the file to somewhere where is will work for all such as:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Selenium driver location search path

I'm trying to set up selenium tests in nCrunch which outputs the tests to its own temp folder.
I'm getting the following error when I create the driver (new PhantomJSDriver()):
OpenQA.Selenium.DriverServiceNotFoundException : The PhantomJS.exe file does not exist in the current directory or in a directory on the PATH environment variable.
However I have checked and PhantomJS.exe does exist in the current directory (\bin\debug).
I then tried using new PhantomJSDriver(".\\") which should be the current directory and that does work.
What is the "current directory" Selenium is referring to in this message?
Rather than assuming ".\\", get the current working working directory by Directory.GetCurrentDirectory or System.AppDomain.CurrentDomain.BaseDirectory . Take a look at Get current folder path.
new PhantomJSDriver() will use your bin folder
if PhantomJS.exe does not exist there, try to find where it's located and insert full path in constructor
new PhantomJSDriver("real_path_to_PhantomJS.exe")

Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\TextFiles\ActiveUsers.txt'

I tried many ways to access a text file in my Visual Studio 2012 Solution from a folder named TextFiles
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"~/TextFiles/ActiveUsers.txt", true))
{
file.WriteLine(model.UserName.ToString());
}
But it kept on throwing the error
Could not find a part of the path 'C:\Program Files (x86)\IIS
Express\~\TextFiles\ActiveUsers.txt'.
Not sure where I made a mistake
You need to use HttpServerUtility.MapPath which will turn the ~/ portion of the path in to the real location it resildes on your hard drive.
So that would change your code to (assuming you are in one of the IIS classes that expose a Server property to it's methods)
var path = Server.MapPath(#"~/TextFiles/ActiveUsers.txt");
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true))
{
file.WriteLine(model.UserName.ToString());
}
I ran into a similar issue and ended up using
string sFileName = HttpContext.Current.Server.MapPath(#"~/dirname/readme.txt");
This is an old question but I just ran into this problem myself and wanted to add what I've just discovered, in case it's helpful to anyone else.
If you have UAC turned off but are not running with elevated permissions, and try to write to restricted files (e.g. the "Program Files" folder) you'll get the "could not find a part of the path" error, instead of the (correct) access denied error.
To eliminate the problem, run with elevated permissions as in this solution: https://stackoverflow.com/a/1885543/3838199
~ is not the "user home" or anything else in Windows. You can still set the path as relative to the working directory (where the executable is) by just not specifying a full path.
For .netcore 3.x
You should make use of IWebHostEnvironment using dependency injection.
You can then use it in your code this way
string wwwRootPath = _hostEnvironment.WebRootPath;
string path = Path.Combine(wwwRootPath, $"TextFiles{Path.PathSeparator}ActiveUsers.txt");
Ensure to use PathSeparator otherwise you might face the same error due to the variance in your hosting environment.

TFS access problems through c# web app

I'm trying to build a web application in C# that can get the latest version of a file from a tfs server. That file with then be edited and etc...
Right now I get permission errors when trying to download a file using
IIdentity WinId = HttpContext.Current.User.Identity;
WindowsIdentity wi = (WindowsIdentity)WinId;
WindowsImpersonationContext wic = wi.Impersonate();
that code. After that I do a connection to the TFS Team Project Collection using:
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new
Uri("http://tfs:8080/tfs/DefaultCollection"));
which works fine. I can use PendEdit, get the workspace, get pending changes, everything. However I cannot do:
SourceControl.DownloadFile(serverFilePath, localFilePath);
I have tried a local path both in my workspace and in a newly created workspace both with same result
I will always get permission denied (To be exact I get this -> TF30063: You are not authorized to access Microsoft-IIS/7.5.) SourceControl is defined properly and I have full permission to my local folder and on TFS. I can get latest by right clicking the file and saying get latest I just can't do it in the app.
Any suggestions/problems with what I'm doing?
If I can't do this using just the impersonate stuff how do you prompt for credentials each time?
SourceControl.DownloadFile(serverFilePath, localFilePath);
The localFilePath that you are mentioning above, is it same as the Workspace path or some other path? Can you try to mention the workspace path of the file and try the method?

Categories