I am trying to upload a file with the sendkeys function on the inputid.
I am currently using the path C:\Users\myusername\Documents\seleniumsolution\Utils\Dir\Dir1\Dir2\Dir3\UploadFolder\example.jpg
I see the file in my solution. But I dont want to give to complete path but only the filename and that selenium finds the path itself. Otherwise I will be the only one that can use this testcase.
I tried with:
var file = Path.Combine(Directory.GetCurrentDirectory(), url);
And tried with:
string documents = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"UploadFolder");
Both of them dont give me the required result.
Hope that you guys can help me out.
You might need to travel on the folders tree to get to the file
AppDomain.CurrentDomain.BaseDirectory
will get you in the bin\debug folder. From there you can use parent to move up to the file location. For example (might need some tweaking)
string solutionParentDirectory = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName;
string file = Path.Combine(solutionParentDirectory, #"UploadFolder\example.jpg");
You can also try
string file = Path.GetFullPath("example.jpg");
Related
I've got an absolute path available to me. Say: C:/Program/CoreFiles/Folder1/Folder2/File.txt.
I need to copy that file to C:/Program/Projects/ProjectName/ but it needs to keep Folder1/Folder2/File.txt intact. So the end result should be C:/Program/Projects/ProjectName/Folder1/Folder2/File.txt.
My first attempt at solving this was to try and get the relative path between 2 absolute paths. Found Path.GetRelativePath(string, string) which obviously didn't help as it wasn't meant for WinForms. It would mess up anyway as the final result would be C:/Program/Projects/ProjectName/CoreFiles/Folder1/Folder2/File.txt.
The target directory is empty and I don't know the relative path to copy beforehand other than somehow getting that info out of the absolute path. Since File.Copy won't create folders that don't exist yet, I need to create them first. So how do I get the path that leads up to the file from the CoreFiles directory out of the absolute path?
The only working solution I can come up with is using regex to just replace CoreFiles with Projects/ProjectName in the path string and work with that. But that somehow seems the wrong approach.
Since you can't use Path.GetRelativePath. I suggest looking at another answer that describes how to do this yourself.
Like here...
How to get relative path from absolute path
Using the method in that answer, you can do the rest of your task as shown below.
string sourcePath = "C:/Program/CoreFiles/Folder1/Folder2/File.txt";
string sourceRoot = "C:/Program/CoreFiles/";
string destinationRoot = "C:/Program/Projects/ProjectName/";
// Use built-in .NET Path.GetRelativePath if you can. Otherwise use a custom function. Like here https://stackoverflow.com/a/340454/1812944
string relativePath = MakeRelativePath(sourceRoot, sourcePath);
// Combine the paths, and make the directory separators all the same.
string destinationPath = Path.GetFullPath(Path.Combine(destinationRoot, relativePath));
// Create nested folder structure for your files.
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
// Copy the file over.
File.Copy(sourcePath, destinationPath);
I'm trying to use C# to create a file, and read files back to fill out a rich text block. right now my problem is in creating/writing to the file.
FileStream fs = File.Create(#".\\tmp\" + fileName);
This is where I'm trying to write to. .\tmp\ exists, but when trying to write it it errors, saying
.\tmp\filename access is denied
The probably is that the user that is running the application probably doesn't have access to write to that directory. The easiest way to test that would be to run your application as administrator you should have access to write to that directory then.
You might also want to consider writing to the current directory no matter what user who is running your application should at the very least have access to that directory
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Probably you don't have access to the relative path.
To get your assembly directory:
private static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Then
FileStream fs = File.Create(Path.Combine(AssemblyDirectory, fileName));
You are using a relative path which leads to a location which you don't have access to.
A possible solution could be to:
Create a folder C:/data and make sure you have read and write rights to that folder
change the code to
string fileName = "file.txt";
FileStream fs = File.Create(#"C:/data/" + fileName);
This should create a file under C:/data with the filename "file.txt", assuming you have the correct read and write rights.
If you want a relative path to the current user's root directory, use:
string currentUserDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
This happened to me . I had had an anti-virus blocking access to any file when the writing or reading process happening from a C# program. I have just deactivated the anti-virus and the code worked like magic !
I'm trying not to hard code my path, but I have not been able to figure our a way to get to an xml file that I have included in my project under a folder labeled Datasource. Here is my latest code that I have tried which still doesn't work.
public static string myAssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
string fileName = xmlFileName;
string path = Path.Combine(myAssemblyDirectory, #"DataSource\" + fileName);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
Here is the output for the path that I'm getting which is putting it in my test results output folder.
"C:\MyAutomation\Automated_Test_Projects\AutomationProjects\MiserReleaseTestSuites\TestResults\marcw_ISD2005M 2016-02-05 10_15_17\Out\DataSource\Miser_Login_Dts.xml"
If possible I'd like to point it to
"C:\MyAutomation\Automated_Test_Projects\AutomationProjects\MiserReleaseTestSuites\MiserReleaseTestSuites\DataSource\Miser_Login_DTs.xml"
".." Can be used to go to the relative parent directory. "." Refers to the current directory.
You can combine these to form a relative path that starts higher up in the directory tree.
In your example you need to go 3 directories higher than the out folder and then into the MiserReleaseTestSuites\DataSource folder. Combining this produces
#"..\..\..\MiserReleaseTestSuites\DataSource\"
You can deploy the file in the same manner as you would when data driving the tests. See https://stackoverflow.com/a/25742114/546871
The TestContext class contains several fields with "directory" in their names. These can be used to access the various directories associated with running the tests. See also https://stackoverflow.com/a/19682311/546871
I am trying to get a folder path in my C drive that I did not want to hard code with my program.
I know that I can use string path = System.AppDomain.CurrentDomain.BaseDirectory; However its give me : C:\FACE\Camera\Camera\bin\Debug . Which I want to only have C:\FACE\ as I want to initialise something in the FACE folder but outside of my Camera folder. I do not wish to hardcode the file path. Is it possible to do it? Thanks for the help!
You could use the method Directory.GetParent for this purpose:
Directory.GetParent("here you will pass you path");
Update
For your case, you could take that you want as below:
string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string solutionPath = Directory.GetParent(projectPath).Parent.FullName;
string basePath = Directory.GetParent(solutionPath).FullName;
The variable basePath contains that you want.
System.AppDomain.CurrentDomain.BaseDirectory always returns the path of executable assembly.You can use DTE if you are using web application
I am trying to write out a text file to: C:\Test folder\output\, but without putting C:\ in.
i.e.
This is what I have at the moment, which currently works, but has the C:\ in the beginning.
StreamWriter sw = new StreamWriter(#"C:\Test folder\output\test.txt");
I really want to write the file to the output folder, but with out having to have C:\ in the front.
I have tried the following, but my program just hangs (doesn't write the file out):
(#"\\Test folder\output\test.txt");
(#".\Test folder\output\test.txt");
("//Test folder//output//test.txt");
("./Test folder//output//test.txt");
Is there anyway I could do this?
Thanks.
Thanks for helping guys.
A colleague of mine chipped in and helped as well, but #Kami helped a lot too.
It is now working when I have:
string path = string.Concat(Environment.CurrentDirectory, #"\Output\test.txt");
As he said: "The CurrentDirectory is where the program is run from.
I understand that you would want to write data to a specified folder. The first method is to specify the folder in code or through configuration.
If you need to write to specific drive or current drive you can do the following
string driveLetter = Path.GetPathRoot(Environment.CurrentDirectory);
string path = diveLetter + #"Test folder\output\test.txt";
StreamWriter sw = new StreamWriter(path);
If the directory needs to be relative to the current application directory, then user AppDomain.CurrentDomain.BaseDirectory to get the current directory and use ../ combination to navigate to the required folder.
You can use System.IO.Path.GetDirectoryName to get the directory of your running application and then you can add to this the rest of the path..
I don't get clearly what you want from this question , hope this get it..
A common technique is to make the directory relative to your exe's runtime directory, e.g., a sub-directory, like this:
string exeRuntimeDirectory =
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
string subDirectory =
System.IO.Path.Combine(exeRuntimeDirectory, "Output");
if (!System.IO.Directory.Exists(subDirectory))
{
// Output directory does not exist, so create it.
System.IO.Directory.CreateDirectory(subDirectory);
}
This means wherever the exe is installed to, it will create an "Output" sub-directory, which it can then write files to.
It also has the advantage of keeping the exe and its output files together in one location, and not scattered all over the place.