Deploying C# application on any computer - c#

I am new to C#.
I have a program that I want to work on any computer.
How do I compile the application to work on any computer without dependencies?
A simple hello world let's say:
class MainApp {
public static void Main() {
// Write text to the console.
Console.WriteLine("Hello");
}
}

In order to run a .Net application, you need the .Net Framework installed on that computer.
If this is a problem for you, pick another language.

To compile the application, you can either use Visual Studio and choose the Build Solution from the Build menu.
You can also use the command line and csc.exe
To compile your application via csc you will need to execute the following statement in the command prompt.
csc YourFile.cs
Any computer attempting to execute the assembly will need the .NET framework installed.

The other computer will need the ability to run .NET programs.
There are two ways that I know of that you can run your software on someone elses computer assuming they meet the above requirement.
The first way is to go into the bin folder and get the executable
(or the .exe file) this can just be distributed and ran. This is ussually in a path simular to this C:\Users\username\Documents\Visual Studio 2012\Projects\YourProjName\YourProjName\bin
The second way is to create a click once application. How to create a click once
On a side note, if you want your program to run on almost any machine I would choose a different language. Java is pretty popular and it is VERY close to C#. Many colleges focus on Java as well so it gives you a leg up if you ever go to school for it. That said, I did my whole degree in Java and ended up in a .NET shop so I had to relearn a ton of stuff.... On second thought, just learn everything...

Related

How to embed the version information in the executable file name when building C# application in Visual Studio?

This question is a complement for the post How to change the output name of an executable built by Visual Studio.
After reading this post I did the following:
Firstly, I followed the answer for this post and I could define the executable file name successfully.
Now, I would like to know if instead of only define the name as "Demo.exe" as mentioned in the example post above, it would be possible to embed the version defined in AssemblyInformationalVersionAttribute or in AssemblyVersionAttribute in the built file, resulting in something like "Demo_v1.0.0.0.exe"?
I'm developing my application in C# WinForms, using Visual Studio Express 2017.
Why would you want to change the name of the executable? Whenever you try building a Setup for your application, you need to change the Setup to include the new file. And when you install an update, your Setup needs to know all versions of your executable in order to delete the old version. That's just not what you want to do.
If you want to keep all versions of the software for yourself, come up with a different solution, e.g. moving the executable into a folder which has the version number.
That said, I have done this for Setups, so customers can download different versions of the Setup. I did that using a commercial tool called Visual Build, but there are other build automation tools available. So, my answer is: set up a continuous integration / continuous delivery pipeline (CI/CD) and automate the step there, not in Visual Studio.
From the project properties, you can add Post build event command line to rename your exe
pseudo
Maybe you can create another console renamer.exe which reads version defined in AssemblyInformationalVersionAttribute or in AssemblyVersionAttribute of your app and renames it and then call that renamer.exe from Post build event command line
write a powershell script to rename the newly built exe and call that script from Post build event command line

Does Visual Studio conceal some details of execution environment of c#?

I am very new to c#.I started c# today.I don't know execution environment of c# very well.
I can explain my question more by taking netbeans as example.
When we create JSP project in Netbeans then it creates web.xml by itself.
This file is needed to deploy the project.So if we want to create JSP program without using netbeans then we have to create web.xml by ourself.
So my question is does VS also create files by itself that are very neccessary to run C# program?
Thanks
Save to a hello.cs file:
class P { static void Main() { System.Console.WriteLine("Hello World"); } }
then from a command prompt in the folder of hello.cs do:
%Frameworkdir%\v4.0.30319\csc.exe hello.cs
If you haven't done errors, you have compiled your first program. You don't need Visual Studio to do it.
So, yes, Visual Studio clearly generates all the files needed to compile a simple program (even a complex one, if you direct it correctly), considering that a .cs file is in truth all you need.
What Visual Studio 2012 generates for a console program is:
a solution and a project file (that are used to organize your source code and remember references to libraries)
a configuration file, (not necessary, but Visual Studio 2012 adds it just to write that the program will run on .NET 4.5)
your source code program
another source code program (AssemblyInfo.cs) so you can add copyright/other metainformations to your exe.
some random files that Visual Studio uses as cache (the .suo files)
I assume you are speaking only of ASP.NET C# execution, since you mention web.xml for Java. There is a web.config that is probably very comparable to that, since I am not a Java developer I really do not know.
The web.config is actually XML and it contains things like connection string to the database, HTTP modules, HTTP handlers, custom application settings that go above and beyond what is part of the core .NET Framework.
The difference lies in that this is not a unit of deployment, like it sounds the web.xml is in Java, but rather web.config is a run-time explanation of settings that the web server (IIS) uses properly run the application.

How can I programmatically build my solution files from C# code?

