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.
Related
Say I use some .json files to descript some object data which effect to the program's behavior, I hope to use these files in the following scenarios
The default values, for this purpose, I need a set of files follows with the application to be packed and installed.
I wish it could be edited by human manually. (Because something have no interface to be modify on UI)
Both user and the program need to kwnow the location the files will be placed after installation.
In debugging stage, I could put these files in the user\AppData\Local.. folder and I know how to access them, but I don't know how to put files into the package and will them generated to anywhere after install?
Thank you for any suggestion.
ps.
I use the "Blank App (WinUI 3 in UWP)" template to create my
application.
I'm new in UWP and WinUI, I used to write traditional Windows Form programs.
How to include externel user files into UWP side-loading package?
You could place the json file into app's project and set the file property as Content, then it will deploy into installtion folder after package install. and please note the json file is readonly in the installtion folder.
so you could call CopyAsync method copy the file to the destination folder that app's local folder with full permission.
For more details about file access permissions please refer this document.
I'm writing some integration tests for some SQL scripts that live in a folder separate from the project. Since the setup of the machine I'm writing on the tests on differs from the machine they will be run on, I would like to include them as resource files rather than hard coding any paths. However the default behavior for adding an existing file as a resource file simply copies the file, which is not what I want in case any of the SQL scripts get updated.
Does anyone know the best way to get the resource file to actually reference the SQL scripts that are in a folder separate from the project, or to somehow copy them into the assembly at compile time so I don't have to load them via absolute/relative paths?
Edit: For clarity, I'm trying to get the resource file to act as a symlink to the original file at compile time. Adding an existing file to a resource in file in Visual Studio simply makes a copy of the file, which means any changes to the original are not propagated to the resource file.
After asking on IRC, someone guided me to what I was looking for. You are able to add an existing file to a project as a link (there is an arrow on the Add box when in the file dialog), and set it's Build Action property to Embedded Resource
https://support.microsoft.com/en-us/kb/306234
Daniel posted a link on how to actually read an embedded resource:
How to read embedded resource text file
the Resources folder typically contains all the app images (those 16 X16) that we use to look our apps good. I haven't dealt with installation and resources folder before therefore :
When application needs to be deployed, what would happen to those images in that Resource folder?
I haven't tried this before so when I install the application where would that resource folder be saved?
If the installation creates another resource folder to keep those images, then what if a user manually deletes that folder? App would crash? what is the solution to overcome this?
I've heard some place the images in a .dll file, is this a common practice?
thanks
When you build your application, Visual Studio invokes the resgen.exe tool to convert your application resources into an internal class called Resources. This class is contained in the Resources.Designer.cs file which is nested under the Resources.resx file in Solution Explorer. The Resources class encapsulates all your project resources into static readonly get properties as a way of providing strongly-typed resources at run-time. When you build through the Visual C# IDE, all the encapsulated resource data, including both the resources that were embedded into the .resx file and the linked files, is compiled directly into the application assembly (the .exe or .dll file). In other words, the Visual C# IDE always uses the /resource compiler option. If you build from the command line, you can specify the /linkresource compiler option that will enable you to deploy resources in a separate file from the main application assembly. This is an advanced scenario and is only necessary in certain rare situations. A more common scenario for deploying resources separately from the main application assembly is to use satellite assemblies as discussed below.
All your queries is answered above
i.e.
When application needs to be deployed, what would happen to those images in that Resource folder?
I haven't tried this before so when I install the application where would that resource folder be saved?
If the installation creates another resource folder to keep those images, then what if a user manually deletes that folder? App would crash? what is the solution to overcome this?
I've heard some place the images in a .dll file, is this a common practice?
For More information
http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx
Where do I put .resx files? Sometimes I see these files under Properties folder. Is there any design guideline about it?
Thank you!
Depends on what you are doing.
For example a web Application they go in App_GlobalResources or App_LocalResources folder.
For other projects I would create a Resource folder and put them there.
In a WinForms app, the .resx file associated ot a class (form, user-control,...) is stored side-by-side with the source code (e.g. C#) file. In addition, a global .resx file is created in the properties to let you store global stuff such as messages, pictures,...
If you right click your project in VS solution explorer you should have an option "Create folder" this will then have a list of folder you can create. App_GlobalResources and App_LocalResources is another.
For project wide strings and resources add the contents to the App_Global forlder. for form specific resources add the content to the App_Local folder instead.
Have a look here for more information
I have an image that is used in some PDF files that my C# application generates. I know how to reference the image file when it is located in my workspace, but when I compile the program, I don't see the image anywhere in the compiled directory.
Can someone tell me what happened to that file, or do I have to manually package the file along with my program when I send the program to the users? I added the image to the workspace by drag-drop to the resource directory of one of my namespaces.
Check the file's properties in Visual Studio. Have you set the CopyToOutputDirectory property to something besides Do not copy?
Hmm... I'm not sure about the whole drag-drop business, but if it's all working the resource will have been embedded into your assembly.
I suggest you have a look with Reflector - you can see the resources embedded within assemblies using that.
Have a look at the build properties for the item in Solution Explorer - in particular, if the build action is Embedded Resource then it will indeed be built into the assembly.