简体   繁体   中英

How to Run c# application through Command Prompt?

I have Two classes. Test.cs, Entity.cs.

I have put this two files in one folder "CommandTest".

*Test.cs Class

namespace Demo
{
 public class Test
{
 public static Entity entity= new Entity();
 public static void Main(string[] args)
{
Console.WriteLine("Demo");
}

*Entity.cs class

namespace Demo
{
public class Entity
{
Console.WriteLine("entity");
}
}

When I am Trying to run it through "Visual Studio Command Prompt". It shows Error,

Test.cs(10,28): error CS0234: The type or namespace name 'Entity' does not exist in the namespace 'Demo' (are you missing an assembly reference?)

I am not getting why it is shows error. Because both class have same namespace. How can I run it through Command Prompt.

Thanks.

You should use all files when calling csc . It doesn't try to find the code files itself. Try this:

csc /out:Test.exe Test.cs Entity.cs

Or, maybe easier:

csc /out:Test.exe *.cs

Also, read the related MSDN article .

Don't forget to add this code block in a method too:

namespace Demo
{
    public class Entity
    {
        public void SomeMethod() /* here */
        {
            Console.WriteLine("entity");
        }
    }
}

you need to provide name of all classes explicitly. so in your case you will execute

csc.exe /out:ExecutableName.exe Entity.cs Test.cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM