Read File from main directory in asp.net application - c#

Solution Explorer
Here, vaccineFiles.txt is the file, I want to access from marked class file named validators.cs. It is marked in blue. What is the proper way to access the file?

My first recommendation is that if validator.cs is not a controller, it´s better to move it outside Controllers folder.
With "AppDomain.CurrentDomain.BaseDirectory" you can get base directory for your execution base path. Another consideration is to set "Copy to output directory" to "Copy always" or "Copy if newer" under properties (F4) for vaccineFiles.txt in order the file to be copied to bin folder after building it.
I hope it could be helpful for you.
BR!

Related

How can I fetch a file to be read in StreamReader and listed in a ListBox? [duplicate]

Task is to form Visual Studio 2010 project so, that during any build or publish some foo.exe utility should be copied to output (bin) directory.
Early I have made PostBuildEvent task in .csproj (MSBuild-file):
<PropertyGroup>
<PostBuildEvent>
Copy "$(SolutionDir)Tools\foo.exe" "$(ProjectDir)$(OutDir)foo.exe"
</PostBuildEvent>
</PropertyGroup>
But this is not universal. During publishing (Visual Studio 2010) foo.exe appears in bin directory, but is not copied to output publish directory. Maybe I do everything completely wrong and there is standard mechanism to include files in projects to be later, during build or publish, copied to bin?
There is and it is not dependent on post build events.
Add the file to your project, then in the file properties select under "Copy to Output Directory" either "Copy Always" or "Copy if Newer".
See MSDN.
I only have the need to push files during a build, so I just added a Post-build Event Command Line entry like this:
Copy /Y "$(SolutionDir)Third Party\SomeLibrary\*" "$(TargetDir)"
You can set this by right-clicking your Project in the Solution Explorer, then Properties > Build Events
In Solution Explorer, please select files you want to copied to output directory and assign two properties:
- Build action = Content
- Copy to Output Directory = Copy Always
This will do the trick.
Add the file to your project.
Go to the Properties of that file.
Set "Build Action" to Embedded Resource.
Set "Copy to Output Directory" to Copy Always.
There is another way that can copy items that are "outside" the Solution (which also makes it technically possible to copy Solution Items as well).
In Solution Explorer, right-click in your project and choose "Add... Existing Item". Locate the file in question (it can by any type, not just code), and next to the "Add" button, click the drop-down arrow and select "Add As Link".
In Solution Explorer, select the item that was just added to your project and change the Copy to Output Directory property to Copy if newer or Copy always, as appropriate.
In my case, setting Copy to Output Directory to Copy Always and Build did not do the trick, while Rebuild did.
Hope this helps someone!
Try adding a reference to the missing dll's from your service/web project directly. Adding the references to a different project didn't work for me.
I only had to do this when publishing my web app because it wasn't copying all the required dll's.
Just so my fellow neuronically impaired comrades might chance upon it here, I had assumed that, for web projects, if the linked file was an external .config file that the "output directory" would be the same directory that web.config lives in, i.e. your web project's root. In retrospect, it is entirely unsurprising that it copies the linked file into the root/bin folder.
So, if it's an appSettings include file, your web.config's open tag would be
<appSettings file=".\bin\includedAppSettingsFile.config">
Duh.

VS solution folder not generating in the bin/debug or release folder