I have a large solution containing many projects with one of them being a Setup project. There are also many current versions stored in separate branches. I have a build tool that used to work in .NET 2, but hasn't worked since we upgraded to .NET 4.
Internally, the new .NET 4 version of the build tool uses Microsoft.TeamFoundation.Client.RegisteredTfsConnections.GetProjectCollections() and versionControlServer.GetAllTeamProjects(false) to get a collection of TeamProjects from my TFS source control server.
I then display them visually in the UI and when a user clicks on a particular solution version, the application calls the following to get the latest for that solution version:
workspace.Get(new string[] { serverPath }, VersionSpec.Latest, RecursionType.Full,
GetOptions.GetAll);
The application used to build the solution files and this would include the Setup project. At this stage, the setup project would create an MSI that the application could be installed with. It is this last step that I am having problems with.
I need to be able to programmatically build the solution that the user selected using C# code. The working .NET 2 code for this was as follows:
Process process = new Process();
ProcessStartInfo processStartInfo = process.StartInfo;
processStartInfo.FileName = processName;
processStartInfo.Arguments = string.Format(" \"{0}\" /BUILD \"Release|Any CPU\"",
solutionPath);
processStartInfo.WorkingDirectory = processDirectory;
process.Start();
There is no error when this is run, but it no longer launches Visual Studio and builds the code. Clearly, this was a poor way to do it initially, but I can't find the 'correct' way using the TFS classes.
I also tried running MSBuild.exe directly (similar to the above example), and this does build the solution, but for some reason does not build the Setup project that produces the MSI. Note that I do NOT use any manually created build files.
Unfortunately, useful documentation for the Microsoft.TeamFoundation namespace is hard to find! I'm hoping that someone here has made use of these classes and can direct me to a solution to this problem.
If at all possible, I need to use .NET classes (eg. not Process.Start) as I really need to know when the build has finished as well. I can however set up a FileSystemWatcher object for this if this is asking too much.
msbuild skips the installer projects because it doesn't know how to build them. FinalBuilder shells out to devenv.exe to build these.
Calling `devenv /build "Release|Any CPU" /project "MyInstaller.vdproj" should run the build you need from the command line, without starting the VS GUI. Try it!
With all that said: We, too, use FinalBuilder, and VS Installer is deprecated, so you'll probably want to plan on replacing that.
It turns out that the problem was unfortunately completely unrelated. The .NET 2 code that was to be updated had hard coded 'Program Files' into the devenv.exe file path - however the new computers are 64bit and Visual Studio 2010 is installed into 'Program Files (x86)'!
So this means that the above code sample DOES work and WILL allow me to build the solutions from C#. It's not the object oriented way that I would have preferred to do it, but after all your comments, I'm just glad to have got it working at all.
Thanks for your time everyone.

Trying to launch a Visual Studio C# program from inside another one

I am having great difficulty getting a Windows program, written in VS 2008 C#, to launch another type program. I've put a main program to offer you the ability to launch some other VS 2008 C# programs. When one of them is selected the following code is intended to do the launch:
System.Diagnostics.Process.Start(#"C:\Documents and Settings\rat\My Documents\Visual Studio 2008\Projects\PV_002_082708\PV_001\PV_001\bin\Debug\PV.exe");
It works, but each user will have a different path. The path shown is for my computer. The code would have to know where each persons program was installed! In the past, I could easily call a program that was in the C:\Program Files location because that's where the MS Package & Deployment program put the programs. VS 2008/2010 doesn't put them there!
I'm sure I am not knowledgeable about Visual Studio 2008/2010 to know if there is a way around this, so any help would be greatly appreciated.
System.Diagnostics.Process.Start(System.Io.Path.Combine(System.Windows.Forms.Application.StartupPath, "PV.exe"));
assuming that the program is in the launched application's path.
or...
System.Diagnostics.Process.Start(System.Io.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), #"Visual Studio 2008\Projects\PV_002_082708\PV_001\PV_001\bin\Debug\PV.exe"));
assuming you want to open the project from it's place.
Once you create the installation package you can set the install path to some fixed path that users will not be able to change and than use that from your code (not very user friendly but it would work) or insert the user chosen path value into the registry and get it from there instead.
May want to have a look at this thread. How-to as well as lots of pro's and cons.
Embedded a *.exe into a dll
If you see the first answer to his question, I think this would work for you.
Simply he is saying, you add a resource file to your project, then when the user runes your program, it extracts your exe file that you added as a resource file to somewhere you know on the end users maching.

Best solution for making an Autorun application?

For my work I need to create a Autorun application for a CD for our client. The problem is all I'm a C# programmer by trade and .NET does not make for the best autoruns since not all computers will have .NET installed on them.
What is a good solution that will work on Win98-Vista computers?
The answer to this question is really one of preference. Technically, anything can be instructed to open as an autorun. The autorun.inf file is simply an instruction file that Windows knows how to read in order to determine what it should do when a CD is inserted. That could be an application (written in any language you choose), a powerpoint presentation, opening a link to a website, etc. As long as you follow the rules of the autorun.inf file:
http://autorun.moonvalley.com/autoruninf.htm
There are many small autorun-utils (some free) that are configurable. I would go for one of those.
http://www.ezau.com/latest/articles/083.shtml
You need two things:
Follow this steps (http://support.microsoft.com/kb/324733 OR http://support.microsoft.com/kb/888469)
When your application is ready (with bootstrapper), you'll have to create a autorun.inf file on CD root path. Search 'autorun.inf create' on your favorite Search Engine.
It is possible using Mono's bundling feature:
"The resulting executable is self contained and does not need the Mono runtime installed to run."
Mono is an Open Source .Net clone and should be able to run most .Net applications. See "Bundles" section here: http://www.mono-project.com/Guide:Running_Mono_Applications
You could use .hta file on CD to launch as splash page, and from there detect if .NET is installed (using COM from HTA file) and then run your custom executable.
.hta files are what Microsoft SQL Server (and most of their others) installation cd uses from memory, easy to make them look very professional (its just html in the background basically)
You can include the runtime with the CD, but you'll have to install it before running your app. You might look into just popping open an html file from the CD.
Otherwise, you can look at writing a small native program that can prompt to install the runtime if it's not there, or run your .NET app if it is.
Try Delphi; it's by far the best way to create native win32 application nowadays.
It creates slick stand-alone .exe files with rich GUI's that don't need any runtime libraries or other annoying dependencies. Works on any windows machine.
There was a program from Macrovision called "Demoshield" that worked well back in the day. I'm not sure if its available anymore, but it was an alright program for creating the auto-run programs.

Categories