Application Executable Path C# - 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.

Related

How to deploy a C# (ClickOnce?) project that is a wrapper on a command line .exe

I have a C# project which is basically a GUI used to call other executable(s) with command line arguments. (The command line exe was actually built with cygwin and uses the cugwin DLL).
So the directory structure that needs to exist once the app is deployed (via setup.exe or whatever) is this:
install dir ---> MyApp.exe
MyApp.config
(dir) bin ---> cmd1.exe
cmd2.exe
cygwin.dll
Now this ought to be simple, but whatever I try, I cannot get the bin directory and its contents to be copied when I install on a second machine with setup.exe. I tried:
- adding them as resources
- setting "Build Action" to Content
- setting "Copy To Output Directory" to Always
But the bin directory was never copied across when I did this. I have tried searching here and elsewhere but I am still at a loss.
Should this be a "ClickOnce" project? (what does this even mean - are there also ClickTwice and ClickUntilYouCanClickNoMore projects - ok, excuse me ...)
Also do I get setup.exe to put this app somewhere sane like C://Program Files/MyOrg/Myapp - instead of being buried somewhere in the user's profile?
(Using VS 2019.)
Since I cannot add an image in a comment, I am adding them here. Here is how my folder structure looks like.
I have a .p12 file and have marked it as "Copy to Output Directory" to "Copy always"
When the project is built, the file gets copied inside bin folder with the file's folder structure which includes "keystore" folder as well.
As a curiosity, I just tried out renaming my folder "keystore" to "bin" (I had to delete existing bin folder), and then added the .p12 file to it. The project compiled, generated new exe file and also copied .p12 file with appropriate folder structure. I am not sure what means to building and generating a Setup.exe though. You can try and let us know if it worked for you.
To package the specified file into ClickOnce, you just need to add it to Application Files....
The following is the folder structure.
And then confirm it has been added into Application Files....
Last step, publish it. And you can access the image like this.
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + #"/img/a.jpg");
}
As to put this app somewhere sane, I am afraid the answer is no.
ClickOnce's installation path cannot be changed. You can find it at C:\Users\username\AppData\Local\Apps\2.0.

Defining relative paths for my Visual Studio Project files in 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.

How can I run an embedded .exe file in C# Windows Forms applications?

How can I run an embedded .exe file (installer) in C# Windows Forms applications the easiest way?
I just want to click a button and an installer should open. My .exe file's name is setup.
If I try Process.Start(setup.exe); I get an error:
The name 'setup' does not exist in the current context
And if I try
System.Diagnostics.Process.Start("setup");
it will open folder C:\Windows\System32\setup.
If what you expect is the easiest way, then don't embed setup.exe as a resource, but as Content.
(Add setup.exe to your project, and right click on setup.exe in Solution Explorer to edit properties, set as content, and select Copy if newer.)
Another option if setup.exe is a project of your Visual Studio solution, is to automatically copy setup.exe in the output directory of the launcher project: Add a reference to setup.exe if it belongs to the solution, so it is automatically copied every time you compile and there was a change.
Last is the code that is pretty easy - you already have it, actually:
System.Diagnostics.Process.Start("HelloWorld.exe");
If needed, you can change the current directory:
Environment.CurrentDirectory = #"c:\someSetupExeDir";
But better, you can use Path.Combine:
String fullPath = Path.Combine(directoryPath, fileName);

open file from folder in visual studio 2012

I had a folder on my desktop with files in it. I copied that into the folder of my solution and in the solution explorer I referenced that folder into the solution. However, Im not able to open files in that folder with a relative path.
The relative path from the cs-file would be "../FolderIAdded/blabla" as seen in the solution explorer. But in the windows explorer, the path is differen of course:
Solutionfolder
- SolutionFolder.sln
- Solutionfolder.v11.suo
- SolutionFolder
-- bin
-- obj
-- Properties
-- TheFolderIAdded
-- App.config
-- Form1.cs
-- etc.
Here, it would be "FolderIAdded/blabla"
Where do I have to put that folder?
My goal: I want to be able to open files from that folder in my c#-code with a relative path.
You're assuming that your program runs in the directory where your source code is located. That's not the case. Depending on your configuration, your program will execute from a directory inside Solutionfolder\bin.
One possible solution is to copy the file(s) to the output directory when you build your project.
Another alternative is to embed the files into your application's assembly at compile time, although this precludes editing of them after deployment. To do that, set Build Action to 'Embedded Resource', then you can access them using the GetManifestResourceStream method of the Assembly class. The filename you need to give it will be derived from the path within the project structure, so in your example it would be "TheFolderIAdded.Filename.ext".
Yes, that's a dot, not a backslash.
Assuming the files are embedded in the same assembly the code that wants to read them is in, the code will look something like
var assembly = Assembly.GetExecutingAssembly();
using (var stream =
assembly.GetManifestResourceStream("TheFolderIAdded.Filename.ext"))
using (var reader = new StreamReader(stream)) {
string fileContents = reader.ReadToEnd();
}
I don't think it's a good idea to write relative path from .cs file. Better build the path base on where the application is executed:
One example, there are plenty other on the web: How can I get the application's path in a .NET console application?
(Your application is not running in the solution's root folder but where the .exe file is locatated. For example when you debug a desktop application, it runs typically from [solution folder]/bin/debug/ )
Then make sure the file you want to open property Copy to Output Directory is set to Copy Always or Copy if newer. (Right click on the file in your Solution Explorer and click on "Properties" to be sure to access it.)

how to set path in MSI installer?

I am using VS2008 setup project to build our msi installer. I have moved some dll files from the default location (root folder of the installation) to a custom bin directory. I think I need to set path in somewhere (registry?) to tell the application the new loation of those dll files. How to do this? thanks,
Just open up the .vdproj file in notepad and search for the dll files you moved. Then adjust the dll path (usually called SourcePath). You should not edit anything in the Registry! Only edit this file.
If these are references in your project, I would remove the references and add them back using the Browse tab of the Add Reference dialog.

Categories

Resources