I have added a solution folder on my visual solution project.
I have added some text files inside that folder.
I have Built and Rebuilt my solution but still nothing.
The error I get when trying to access a file from the folder:
System.IO.StreamReader file0 = new System.IO.StreamReader(Path.Combine(Environment.CurrentDirectory, #"newFolder\SampleText.txt"));
An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll
Additional information: Could not find a part of the path 'I:\Programming\Projects\SampleProject\SampleProject\SampleProject\bin\Release\newFolder\SampleText.txt'.
The path to the Release folder is valid, but newFolder is not inside.
I don't understand why it is not being generated, any help would be great.
Click on your text file, change Copy to Output Directory to Copy if newer or Copy always. This will copy the file and any folder structure beneath it up to the project level and re-create it in the output folder.
The answers correct. You need to change "Copy to Output Directory" to "Copy Always" or "Copy If Newer" BUT YOU NEED TO DO ONE MORE THING. You need to choose Build Action as "Content". By doing so the folders that contain the files are generated in debug folder automatically without hassle. The other Build Actions may also work I am not sure but not "Resource" one.
If you set text file's property "Copy to Output Directory" to "Always" in solution then folder that contains this file will be created automatically
If you create your folder at the project level as opposed to the solution level, you can then go to each file in that folder and view the Properties (right-click / Properties). Here, can then set the "Copy to Output Directory" option to "Copy Always" or "Copy if newer", whichever one is appropriate for your purposes.

Reading Xml file path from Windows for application

I have my files place in a directory name XMLpackage which is located under the Root of my project
At run time i need to read the file by passing the Path.
By putting my file in debug folder i was able to read. But in this specific XMLpackage folder i can not read it.
How to do this?
Change the file property 'Copy to Output' with value other than 'Do not copy'
To get the file path try,
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLpackage", "file1.xml");
Try this. Right click the file in Solution Explorer and click on Properties. Change the "Copy to Output Directory" to "Copy Always" as shown in the image.
You can access the file like
string f = System.IO.File.ReadAllText(#"XMLpackage\TableScripts.xml");

WPF C# XML resources in subfolder not getting built

Currently in my project directory I have a folder called "Data" and in it I have two XML files that I use in my WPF application. I have set the XML files as "Resource" and the "Copy to Output Directory" option to "Copy Always". However, the following code throws a DirectoryNotFoundException because the files aren't being copied over to bin\debug when I build my application.
return new Game(XDocument.Load("Data/Game.xml").Root);
How do I make it so that everytime I build the files are moved over without having to do it manually everytime I change my XML?
Set 'Build Action' to None
When you set Build Action to Resource - it means, that VS will automatically pack this file into assembly as embedded resource. More about Resource Files on MSDN
And when 'Build Action' is None - Studio has nothing else to do, but to follow "Copy to Output Directory" command.

Publishing RDLC files

I have a few RDLC files in my WPF application. When I publish the project, the report files don't get published, and when I try to access them with the client I get an error saying that they're not found. I've checked the publish folder, and indeed, they're not there. I call the RDLC files with the following code:
Microsoft.Reporting.WinForms.ReportViewer rvReportViewer;
/* ... */
rvReportViewer.LocalReport.ReportPath = string.Format("Reports/{0}.rdlc", ReportName);
I've tried setting the Build Action on these to "Resource", which is what I have for my static image files, but that doesn't seem to make any difference.
Why don't the reports get copied to the publish directory? Is there a way to force this to happen, or should I be accessing them in some other way?
Thanks!
I don't know if there's special concerns with RDLC files, but here's the general approach for making sure other files get published.
Right click the file in the solution explorer, select Properties. Set the "Build Action" to "Content", and "Copy to output Directory to "Copy if newer".
Go to the project properties->Publish page. Hit "Application Files Button", check "Show all files". Set the "Publish Status" to "Include (auto)"
File Properties
Application Files
I've been using the answer by #nos for a while, but I noticed that in the publication, the set of .rdlc-files is copied to two places:
The original webapplication project relative location;
Inside the bin folder.
So if I had \Reports\Report1.rdlc, I would find in the published location:
1. <published folder>\Reports\Report1.rdlc;
2. <published folder>\bin\Reports\Report1.rdlc.
I don't mean to use copies of the rdlc's in the second location and, indeed, if I delete these, the reports are still generated.
I changed the settings for each rdlc-file to:
Build Action: Content;
Copy to Output Directory: Do not copy.
And now nothing is written to the second location.
Sorry guys this has nothing to do with "Copy Always" or "Copy if Newer". In fact I set it to "Never Copy".
This issue is resolved by changing the Build Action from "Embedded Resource" to "Content". Then when you build and publish RLDC files will get copied to the right place.
instead of using .ReportPath
**used the following code:
reportViewer1.LocalReport.ReportEmbeddedResource="[Project
Name].[FolderName if Exist].[ReportName].rdlc";
Then rebuild your deployment Project.

Categories