I am making a console program in VS2010. The structure of as layed out in the solution explorer is Project file, Properties folder, References, and Program.cs.
I want to make use of a text file for reading/writing. What is the correct way of adding this file to the project? If I just do StreamWriter sw = new StreamWriter("maze.txt") then it'll create the file in the output folders (bin/debug or bin/release). But it won't show up in the solution explorer.
Now if I right-click on the solution explorer and Add New Item, I can create a text file in the root project folder (same level as Program.cs). This will show up in the solution explorer.
Is there a way for me to access this newly created file? Other than doing something like StreamWriter sw = new StreamWriter("../../maze.txt") by specifying the path to be two parents up?
How am I supposed to manage external files in a Visual Studio console applicatoin? In a WinForms application, there's a resources folder where I can add these things and a Resources.resx file to manage it. I can access it with Properties.Resources.someres.
If you want to create a file at design time and have it included in your bin folder then add the text file, go to properties, and select "copy always" or "copy if newer" for the copy property.
Note that this will copy the file from the project to the bin folder, but changes in the bin folder when debugging won't be copied up into the project itself. I doubt you want the program to interact with the version of the file in the project itself. If you do something like that then anytime you run the program outside of VS (i.e. when you copy it to the machine of an actual user) it won't work.
As for your resources question, you can still use resources. Just right-click and "Add" -> "New Item" (aka keyboard shortcut Ctrl+Shift+A) and choose "Resources File". You could also set the text file to be an Embedded Resource. MSDN has a lot of information on using resources on their Managing Application Resources page.
You can still embed resources to a console application. I've used this technique in quick apps so we don't need a installer or deploy dependencies such as these type of files. This question discusses a similar topic.
Related
I have multiple ASP.NET MVC5 projects that use several similar styles/scripts, and I think, for maintenance's sake, it'd be ideal to have a separate project in Visual Studio 2013 containing the LESS stylesheets and JavaScript files that can be shared between the two.
My setup is as follows:
Web.Project1
Web.Project2
Web.Assets //would contain LESS/JS files
Resources
Domain
Data
Any pointers?
I've been looking at adding as a link, but I'm not certain that would accomplish what I need.
I recently went through this process for a project of mine, and concluded that if your goal is simply to avoid duplication by keeping your code DRY then using linked files works great.
It's simple enough to do, but I'll write it up anyway as it might save someone some time.
Quick Summary
Create common Solution Folders containing the resources to be shared, or simply designate one of the projects to be the master.
Use "Add as Link" to add the shared resource files to each project as needed.
Add an AfterBuild task to each project file that will copy the linked files to project folders. This is only needed so that Visual Studio test/debug (F5) will work locally.
Detailed explanation follows:
Configuring Solution Folders for the Shared Resources
** Note that if you're simply going to share files directly from one project to one or more additional projects then you can skip this step.
Visual Studio solution folders do not have to reflect physical file system folders, but doing so will help preserve your sanity. So first create the folders on your local file system and copy the resource files into them. The new folders should be located under your solution folder. For example:
\MySolution
\Common
\Images
\Scripts
\Styles
Back in Visual Studio, right click on the Solution Items folder and use Add Solution Folder to replicate the new file system folders.
Next, add the files to the new solution folders by right-clicking each folder and using Add Existing Item to add the contents of the folders.
Add Shared Resources as Links
For each project that will use the shared resources, right-click the project folder and choose Add Existing Item. Browse to the common folder, select the desired files, click the drop-down arrow next to the "Add" button and choose "Add as Link".
You may get a source control warning about adding files that are outside of the project directory structure, but this can be ignored since the linked file will be under source control at its source.
Add an AfterBuild Task to Copy Files
When you publish the application to a server the linked files will copied to the project folders to which they are linked and everything works as expected. However, in the development environment the linked files do not physically reside in the project folders. So when you hit F5 to test your application in VS, the shared resources will be missing.
The simple solution is to add an MSBuild task to copy the linked files from their source after each build. This needs to be done to for each project that contains the shared resource links.
Right-click the project and choose Unload Project. Right-click the project again and choose Edit <ProjectFileName>. Scroll to the bottom and add the following (just above "</Project>"):
<Target Name="AfterBuild">
<!-- Copy linked content files to local folders so that they are available in the debugger.
This is only an issue when running the application locally. The linked files should
be automatically published to the correct folder when publishing to a web server. -->
<Copy SourceFiles="%(Content.Identity)"
DestinationFiles="%(Content.Link)"
SkipUnchangedFiles='true'
OverwriteReadOnlyFiles='true'
Condition="'%(Content.Link)' != ''" />
</Target>
** Copy task adapted from this link.
Save the project file then right-click and choose Reload Project.
I have a project that acts as wrapper for a 3rd party .exe (it has static methods to create the command line and run the executable). I want to use it in at least a couple of projects in my solution. Ideally, the .exe should only be in that wrapper project (I don't want to have to add it to each project that uses it). Right now I'm trying to get this to work with a web project (.NET MVC) running on IIS 7 but when I use Assembly.GetExecutingAssembly().Location to see the directory that my wrapper is called from I'm in a folder like
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET
Files\"my
project"\65a016fb\ac5f20a7\assembly\dl3\d8de0f10\06e277a2_55b2cd01
and my 3rd party .exe is nowhere to be found. Is it possible to copy files with a reference that do not compile?
Btw, I have set the "Copy to output directory" and "Build Action" properties of my .exe to "Copy always" and "Content"/"Resource"/"Embedded Resource" without success so far.
Try to add an existing files in your projects which use your wrapper. When the dialog box appears, do this:
http://wiki.oxygenelanguage.com/en-w/images/0/0d/AddAsLinkAero.png
Either that or drag neccessary files to your projects from the wrapped with Alt holded.
That will add the files as links. Links are good choice for reusal whenever the original files has to be located in the only place.
Adding files as links in conjunction with setting to that links build action to Content and Copy always to the option of copying them to the output directory should work for your.
If anyone runs into this problem, here's how I got around it:
Add the .exe to your project's resources (right-click on the project,
Properties -> Resources, add file and give it a resource name)
You may now access your .exe as a byte[] (at least if you're building to a dll)
with Properties.Resources.*NameYouGaveYourResource*
Before using your .exe, use File.Exists() to check if the file exists in Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) (or you can put the exe wherever you want using the method, above gets you directory the .dll is in) - if not, write out the file:
using (FileStream fileStream = new FileStream(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\EXE.exe", FileMode.CreateNew)) {
fileStream.Write(Properties.Resources.NameYouGaveYourResource, 0, > Properties.Resources.NameYouGaveYourResource.Length);
}
Now you can use the exe easily as a Process. Pretty far from ideal, but it means the exe accompanies your .dll wherever it goes.
I have created a C# WinForms application. It has some additional files that it uses, such as help files and some external data files. I want to put these files in folders under the Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) path so that the application can find and read them.
This path doesn't get created until the application is installed right? So where do I put these files in my VS2010 project, or how do I tell my project that these files exist so that when I am running (debugging) the application from VS it will find the files.
Thanks
EDIT: I did think about adding the files as resource files, but unfortunately some files may get added after the project is built and deployed. Therefore we decided to make the program search and find the data files (and associated help files) at a specific location and load them if they exist.
Thing is that your application should not need to use 'Environment.SpecialFolder.ProgramFiles'. It "should" be location agnostic. The quick answer to your question is "from the folder that the application was launched from". The real answer is how you reference these files.
Your help files and data files need to be deployed to folder with a known relationship to the application. That is, the same folder or a child folder. Consider making the file resources.
Now if the files are user configurable or run time writable then they should not be in the program files area but be in the application data area. If so, then there is your path!
Hope this helps.
You should add these files to the main (exe) project inside your solution.
(Right click on Project Name, Select Add Existing Item)
Then set its property Copy to Output Directory = Always
(not sure of the translation because I use a localized version of VS)
Of course, it should be noted that, when you deploy your files in the Environment.SpecialFolder.ProgramFiles, you could not be able to write to them (Windows7, Vista) for security reasons.
Add the files as resources in your project. Take a look at this MSDN article. You can add resources to a project by right-clicking the Properties node under your project in Solution Explorer, clicking Open, and then clicking the Add Resource button on the Resources page in Project Designer.
You can add resources to your project either as linked resources, which are external files, or as embedded resources, which are embedded directly into the .resx file.
When you add a linked resource, the .resx file that stores your project resource information includes only a relative path to the resource file on disk. If you add images, videos, or other complex files as linked resources, you can edit them using a default editor that you associate with that file type in the Resource Designer.
When you add an embedded resource, the data is stored directly in the project's resource (.resx) file. Strings can only be stored as embedded resources.
Everything in Visual Studio seems to lead one to putting data files with the application.The app.config goes there, when I create an .XML data file, there is a Copy to Output property that will automatically copy that file to the exe folder. Howerver, it seems that under Vista and Win7 UAC doesn't want the application to be able to write data to any file in the application directory. So I'm changing my evil ways so that I use the LocalApplicationData folder for files I want to read and write. (I just read the app.config so I'm leaving it alone)
I'm using a VS2010 Visual Studio Installer project to create the installer for this app and I can't seem to find a way to target the folder for my .xml file to the LocalApplicationData folder. I can click on the file and see a Folder property but the dialog only has options for Application Folder, User's Desktop and User's Program Menu. Is there some way to do this in the installer or do I have to write code that checks for the file and copies it over from the .exe folder when it doesn't exist? I figure I'm late to this particular party and there must be a canonical way of handling this.
Also, I'm wondering about debugging, is there something similar to the copy if newer functionality in the build process that will now copy this .xml file automatically over to the LocalApplicationData folder whenever I update it?
The Setup project doesn't expose LocalApplicationData in the Special Folders list. You can use it anyway by doing this:
Add a Custom Folder and set the DefaultLocation property to [LocalAppDataFolder]
I've got an application that I'm moving over to ClickOnce and the app has a moderately sized data folder with hundreds of files that I need to inlcude in the deployment. The folder needs to be in the same place relative to the EXE after deployment. I've seen several suggestions on how to do this but there doesn't seem to be a agreed upon method for doing this.
Any suggestions would be great -
Thanks!
One good way of doing this is:
Create a folder under the app in VS name e.g. "datafiles"
Add all files to that folder using Add as link in the dialog box after selecting Add existing item on the folder
Mark all files as Copy if newer (Copy to output directory property)
Make sure the build action is content
--> when you publish the files will be put in that folder and be a part of the application installation
Good luck!
After deployment, all files marked as data are placed in the ApplicationDeployment.DataDirectory folder. I know of no way to change this. You could copy the data files during the first run of your app, but this approach will not survive any upgrades that include data file changes.
Alternatively if you have control over the location of the data folder during development, you can place in the same relative (to the app folder) location as will be specified after deployment.
https://msdn.microsoft.com/en-us/library/kzy0fky2.aspx
https://msdn.microsoft.com/en-us/library/6fehc36e.aspx
These two articles provide methods of doing this. Between the two of thing you can find one that works for you. The one that worked for me was:
With a project selected in Solution Explorer, on the Project menu,
click Properties. Click the Publish tab. Click the Application Files
button to open the Application Files dialog box. In the Application
Files dialog box, select the file that you wish to mark as data. In
the Publish Status field, select Data File from the drop-down list.