I'd like to add a config.json file to my project and have a class read from the file.
I added the file in VS and added the Build Action = Content and Copy to output directory = Copy always properties.
When building, the file is copied to C:\...\mysolution\myproject\bin\Debug.
But File.ReadAllText(#"config.json") throws a FileNotFoundException.
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); doesn't return the folder where the file is copied:
C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\root\\2158bea8\\d46fc01c\\assembly\\dl3\\eca14761\\49a747bf_be72d001
"\config.json" will read from ROOT, try "config.json"
as Steve said, you need to use "config.json", this will read from where the exe or application resides.
So you need to tell VS to copy this file to your output directory when building, to this do the following:
Right click the file in VS > Properties > Set "Copy to output Directory" to "Copy Always"
Related
i tried to create access to file that loacated in main project folder, but i get error that says the file not found in another folder in project
System.IO.FileNotFoundException: Could not find file ,this is thepath which trying to find the file
C:\Users\User\source\repos\AutomationAssignment\AutomationAssignment\bin\Debug\netcoreapp3.1\DataEX.xlsx'
This is the path that i want to use
C:\Users\User\source\repos\AutomationAssignment\AutomationAssignment\DataEX.xlsx'
static string path = Path.Combine("DataEX.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(File.Open(path, FileMode.Open));
string url = System.IO.File.ReadAllText("Url.txt");
The spreadsheet should be copied to the build output directory, so it resides in the same folder that your test project DLL is in.
Add the spreadsheet to your test project so it shows up in the Solution Explorer panel in Visual Studio.
If you can already see the spreadsheet in Solution Explorer, skip this step.
Right-click on the spreadsheet in Solution Explorer, and choose "Properties".
Set the "Build Action" property to "None".
Change the "Copy To Output Directory" to "Copy Always".
You can set this to "Copy If Newer" but this typically looks at file modification dates. I'm paranoid, so I have it copy the file every time. This is fine for small files.
The spreadsheet should exist in the same folder as the DLL file running your tests. Now change your code:
var folder = Path.GetDirectoryName(GetType().Assembly.Location);
var path = Path.Combine(folder, "DataEX.xlsx");
var workbook = new XSSFWorkbook(File.Open(path, FileMode.Open));
When a form loads, I need to read a binary file under the /skubin folder and use it to populate a List. But I’m unable to open the file. When I do, I receive an error indicating that the file doesn’t exist.
Below is the snippet of my code which I am trying to read the file from the folder.
string startpath = Application.StartupPath;
string BinDir = Path.Combine(startpath, "skubin");
binNanme = Path.Combine(BinDir, "skuen.bin");
if (!File.Exists(binNanme))
{
MessageBox.Show("Load bin fail");
return;
}
When checking the BinDir value, instead of pointing to <project_root>/skubin, it's pointing to <project_root>/bin/Debug/skubin.
I am not understanding why it is pointing to /bin/Debug folder.
Right-click on the .bin files in the skubin folder within solution explorer and select properties. Set "Copy to Output Directory" to "Copy Always". This should solve your problem without making any code changes. I am assuming you need those binary files at run-time.
When you compile your project, the results are placed into a {projectFolder}\bin\Debug, and when debugging, the application is run from there.
You have 2 choices:
Keep your code as-is, and in the properties window, mark your bin files as "Copy if newer" or "Copy always". This will copy the files into \bin\Debug\skubin when you compile, and access them from there. This would simulate deploying those files with your application.
-- Or --
Modify your code to move up 2 directories from Application.StartupPath:
string BinDir = Path.Combine(startpath, "..\\..\\skubin");
This would be the option if you're not thinking about deploying your application, and just running it from within your project folder.
string filepath = Server.MapPath("~/skubin")
filepath= Path.Combine(filepath, "skuen.bin")
// open the file
//if not in a controller, you may use this
HttpContext.Current.Server.MapPath("~/skubin")
For Windows App, you may try Best way to get application folder path
I read the json configuration file (./config) in the Program.cs source code, but when I compile and run, the path of the program is at the compiled file location (/Debug/{ProjectName}), so it reports an error. Because it can't find the config.json file.
What do I have to do to automatically copy the json file to the compiled folder location at compile time?
Using VS 2017, open Solution Explorer and right click on the config file you want to move.
Go to properties and Set "Copy to OutPut Directory" to value "Copy if newer".
Hope this helps.
I have website project with a publish profile set up to publish to ~\www\Website (the project source folder is ~\Website)
There is an .xml file in ~\Website\bin that is included in the project (it's the only file from the \bin folder that is) which is required for the CMS (Sitecore).
Looking at the included file's properties menu, Build Action is set as Content and Copy To Output directory is set as Copy Always.
When I run the publish, the file is not copied to ~\www\Website\bin but instead is placed in ~\www\Website\bin\bin.
I've tried changing the "Items to deploy" in the projects Package/Publish properties to "All files in this project" as describe here but the result does not change.
This is quite vexing and I've not been able understand why it occurs. Does anyone have any insight? Thanks.
The basic logic behind "Copy always/if newer" is that the subdirectory structure is preserved when the files are copied to the output directory. Files that are located in subdirectories of your project are copied into the output directory in the same subdirectory structure.
ProjectDir\a.txt -> OutputDir\a.txt
ProjectDir\Subdir\b.txt -> OutputDir\Subdir\b.txt
By default, the bin-directory is the output directory of a web application. So if you put the file into the bin-subdir then it will be copied into bin\bin as you describe.
In order to solve this, move the file to the project directory. It will be copied to the output directory (bin) without a subdirectory.
I am using c#. In my project I am having a xml folder in which i have an xml file say "file.xml"..
I want to use the file in my project. I want to take that file from the current project itself,for that I am giving the path as:
xmlDoc.Load(#"..\xml\file.xml");
but it is not taking the file.
It is showing some "C:" path..
how can I retrive this file from project itself.
You should set the Copy to Output Directory property on the file in the Solution Explorer to gocpy the file to the folder with your EXE.
You can then write
xmlDoc.Load(Path.Combine(typeof(MyClass).Assembly, "file.xml"));
This uses the actual location of the EXE file and will work regardless of the current directory.
EDIT: In ASP.Net, you should put your file in the App_Data folder (which is not publicly accessible), then write
xmlDoc.Load(Server.MapPath("~/App_Data/file.xml"));
You should set the Copy to Output Directory to "copy if newer" and you can then use:
Path.Combine(Application.StartupPath, "file.xml");
Path.Combine(typeof(MyClass).Assembly.Location.ToString(), "file.xml")