Defining relative paths for my Visual Studio Project files in c# - c#

So I've downloaded a framework to write code within, and I needed to link the source code to an executable file to allow the code to run. However, moved the root folder (containing all files related to the project) from the folder it was in previously, and now nothing works. I've been attempting to change the paths for the files to be relative to the root folder within the properties tab, but I'm not sure how to go about it.
Path to Executible - C:\Users\me\Downloads\Competition-Pack-v43-GeometryFriends-updated-14-08-2016\GeometryFriendsAgents\GeometryFriendsGame\Release\GeometryFriends.exe
C:\Users\me\Downloads\Competition-Pack-v43-GeometryFriends-updated-14-08-2016\GeometryFriendsAgents\GeometryFriendsGame\Release
Is there an equivalent to using Macros to define paths in C#, like you get when you build a C++ project?

Add the .exe as a resource (non embedded) to your project, and set the "Copy to output" property to copy always. This ensures the needed exe will always be relative to your build path. The simply use relative paths in your code.
Example:
(This is all done in VS's solution explorer window)
Right click your project, and add a new folder, name it Externals.
Right click the folder, click Add, and then Existing item. Locate your exe, and add it.
Now right click the exe, select properties, and set the "Copy to output" property to anything other than "Do not copy".
Now, anywhere in your code, use the path "Externals\YourExe.exe". The double backslash is to unescape the backslash. An alternative would be to use #"Externals\YourExe.exe", which turns the string to a path during compile time.
There you go, when you build, the exe will be automatically copied to the build directory's "Externals" directory, and always be relative.

It turned out in the end that Visual Studio has a Macros option for this very task.
When you're looking at your include directories window as in the image above, there's a button labelled Macros. Click that, and it gives you access to a list of predefined paths. They all start with a $, like the ones already provided that point to the include folders for Visual Studio.
In my example above, I added $(MSBuildProjectDirectory), which points to the exact folder that I needed on the drive my code folder was located on. Hope I can help at least one or two people with this, cause it drove me insane until I came across it.

Related

how to set image in Visual Studio C# Windows Forms with Image.FromFile

So I'm trying to set a custom image for a form application I've made. The images I'm trying to target are in a folder called "Images" on the same level as my Solution file. The solution file is a C# windows forms (net core framework) solution. It's a basic form app that I want to display an image based on a users selection, however right now I get an unhandled exception everytime I try to set the image with this code:
picFood.Image = Image.FromFile("../../Images/burger.jpg");
The exact error is "System.IO.FileNotFoundException: ../../Images/burger.jpg"
In another totally unrelated solution this works. Folder structure is the same. A folder called Images, on the same directory level as the .sln file holds the images there. They're in my solution explorer and everything. I've tried this with one "../" and no "../" as well so I'm not sure what to do from here.
Files with relative paths are opened relative to the working directory of your application.
In this case, when launching from within Visual Studio, the default is the bin folder where the compiled application is put by default.
So if your binary is in <project dir>/bin/Debug/App.exe this path will resolve to <project dir>/Image/burger.jpg.
If you have changed something in your build configuration, or your application switches directory at runtime (e.g. via Directory.SetCurrentDirectory), this path may be different than you expect.
To understand your issue, I suggest you start looking at what your working directory is. You can obtain that in your code via Directory.GetCurrentDirectory().
You can also resolve your relative path using Path.GetFullPath.
Print these two values to see where your program attempts to load the file from.
Keep in mind that any image files you put in the solution/project folder will need to be copied with your binary if you want to use them.
To use relative paths without .. you can copy them alongside your binary during compilation, see:
VS2010 How to include files in project, to copy them to build output directory automatically during build or publish and Copying Visual Studio project file(s) to output directory during build for how to do that.

Visual Studio 2017 can't find folder that exists in the solution

I have Visual Studio 2017 project in which I need to open a folder named Devdog.General, which is also included in my Solution. However, it won't recognize it as an existing folder and gives me a missing directory / namespace error when I try to import it. See screenshot for more info.
Try this button:
I had a similar problem the other day and it had to do with files that Visual Studio didn't know were part of the project because they'd been created externally (and that included folders). Visual Studio could see them but it was hiding them from me.
After clicking that button, you will be able to right click on the folder and select "include in project" which will include the files for compilation and navigation.
First, try selecting any of your "*.cs" files inside this "General" folder and check in the "Properties" view if the Build Action is configured to Compile. Visual Studio will only compile your file if it has the correct build action.
Secondly, open that "*.cs" file and verify if the namespace defined inside it is correct. For C#, folder structures don't matter much: what really matters is the namespace you have defined your classes in. You can define your classes in namespaces completely different than the folder they are included in your project. That is completely arbitrary, and up to your organizational needs.
An image illustrating what you need to check (and where) follows.
Things to Check
1.) Check the namespaces inside .CS files - they collectively create usable namespaces you are referencing - not folder names. If you create a few files, then move them to a folder, and create newer files inside that folder they will have different namespaces. The newer files will have the default Namespace value followed by .FolderName. So be sure to check that out.
2.) CSPROJs can get hosed and lose reference to folders that display in the UI. Remove the folder from the project (through the VS2017 UI, right click and choose that option). DO NOT DELETE. Then recreate the folder in the UI (it shouldn't let you, navigate to the folder directory manually and rename the folder.OLD temporarily). Once the folder is rename, try recreating the folder. Upon success move all your CS files into the new folder from folder.OLD. After files and folders are back to where they were essentially, then in Visual Studio, "Add existing items" on your .cs files. This recreates the CSPROJ references one by one.
3.) Unload CSPROJ, right click and Edit CSPROJ to manually check all the .CS references in ItemGroups. Make sure its myfolder\myotherfolder\mycsfile.cs.
4.) Remove reference to other projects that contain namespace, and re-add them. Verifying, one by one, the namespaces begin reappearing in Intellisense as recognized.
5.) If you try the above step, close Solution, close Visual Studio, navigate to SLN folder container, and delete hidden folder .vs and then reopen everything.
What happens is sometime a folder rename or file transfer doesn't propagate to the .CSPROJ folder, a namespace then doesn't get intellisense cached, and errors galore show up.
Please check your folder name and namespace name. probably folder name and namespace did not match
enter image description here

