Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, May 28, 2018 10:02 AM
Hi,
I hope you can help. I have recently downloaded Visual Studio 2017.
I would like to know how to run the Hello World code (written on Notpad) from the Visual Studio Command Prompt (or maybe the Windows Command Prompt) without Visual Studio opening up.
This is the well-known code:
Module HelloWorld
'every console app starts with Main
Sub Main()
System.Console.WriteLine("Hello World")
Console.Write("Press any key to continue...")
Console.ReadKey(True)
End Sub
End Module
I saved it with a vb extension. Yet when I bring up the Windows command prompt, and write in the path to the notepad with the vb extension (C:/Users/lenovo>vbc Helloworld.vb), it says 'vbc' is not recognised as an internal or external command, operable program or batch file.
The same results happens if I use the Visual Studio Command Prompt: C:\Program Files\Microsoft Visual Studio (x86)\2017\Community>Users\lenovo>vbcHelloworld.vb
I know everyone says use the module in Visual Studio Console NET, but I'd still like to know how to use the Command Prompt on its own - and I cannot understand why it won't work.
I have tried all sorts of combinations in the Command Prompts, added to PATHS in The PC Folder etc etc (and that has created a problem now too).
I'm pretty frustrated at the moment and hope someone has an answer I can understand and make work.
Thanks.
All replies (9)
Monday, May 28, 2018 10:30 AM
In start, there should be a Developer Command Prompt, this should be in the Visual Studio 2017 folder above the Visual Studio 2017 shortcut to run Visual Studio.
If you run this, it will automatically set up the command prompt to include the path to vbc.
This is the easiest way to do it.
This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
Monday, May 28, 2018 11:26 AM
There is an assembly created as soon as you do start in the bin folder of your project in debug or release depending what you told to do.
For a module that is an Exe
You can copy (or run it direct from that bin folder) that to your program directory and run without that vbc.
Vbc is the compiler that is not used anymore (it still exist somewhere on your computer)
Success
Cor
Monday, May 28, 2018 12:13 PM
Thanks Darran.
Unfortunately I have tried that. If I save it in the Community Folder, it says Helloworld.vb could not be found.
If I save it in any of the folders under the Community Folder, or any of their subFolders, it says, Access is Denied.
Unless I'm missing a step?
With any luck, you'll know what I'm doing wrong and be able to make suggestions?
Thanks.
Monday, May 28, 2018 12:17 PM
Thanks Cor,
I would like to write in Notepad, save the file, then go to either the Desktop App Command Prompt, or to the Visual Studio Command Prompt Shortcut without open Visual Studio.
I think you're suggesting using the Visual Studio Command Prompt created under External Tools in the Tools tab in Visual Studio itself?
Monday, May 28, 2018 12:45 PM
By the sounds of it you aren't that familiar with the command line environment. I would suggest at the very least learning how to change directories.
Anyway, the developer command prompt puts the Visual Basic compiler into the path, so you can run it from any directory. You see, if you use dir to see the contents of the current directory:
you should notice that there is no vbc.exe file there, yet it is possible to run it from that directory. In fact, the compiler that it should be running is in C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn. This is the power of the path.
This means that if you can run it from one directory, you can run it from any directory:
and so on. So where does this mean you put your source files? The answer is anywhere, but usually your user profile is the best place. You have full write access to it by default. When Visual Studio is first run, it should create a directory in your user profile where you can put your source files, this is C:\Users\your user name>\source (assuming C is where your user profile is, substitute this for the correct drive letter if it isn't).
You can place your source files in there. But you will then have to direct the command line compiler to find those files, since they wouldn't check this location by default. There are two ways of doing this.
1) Giving the full path to the source file to the compiler:
You will have to use the /out option for the compiler to tell it where to write the output file to otherwise it will try to write to the current directory. You can find the full list of command line options by running vbc /c or searching online.
2) Changing to the directory where the source file is, and then running vbc there:
In this case you don't have to use the /out option to tell the compiler where to write the output executable, this is because it defaults to the current director. If you notice, you don't need to put the full path of the source file in either since it defaults to looking in the current directory for the file.
This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
Monday, May 28, 2018 12:58 PM
Just to add a bit of extra information, this is a common theme across tools. For example, using the C++ command line tools:
Notice how this is like 1) above? For cl.exe, the source file is passed in using the full path, the Fo option points to the directory where the object file will be written. Then with link.exe, the object is passed it using the full path and the out option gives the full path to where the output file should be written?
If you want to make this like 2) above then:
While I used a different directory than the vbc example, this doesn't matter.
Also, when you tried to write to the Visual Studio directory and you were being told that you don't have permission to do that, this is a security feature of Windows. To protect important programs, certain directories, like C:\Windows, C:\Program Files and C:\Program Files (x86) have a requirement that you need to have administrative rights to write to these directories, and this is a right that regular user accounts don't have by default. This is a good thing.
This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
Monday, May 28, 2018 3:32 PM
Thanks Cor,
I would like to write in Notepad, save the file, then go to either the Desktop App Command Prompt, or to the Visual Studio Command Prompt Shortcut without open Visual Studio.
I think you're suggesting using the Visual Studio Command Prompt created under External Tools in the Tools tab in Visual Studio itself?
That we did in 1998. However, .Net programs are not made for this anymore. I made this once, but as a learning tool. But why. Visual Studio is Freeware.
Nevertheless, it is still possible. Like I wrote you have to find the folder of VBC and then this
/en-us/dotnet/visual-basic/reference/command-line-compiler/compiler-options-listed-alphabetically
Success
Cor
Monday, May 28, 2018 4:42 PM
There is a modern use case for this, which is why the Build Tools for Visual Studio 2017 package exists. This package basically has MSBuild, the command line compilers and SDKs. It doesn't include the Visual Studio IDE.
Anyway, the modern use case for this is continuous integration (or build servers). If you have a server that is used to automate building, then you generally don't need the VS IDE.
There are also other build systems, and as long as you have the command line tools available, you can easily use one of these.
This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
Wednesday, May 30, 2018 11:59 PM
Hi Daran,
Thank you for your help. I now know a lot more about command line navigation from researching it after reading your post. Actually, I found it interesting.
I also now know what I did wrong. I have to save notepad as helloworld.vb instead of vbc helloworld.vb
Next I navigate in the Developer Ccommand Prompt to the folder holding helloworld.vb - using the change directory (cd \ command as you showed and click Enter.
Then I type in vbc helloworld.vb (vbc presumably means calling the Visual Basic Compiler to run and create an executable file).
The I click Enter to create the executable file that magically appears in the folder holding helloworld.vb.
Then I type in helloworld - and viola - the executable file runs.
Success.
Initially I wanted to reply using images but could not as I have not yet been verified. Since then I have found out what to do, and have now come back to thank you for your time and help in getting me to the next step.