visual studio 2010 how to reference a folder inside a project? - wpf

I have added a folder with a bunch of images inside my vs2010 project.
I want to reference this folder through code.
Since the final prog may be deployed into different location absolute path won't do!
What is the standard way to get the folders relative path?
I am fairly new to this, so sorry if I am asking something too obvious!
Thank you.

You might want to add images to the resources file, so you will be able to work directly with an image without writing code to find the file.
Anyway, you can get the application path:
string appFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
And then you can use methods from Path class, like Path.Combine to refer to the folder.

Resources is a good idea. An alternative is to have the Application's installed folder location written to the Registry when the application is installed. OR do something like
string dir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

If these images are relative to your executable, then you can either use argv[0] from main() (if it's command line), or even better follow what MSDN is advicing:
By convention, argv[0] is the command
with which the program is invoked.
However, it is possible to spawn a
process using CreateProcess and if you
use both the first and second
arguments (lpApplicationName and
lpCommandLine), argv[0] may not be the
executable name; use GetModuleFileName
to retrieve the executable name, and
its fully-qualified path.

Related

How to open file with path relative to c file, not working directory

I'm modifying a large codebase written in C. This code is designed to be compiled and ran from an arbitrary working directory. This is so configuration files can be read and output written to/from the working directory, making it easier to organize the setup and outputs of the code.
The additions I've made to this code need to read data from a few data files. I would like to place these in the same directory as the .c file where they are read, with a directory structure as follows:
big_project/
|-- models/
| |-- my_file.c
| |-- my_data.txt
My problem comes in when trying to open this data file using relative paths. Typically relative paths are relative to the working directory, which would not work in my case since the working directory can be arbitrary. From inside my_file.c, how can I open my_data.txt for reading using relative paths?
Based on our conversation in the comments, you have several alternatives, which I'll list below from, IMHO, most to least desirable.
You never specified if you were in Windows, GNU+Linux, or were doing cross-platform development, but I'm sure you can adapt the suggestions to your platform.
Multiple and Custom Config Files (Recommended)
You could modify your program to look at your platform's standard location for program data and/or configuration files. For example, you could have it look for a standard config at /etc/<your-program>/default.conf in GNU+Linux or %APPDATA%\<your-program>\default.conf in Windows.
If different users need to use their own personal configs, the program could also be made to accept a config file path as an argument. For example:
GNU+Linux:
$ ./your-program --config ${HOME}/.your-program/my.conf
Windows:
> your-program.exe --config %userprofile%\your-program\my.conf
Note that the use of %userprofile% may change based on Windows versions and/or shells used (e.g. standard cmd.exe vs powershell).
Compiling in the Path (Not Recommended)
Based on your comments, a short-term workaround could be to compile the absolute path into it for the __FILE__ macro to give that back to you at runtime. As I said in my comment:
if you're completely sure about the program always being placed in the same directory for everyone, then you can set the absolute path shown by the __FILE__ macro if you send the full path when compiling; e.g. gcc $(pwd)/your-file.c, when it prints __FILE__ will show the full path it had at compile time, not run-time. (Can't add enough disclaimers here, though)
Please note that there're many reasons to not use this approach. I'm simply suggesting it as a short-term workaround to pull out of an existing crisis-level situation you may have, while you (hopefully) take a closer look at the more desirable approach to handle configurations and path-finding.

How to put all the files used inside the winform application in the exe itself?

