How to modify and export files included in project? - c#

For example, lets say I have a text file with one line of code named Template.txt included in the project folder.
Since the text file is included with the project, is there another way to specify this file or would I still have to specify the full file path?

If it's included in the project and if you select the option copy always from the property Copy to Output Directory (Right click on the file and select properties in VS), you will have the file available in the bin/ folder after compilation. So, you would not need the entire path. Just the name of the file.

If i understand your question,
you can put the file on the same folder where the exe file is and specify only the name of the file [Template.txt].

Related

How to get .HLP file dynamically at run time Using C#

I need to open .HLP file by button click. i don't want to call .hlp from local folder like C:\ drive. i don't know where should i put .hlp file in my project. Please tell me with sample code.
i used this code :
Help.ShowHelp(this,"C:/Users/pushparajs/Downloads/TabbedMdiChildForms_src/TabbedMDIChildForms/helpfile/Properties/TTYUI.HLP");
You shouldn't need to specify the full path. Normally you just specify the file name, e.g. "TTYUI.HLP", and Help.ShowHelp(this, "TTYUI.HLP") is smart enough to look in the same folder as Application.ExecutablePath.
You can add a reference to your hlp file in your solution explorer, and open properties by right clicking, and specify to copy to output folder.

How to send all .cs files to output directory after every build in Visual Studio 2012.

I have some project what contains 3 dll libraries (one of them has 2 dependencies)
At the output folder of 'DLL parent' I need to have all source files of this 2 children. Moveover I want to save structure of this cs files by making 'cs' folder at output folder of DLL-parent, create subfolder with DLL-Child name there and copy all cs files of it to this folder.
For doing this I use post build events for DLL-Child.
And at bottom level when I build just this dll it works perfect.
After I set this command line at DLL-Child post build event it's output is created with folder with source files inside.
But when I build whole solution problem that this folder is not copied from output of DLL-Child to output of DLL-Parent.
Why ? At references section of DLL-Parent I set 'Copy local' to true for dependencies but it copies nothing.
Have your files be exported in separate folders per project. With this simple post build script (it's better to set it as post build event and select on successful build so as to not copy it each time but only when a build has been done properly)
copy "$(ProjectDir)FolderName\*" "PathYouWantYourFilesCopiedTo"
You can output all files from specific folder in your project.
You do not need checks for folders existing, VS does it on it's own. Create one build event for each folder of your files. If you have them in separate folders, nothing will overwrite nothing (as long as you maintain the folder structure in the output). And copy a whole folder is easier than copying file by file.
Basically what you need to do is to specify explicitly folder names for the output location. That will stop overwriting of files.

Change picture box picture

I added picture box to my form and import 2 pictures, from properties under image property i choose the first picture when the application starting and inside my start button event i want to change my picture to the other picture.
this is what i have try:
pbIndicator.Image = Image.FromFile(#"..\Resources\indicator_green.png");
but file not found exception error occurs.
You should be able to do something like this:
pbIndicator.Image = Resources.indicator_green;
Be sure that in the property window if the Build Action is on Content, and Copy to Output Directory is on Copy if newer.
If you want it to be content. Else use the answer Shadow Wizard gave.
As I wrote in the comment if indicator_green.jpg is an image included as resource via resource file (Resources.resx) then it won't be copied to output directory (it means it's in your project folder because it's used to build executable but it'll be embedded inside your assembly, not deployed standalone).
Resource files will (by default) place resources you add inside Resources folder (and then linked). You can always access them using generated code file for resources:
pbIndicator.Image = Properties.Resources.indicator_green;
You may change namespace Properties and property name according to what you have in your project (by default property name has the same name of the resource and then same name as original file).
Of course you're not forced to embed your resources in your assembly. If you want to deploy them as standalone files just right click Resources folder and add an existing file. In the properties window for that file select Copy always for Copy to output directory and et voila, you'll be able to read it with:
pbIndicator.Image = Image.FromFile(#"Resources\indicator_green.png");
Please note that Resources folder won't be a sub-directory of your output directory (do not forget that source files are not part of installation).
Anyway I suggest you do not build path like that, little bit better would be to do not rely on current folder:
pbIndicator.Image = Image.FromFile(
Path.Combine(Application.StartupFolder, #"Resources\indicator_green.png");
You're not limited to Resources folder, you can do that with any folder (and with any name).

c# class library use files

I'm building a class library which use a .dat file, inside the library i made a folder App_Data and put the .dat file inside it, how can i have my library to use the .dat file. How could it be included at the dll when build?
I already tried Path.GetFullPath, and almost anything in Path, but all i get is the path to debug folder, and in that folder i dont even have the .dat file which i expect to be included because the .dat file is in the project.
I just want to use that .dat file without hard coding its path, and it should still work when referenced.
You must right-click the .dat file and set Copy to Output Directory = Copy Always. Doing so, the path to the debug folder will correctly point to the file. No need to hard code the path.
If you're interested in embedding the .dat file in the DLL (a bit more work), you can go this way:
Can I embed other files in a DLL?

Embedding a PDF as a resource in WPF application

I want to embed a PDF file (which is basically have Product details, Release notes) and wants to open that file from menu bar. What would be the best approach. I need to use this file in installer also. So i'm looking for an approach in which file will be moved to BIN on compilation and from there installer can access that file.
IDEAS ...
Add the file to the project the builds the EXE (use Add existing file in visual studio). Then right click on the file in visual studio, go to properties, and verify that the build action is "Content" and the copy to output directory is "Always" or "If newer" (whichever suits you).
Then this file will always be copied to the same directory where the EXE resides and your application will be able to access it because it's always in the application's directory.
If the installer just takes the BIN directory then it will also be able to access it because the file will reside in the BIN directory.
Have fun!
Finally i did it in following way:
a. We've a folder which contains notes.pdf (used by installshield).
b. Created a pre build command to copy the pdf file from installshield folder to output directory.
c. use process.start("notes.pdf"); to open the file. As it would look in bin directory first for pdf file and we've already copied it there.
It worked for both Installer version and running application from code.

Categories