How do you add files to projects? - c#

Im currently trying to add 2 files to my project. One of them is a .ovpn (vpn Config file) and the other is a batch file.
Im trying to create a directory in my code and then add these files to that newly created directory so when the users first run's this application it creates the files needed to run the application.
Is there anyway of adding these files like a resource so I can just reference them easy?
Sorry if this is a really simple question. Just never done it before.
Thankyou in advance!

You can try to change properties of your files like:

You don't need to necessarily have them embedded in the executable to have them easily referenced.
Add a 'resources' type object to your project - call it Resources.res
Right click on your project, make a New Folder
Call it 'Resources' - add your resource files in here.
Double click on your Resources.res file
Click on the 'Add New Existing File'
Navigate to your file that is in the Resources directory and select it.
You should see your file showing up inside the resources viewer now.
When you are writing code, you can now reference your added files via:
Resources.<nameoffile>
If your file is a text file, you can do things like
string jsonRequest = Resources.RegistrationRequest;
This should automatically set the files to be embedded as well, so they should be accessible at run time.

Related

Use external existing file as resource file in .NET

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

How do I assign a relative path of an embed image to a ActiveSheet.PageSetup.LeftHeaderPicture.FileName?

There are 2 SSCCEs for this:
1.
I have added a picture to my VSTO document-level add-in by right clicking the solution -> add -> existing item -> myPic.jpg.
Now I am working with an Excel spreadsheet and want to add a picture to the top left header. Using PageSetup.LeftHeaderPicture.FileName and providing an absolute path to the picture it loads just fine while debugging.
When I am trying to change the path to not-absolute and say something like
ActiveSheet.PageSetup.LeftHeaderPicture.FileName = "\\Assets\\myPic.jpg"
I keep getting a HRESULT: 0x800A03EC exception.
I think I am not getting the correct syntax for to access the Assets\myPic.jpg.
2.
I have added a new resource, selected an existing item and selected the myPic.jpg. I can access it via Resource1.myPic but ActiveSheet.PageSetup.LeftHeaderPicture is read-only...
The PageSetup.LeftHeaderPicture.FileName needs a string type parameter and I am not sure how to retrieve the path to my already embed resource...
Q:
How do I embed a picture in my solution as a resource (or just an existing item) to be able to use it with PageSetup.LeftHeaderPicture.FileName?
Right, I have solved it.
Added an existing item myPic.jpg to the solution and set Build Action to Content and Copy to Output Directory to Copy Always (but I am sure you can set it to copy if newer)
Note: With this setup your file always gets "copied" to the published directory.
Now all you need in your code is
ws.PageSetup.LeftHeaderPicture.Filename =
AppDomain.CurrentDomain.BaseDirectory + "\\myPic.jpg";
ws.PageSetup.LeftHeader = "&G";
AppDomain.CurrentDomain.BaseDirectory is very well explained here
and the end result as expected
From my experience adding files to a project will embed them into your .exe. That means you can not refer them by using a file path. You can however read them.
If you want your file to placed outside the .exe file you should just add it your project then right click on it and select properties. Set the Copy to output directory property to Always copy. Your file will be copied into the build folder under the same folder structure. Now you can refer your file with a relative path.
If you really need to get that picture from the resource file you could save it as a temporary file (using Path.GetTempFileName) then load it via file path.

VS2010 (C#) Environment.SpecialFolder.ProgramFiles is where in the Solution Project?

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.

.NET Projects: Where do I put .resx files?

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

C# assembly - shortcuts

I have a C# project that has multiple folders: Folder1, Folder2.
I added a shortcut in 'Folder1' to an xml file that is already in 'Folder2'. In this case when I compile the project the xml file will I have two copies in the assembly?
You were not clear with your description - did you reference the file with a shortcut, or did you add it to Folder1 by using the VS Solution Explorer and adding an existing item as a link?
The two methods are quite different. If you simply went into the filesystem and added a shortcut, then that is not automatically part of the project unless you specifically add it. And if you did add it, then it obviously can't be compiled, the best you could do is just have it set to No Compile and copy to the output directory.
If you added the file to folder one via the VS solution explorer and added it as a link, then it will be part of the project, and will be included twice, but it will be placed in Folder1 under the bin/debug or bin/release folder upon building. There will be no clash because they are in different folders, and they are not compiled, simply copied to the folder structure under the output directory.
Edit: and it won't be part of the assembly unless you set it to be a Resource or Embedded Resource. If you do that then yes, it will be in the assembly, but under two different resource paths.
In the Properties(context menu) of the selected file in the Solution Explorer change Build Action property to No Compile.

Categories

Resources