I am building an integration test with Selenium and need to upload a file. I have the file included in my project but I need the full path to enter into the browser, to check if the browser upload system works.
How can I get the full path of a file that is included in my project? The test will be run on several different machines from different locations.
You don't need to. Mark it as 'Copy To Output Directory' (right click in Solution Explorer and look at the props) and it will always be in your working dir and so you can use a relative path, e.g.:
new StreamReader("myFileThatIMarkedAsCopyAlwaysInVisualStudio.txt");
Related
I'm trying to load and save an xml file called Modules.xml in my code. I have currently got the file path hardcoded as shown below. I am trying to get the file path within my code without it being hardcoded.
I have tried using Path.GetDirectoryName and new FileInfo("Modules.xml").Directory.FullName. However, both of these target the file in my debug folder, when the file I need is in the main solution folder.
Is there a way to target the file in my main solution folder instead of my debug folder? (both files are called Modules.xml)
doc.Save("C:\\Users\\Matthew\\Desktop\\Year4\\Object Oriented\\Project1\\Project1\\Modules.xml");
Both file locations are shown below:
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\Modules.xml
^^^this is the file path I need for my code^^^
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\bin\Debug\Modules.xml
The best approach here would be to use a configuration file, e.g. app.config, for storing such a string. Then you can change file path without recompiling the code, and your file can be stored in any location accessible by application.
If you really want to access your file the way you explained, AppDomain.CurrentDomain.BaseDirectory will provide you with the bin/Debug location in runtime. Then you can find a relative path from there like:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, #"..\..\", fileName);
where fileName is "Modules.xml" for example.
I have tried using Path.GetDirectoryName and new
FileInfo("Modules.xml").Directory.FullName. However, both of these
target the file in my debug folder, when the file I need is in the
main solution folder.
That's because bin\Debug is your working directory when you start and run the project. To change that, you can set the working directory environment variable to point to your solution directory (instead of bin\debug|release) which I wouldn't recommend that. Because when you finally endup with development, and release the application, there wouldn't be any solution directory that holds your XML file. What I can suggest is to copy your XML file to the output folder. Either you are in development (debug) or production (release) mode, the XML always going to be copied to final directory. And you can access the working directory with something like AppDomain.CurrentDomain.BaseDirectory. To enabling copy XML to output directory, right-click on it, choose Properties, set Build Action to None, and set Copy to Output Directory to Copy Always or Copy if newer. You're good to go now.
In my solution I have multiple projects and in Project A I want to System.IO.File.ReadAllText(string directory) from a file in project B however I do not want to hardcode the path as this will change once deployed on the hosting server. I've set the Copy Always on the file but cannot use the Path.Combine(GetCurrentDirectory) as the directory will be different, it will be from a different Project in my Solution file. How can I go around this?
Ive also tried
System.String.Format(#"{0}bin\{1}", System.AppDomain.CurrentDomain.BaseDirectory, filename);
however the problem is that the file in project b is not the BaseDirectory.
I have a folder in my main project that is separate than the Resources folder but also has some resources it that I would like to copy to the output folder as I reference it throughout the app.
I have set the build action to Resource, content, embedded content.. tried them all..
Also set it to always copy to output directory.
Now, from within my application I'm entering
AppDomain.CurrentDomain.BaseDirectory + "Dashboard\\Role.xml";
or also
"..\\..\\Dashboard\\Role.xml";
for the path and get an exception that says that the file can not be found.
Both of these paths work in my development machine but not once I deploy through click once.
I have tried to add it to the application files in the publish section as suggested in another post but it does not appear there.
I have also tried to put it in the resources folder and still nothing.... any ideas?
I followed some steps in this link and got it working:
https://msdn.microsoft.com/en-us/library/kzy0fky2.aspx
Basically, you mark the file as build action "Content" and set it to always copy.
Then, you go to the application files in the publish section of your clickonce application and they will now show up.
Switch them from datafile to "Include" and you're set!
I had a folder on my desktop with files in it. I copied that into the folder of my solution and in the solution explorer I referenced that folder into the solution. However, Im not able to open files in that folder with a relative path.
The relative path from the cs-file would be "../FolderIAdded/blabla" as seen in the solution explorer. But in the windows explorer, the path is differen of course:
Solutionfolder
- SolutionFolder.sln
- Solutionfolder.v11.suo
- SolutionFolder
-- bin
-- obj
-- Properties
-- TheFolderIAdded
-- App.config
-- Form1.cs
-- etc.
Here, it would be "FolderIAdded/blabla"
Where do I have to put that folder?
My goal: I want to be able to open files from that folder in my c#-code with a relative path.
You're assuming that your program runs in the directory where your source code is located. That's not the case. Depending on your configuration, your program will execute from a directory inside Solutionfolder\bin.
One possible solution is to copy the file(s) to the output directory when you build your project.
Another alternative is to embed the files into your application's assembly at compile time, although this precludes editing of them after deployment. To do that, set Build Action to 'Embedded Resource', then you can access them using the GetManifestResourceStream method of the Assembly class. The filename you need to give it will be derived from the path within the project structure, so in your example it would be "TheFolderIAdded.Filename.ext".
Yes, that's a dot, not a backslash.
Assuming the files are embedded in the same assembly the code that wants to read them is in, the code will look something like
var assembly = Assembly.GetExecutingAssembly();
using (var stream =
assembly.GetManifestResourceStream("TheFolderIAdded.Filename.ext"))
using (var reader = new StreamReader(stream)) {
string fileContents = reader.ReadToEnd();
}
I don't think it's a good idea to write relative path from .cs file. Better build the path base on where the application is executed:
One example, there are plenty other on the web: How can I get the application's path in a .NET console application?
(Your application is not running in the solution's root folder but where the .exe file is locatated. For example when you debug a desktop application, it runs typically from [solution folder]/bin/debug/ )
Then make sure the file you want to open property Copy to Output Directory is set to Copy Always or Copy if newer. (Right click on the file in your Solution Explorer and click on "Properties" to be sure to access it.)
I have various files in my Visual Studio Solutions that have to be copied to the bin/Debug folder if I change it.
I tried to set Copy to Output Directory - Copy always but it doesnt work. So how can i make sure that when building these files are copied to bin/debug?
Here a screenshot from one example:
Make sure the build action for the files are marked as content, otherwise they will not be copied while building.
Content - The file is not compiled, but is included in the
Content output group. For example,
this setting is the default value for
an .htm or other kind of Web file.
Are you using the Debug profile? The build section of your properties has an output path which is set to bin/debug if you are using this profile.