Two C# project files for VS2010 and VS2012 in the same directory

I'm writing a C# project which I want to compile both under VS2010 and VS2012. I want to have two different .csproj files for that, even though VS2010-project can be directly included in VS2012-solution. I want two different project files because of different target .NET-versions.
Additionally, I want both projects to be named the same within different solutions for VS2010 and VS2012. The both should share the same .cs-files and, possibly, some resources.
Easy task? Strange thing, but I don't know, how to do that properly.
First approach, doesn't work: name project files as MyProject10.csproj and MyProject12.csproj, leave both in the MyProject folder. But in this case they are displayed in solution as MyProject10 and MyProject12, not simply MyProject. If I rename one to MyProject in solution explorer, it automatically renames the corresponding project file.
Second approach, works, but has drawbacks. Move project files into different subdirectories, named VS10 and VS12, manually change include paths in .csproj-files, adding ..\ as a prefix. In this case I see strange shortcut-like arrows under the icons in solution explorer.
The only bad thing for the second approach is that when I add new files in the project using solution explorer, this file is placed in the VS10 subdirectory, not in the root project directory. But it seems like project directories for .csproj-files are the directories containing the file by design, and I use the dark side of the force which I really don't want to do.
Any better ideas?
Worth giving a try is changing the name of the output assembly in both projects to one and the same. The project files are still going to be different, but the output assembly will be with same name. Also be carefull where is the output directory. I think this way you can sovle your problem.

Application Executable Path C#

I've added an executable to my Visual Studio 2010 C# Solution. In the properties of this executable, the executable path is a full path ("C:\Test\MyProgram\MyProgram.exe")
When I deploy my solution (with installshield) on a new PC, the executable is part of the deployed solution together with some source files and the solution file. So far so good.
But when I open the installed solution file (in Visual Studio 2010),
I'm not able to build it because It can't find the executable in the specified path:
("C:\Test\MyProgram\").
Here is the question: How can change the full path of the executable, so it gets the path of where the solution is installed on the new PC. Something like :
"[InstallDir]\MyProgram.exe"
Thanks
Update: I found out that you can use relative path in Application's Executable path. Thanks for all your answers.
You could use TargetDir property
I am just thinking off the top of my head here. There may be a much simpler way. I'm thinking you might want to create a Custom Action that runs at the end of your installer that manually opens the .xxproj file, and manually edits the path of the reference. As another poster stated, you can get the new path from the TargetDir property: http://msdn.microsoft.com/en-us/library/aa372064%28VS.85%29.aspx
Example of creating Custom Actions: http://msdn.microsoft.com/en-us/library/9cdb5eda(v=vs.80).aspx
Add your Executable to your Project TO The Main Dir, right click-> Copy To OutPutDirectory -> Copy
this is easiest way to make your file to copy to your target dir, and have your SourceControl Visual studio plugin manage it.
Your executable should be be somewhere in your project source files structure, perhaps in a subfolder. When you add this executable file to project it should then be added on a relative path which is what you want. If this is not happening you should manually edit csprj file. To do this, right click on project, unload it, right click again and edit project file.
Of course, your executable file should have its property Build Action set to None and Copy to output Directory to what you want.
If for some reason you cannot add this executable directly into your project files structure I'd suggest to use pre-build event to copy it from where it exists into your project files.

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