I am making a winform application whereby I am using some images to show .It works fine when run . But what if I want to take the exe out from the debug folder and use it in some other machine then it will give exceptions that it can't find images on the same path(its obvious as it is not there in that machine path).
Idea to overcome this is to make a setup out of it,but that too is a tedious task.
Can it be possible that we can accomodate all the other sources(like images) used in the project in such a way that it should go along with the exe ?
It's a kind of an odd error, because the *.exe file in the Debug folder is created after adding all the resources into it. And that's why the *.exe file gets lager in size. But it will be a problem if you have mentioned the paths of the resources (like images) manually.
To avoid that always use the properties panel to import resources to the project and this will create the Resoures.resx automatically. Then all the resources will stick to the *.exe file.
But if you have mentioned the paths manually you must provide them in the targeted computer which you are going to use the *.exe file. To make it more easier, give a path in the same folder where the *.exe file exist.
For example give simple paths like (#"image.jpg"), without giving paths like ("C:\Users\Sam\Pictures\image.jpg").
And create a setup including all the resources like images, databases, etc.

How link to any local file with markdown syntax?

I have a local markdown file containing several links and I want that links head to local file like pdf.
I use the following syntax:
[my link](file:///C:/my_file.pdf)
But when I open my markdown file into a Firefox page and click on the link, nothing happens.
What exactly have I missed? Is it possible to open local file?
None of the answers worked for me. But inspired in BarryPye's answer I found out it works when using relative paths!
# Contents from the '/media/user/README_1.md' markdown file:
Read more [here](./README_2.md) # It works!
Read more [here](file:///media/user/README_2.md) # Doesn't work
Read more [here](/media/user/README_2.md) # Doesn't work
How are you opening the rendered Markdown?
If you host it over HTTP, i.e. you access it via http:// or https://, most modern browsers will refuse to open local links, e.g. with file://. This is a security feature:
For security purposes, Mozilla applications block links to local files (and directories) from remote files. This includes linking to files on your hard drive, on mapped network drives, and accessible via Uniform Naming Convention (UNC) paths. This prevents a number of unpleasant possibilities, including:
Allowing sites to detect your operating system by checking default installation paths
Allowing sites to exploit system vulnerabilities (e.g., C:\con\con in Windows 95/98)
Allowing sites to detect browser preferences or read sensitive data
There are some workarounds listed on that page, but my recommendation is to avoid doing this if you can.
You link to a local file the same way you link to local images. Here is an example to link to file start_caQtDM_7id.sh in the same directory as the markdown source:
![start_caQtDM_7id.sh](./start_caQtDM_7id.sh)
After messing around with #BringBackCommodore64 answer I figured it out
[link](file:///d:/absolute.md) # absolute filesystem path
[link](./relative1.md) # relative to opened file
[link](/relativeToProject.md) # relative to opened project
All of them tested in Visual Studio Code and working,
Note: The absolute and relative to opened project path work in editor but don't work in markdown preview mode!
If you have spaces in the filename, try these:
[file](./file%20with%20spaces.md)
[file](<./file with spaces.md>)
First one seems more reliable
This is a old question, but to me it still doesn't seem to have a complete answer to the OP's question. The chosen answer about security being the possible issue is actually often not the problem when using the Firefox 'Markdown Viewer' plug-in in my experience. Also, the OP seems to be using MS-Windows, so there is the added issue of specifying different drives.
So, here is a little more complete yet simple answer for the 'Markdown Viewer' plug-in on Windows (and other Markdown renderers I've seen): just enter the local path as you would normally, and if it is an absolute path make sure to start it with a slash. So:
[a relative link](../../some/dir/filename.md)
[Link to file in another dir on same drive](/another/dir/filename.md)
[Link to file in another dir on a different drive](/D:/dir/filename.md)
That last one was probably what the OP was looking for given their example.
Note this can also be used to display directories rather than files.
Though late, I hope this helps!
Thank you drifty0pine!
The first solution, it´s works!
[a relative link](../../some/dir/filename.md)
[Link to file in another dir on same drive](/another/dir/filename.md)
[Link to file in another dir on a different drive](/D:/dir/filename.md)
but I had need put more ../ until the folder where was my file, like this:
[FileToOpen](../../../../folderW/folderX/folderY/folderZ/FileToOpen.txt)
If the file is in the same directory as the one where the .md is, then just putting [Click here](MY-FILE.md) should work.
Otherwise, can create a path from the root directory of the project. So if the entire project/git-repo root directory is called 'my-app', and one wants to point to my-app/client/read-me.md, then try [My hyperlink](/client/read-me.md).
At least works from Chrome.

File path in app.config file ? Window application?

In window application, I am accessing one file and the file path is declared in app.config file. After that I create exe for that application. Now the problem is when path is changed in app.config file when setup is created is this changes updated in setup or not?. If yes, how can I do that?
Its better to use relative paths, but As I understand, you cant use they.
So, only one possible solution in my opinion - define file path during instalation or add special dialog on programm first run to let user to define the path.

How to copy resources from one executable to other

Ok my question is a little odd. But here we go.
I am trying to develop an executable file "wrapper" and a console program. The task of the console program is to copy Icons and Version Informations from another exe file to the wrapper file so that both the wrapper file and the exe file looks exactly same. Apart from that the exe file is appended to the wrapper file at the end. So that when the wrapper is executed it can extract and execute the appended exe file.
My question is how do I create the wrapper file so as to accomodate the Icons and Version info from other exe file ? I mean How should my resource file be ?
And next is How to copy Icons and version info. I hv searched and found a few codes and MSDN instructions but everyone of them uses FindResource, LoadResource, etc. But by following this method, I am losing the original contents of the wrapper file. The size of my file reduces from originally 67kb to 14kb and when I open up in notepad, I see lots of contents are gone ...
can anything be done by using SHGetFileInfo() ? This can be used to get HICON from the exe file. but how do I use this HICON to replace the icon resource in the wrapper file ??
The basic approach in your previous question is correct. You definitely don't want to be mucking around with SHGetFileInfo and HICONs. The type of resource shouldn't matter.
Your wrapper should start with no resources. This ensures, for example, that any icon you add will be both first and lowest numbered and thus guaranteed to be used as the app icon.
To understand what's happening with your code, use a tool that can view the resources in the resulting exe. Visual C++ Express can't do this, but the paid versions can. Alternatively, Google turns up a bunch of free utilities to do this. Here's one, I don't know if it's any good. The page also contains links to some other tools.

Resources