Text node cannot appear in this state (Line 1, position 1) - c#

I'm just starting to learn C#, and I am using Koding to develop (since I don't currently have access to my Ubuntu machine). I'm attempting to get the Hello World program to work, but the compiler I'm using (which is Mono) isn't taking it.
This is the code that I'm using for the program:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1{
class Program{
static void Main(string[] args){
Console.WriteLine("Hello, World!");
Console.ReadLine();
}
}
}
I created a file, wrote the code, and attempted to compile it using xbuild program.cs but I got the error message Text node cannot appear in this state (Line 1, position 1).
What am I doing wrong here? Is it the compiler, the OS, the code?

xbuild expects a project or solution file (which are xml format), not a c# source file. You should pass that (e.g. xbuild program.sln), or if you don't have one then invoke the compiler directly by mcs program.cs or similar.
PS: You should have mentioned the command line you used in the original question, instead of in a comment later.

Related

.NET Assembly issue with MATLAB [duplicate]

I used the MATLAB Compiler to generate a .NET Assembly with a very little MATLAB code:
function output_arg = extest( input_arg1,input_arg2 )
output_arg = input_arg1+input_arg2;
end
I generated the dll with the wizard.
Within my Visual Studio project I added the reference to the generated dll (extest.dll) and to the MATLAB Runtime dll (C:\Program Files\MATLAB\MATLAB Runtime\v92\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll) as mentioned in the "Assembly Description".
This is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathWorks.MATLAB.NET.Utility;
using extest;
namespace DllTesting
{
class Program
{
static void Main(string[] args)
{
ExClass e1 = new ExClass();
}
}
}
It builds without errors an intelisense is working (so all references should be good from my understanding)
But when I launch it, the following exception gets thrown (on new ExClass()):
An unhandled exception of type 'System.TypeInitializationException' occurred in DllTesting.exe
Additional information: The type initializer for 'extest.ExClass' threw an exception.
Any suggestions what is wrong with this code or whats missing?
Try adding this before the class definition
[assembly: MathWorks.MATLAB.NET.Utility.MWMCROption("-nojit")]
Also make sure that the .NET version you use for assembly is the same or lower than the one used for your Visual Studio project.
Another solution might be adding the path of the MATLAB runtime (e.g. C:\Program Files\MATLAB\MATLAB Runtime\v92\runtime\win64) to the PATH Environment Variable.
If none of these helps, have a look here and here, you might have a 64/32 bit mismatch.

Unable to manually compile and use user-defined namespaces in c#

I had just started learning C# programming language and I'm trying to run code manually(using command line) and on Visual Studio both. But now I'm stuck on "namespaces". I had made two files, one containing user-defined namespace and another file will access that namespace features. I succeeded in running this code on Visual Studio but unable to run manually. The file containing only namespace is not compiling and giving error that "it does not contain a static 'Main' method suitable for an entry point". The two files are in the same folder. See the code below:
//file "Check1.cs"
namespace Ankur{
public class Check1{
public string a = "Hello world!";
}
}
//file "Checkit.cs"
using System;
using Ankur;
class Checkit{
static void Main(){
Console.WriteLine(Check1.a);
}
}
How Visual Studio is running that code and what should I do to successfully run this code manually?

AForge.Video.FFMPEG used in C#

I use Visual C# 2008 and want to write AVI file from bmp sequences.
I found AForge.Video.VWF but it's just for "vmw3" or "DIB " codecs and I want to use AForge.Video.FFMPEG but it got error.
For example I just code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AForge.Video.FFMPEG;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
VideoFileWriter vfw = new VideoFileWriter();
}
}
}
But I got this filenotfoundexception
{"The specified module could not be found. (Exception from HRESULT: 0x8007007E)":null}
In order to use AForge.Video.FFMPEG correctly, you have to be sure to include the FFmpeg dll's into your output folder. The easiest way is to add them into your VS project, go on their properties and set the Copy to Output Directory option to "Always".
The FFmpeg binaries used by AForge can be found on the AForge.NET's external folder, typically in C:\Program Files (x86)\AForge.NET\Framework\Externals\ffmpeg\bin
Not sure if this is still relevant, but downloading Accord.Video.FFMPEG will take care of this for you, just remember to build explicitly to x86
using Accord.Video.FFMPEG;
it's happened because this AForge.Video.FFMPEG.DLL depend upon another dlls. copy all that dll into output folder.

Windows Store Project - Assembly reference error when trying to run cmd commands from a C# file in VS2012

I'm doing some exercises with C# in the trial version of VS 2012. I want to execute a cmd command from a CS file. For this, I've tried Process.Start as well as System.Diagnostics.Process that are mentioned in these posts:
Run Command Prompt Commands
Execute CMD command from code
However, despite I added "using System.Diagnostics" and "using System.ComponentModel", I'm still getting "The type or namespace name 'Process' does not exist in the namespace 'System.Diagnostics', missing assembly reference" error. ¿Any suggestion so I can i get rid of this error? Thanks in advance.
This usually happens when you have Target framework = .NET Framework Client Profile, but DLL you reference is from .NET Framework (full). Make sure you have System.dll in your references from valid framework.
I just did the same - created empty console application with the following code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var prc = Process.Start("explorer.exe");
}
}
}
Works perfectly fine for me.
Additional thing to check is Intellisense - when you start typing "System.Diagnostics.Proc"... - does it show you dropdown with "Process" there?
UPDATE:
Windows Store projects are based on different version of target .NET Framework - .NET for Windows Store apps, which does not support functionality you need.
For more details do web search:".NET for Windows Store apps". Helpful links:
http://msdn.microsoft.com/en-us/library/windows/apps/br230302.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/br230232.aspx

Does not contain a static 'main' method suitable for an entry point

I began organizing my code to day into seperarate .cs files, and in order to allow the methods that work with the UI to continue to do so I would create the .cs code under the same namespace and public partial class name so the methods could be inter-operable.
My header look like this in four files, including my main core file that calls:
public shell()
{
InitializeComponent();
}
Header area of .cs files that work with the UI (and seem to be causing this new conflict):
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Threading;
using System.Collections.Specialized;
using System.Net;
using System.Runtime.InteropServices;
using watin = WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
using System.Web;
namespace WindowsFormsApplication1
{
public partial class shell : Form
{
Now when I try to debug/preview my application (BTW this is a Windows Application within Visual Studio 2010 Express) I get this error message:
Does not contain a static 'main' method suitable for an entry point
I looked in the application properties in Application->Startup object, but it offers me no options. How can I inform the application to begin at the .cs file that has my InitializeComponent(); command?
I've looked around so far without a solution.
The properties on each .cs file are set to 'Compile'.
I do not see an App.xaml file in my Solutions explorer but I do see a app.config file.
I'm still very new and this is my first attempt at an organizing method with c# code.
I was looking at this issue as well, and in my case the solution was too easy. I added a new empty project to the solution. The newly added project is automatically set as a console application. But since the project added was a 'empty' project, no Program.cs existed in that new project. (As expected)
All I needed to do was change the output type of the project properties to Class library
Change the Output Type under the Project > Properties to that of a “Class Library”. By default, this setting may have been set to a “Console Application”.
I had this error and solved it using this solution.
Right click on the project
Select "Properties"
Set "Output Type" to "Class Library".
Try adding this method to a class and see if you still get the error:
[STAThread]
static void Main()
{
}
If you don't have a file named Program.cs, just add a new Class and name it Program.cs.
Then paste this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Sales {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Select App.xaml and display its properties. Set Build Action to ApplicationDefinition.
App.xaml and its corresponding *.cs file must be placed into the root directory of the *.csproj file, i. e. not into a "Source" folder.
Had this problem in VS 2017 caused by:
static async Task Main(string[] args)
(Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater)
Adding
<LangVersion>latest</LangVersion>
to app.csproj helped.
Edit .csproj file
<OutputType>Library</OutputType>
cheers !
If you do have a Main method but still get this error, make sure that the file containing the Main method has "Build action" set to "Compile" and "Copy to ouput directory" set to "Do not copy".
For me, the error was actually produced by "Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater". This issue was resulting in the "Does not contain a static 'main' method suitable for an entry point" message in the Error List, but the Output window showed the "not available" error.
To correct this, I changed the language version from 'C# latest minor version (default)' to 'C# latest minor version (latest)' under Advanced Build Settings.
hey i got same error and the solution to this error is just write Capital M instead of small m.. eg:- static void Main()
I hope it helps..
Looks like a Windows Forms project that is trying to use a startup form but for some reason the project properties is set to startup being Main.
If you have enabled application framework you may not be able to see that Main is active (this is an invalid configuration).
Salaam,
I have both Visual Studio 2017 and Visual Studio 2019
Visual Studio 2019 does not show this error but 2017 does. Try Installing Visual Studio 2019.
Visual Studio 2017
Visual Studio 2019
Just right click on project and select properties and then set Output type on Class Library
After placing the above code in Program.cs, follow below steps
Right click on the project
Select Properties
Set Output Type to Windows Application
Startup object : namepace.Program
When you want to allow paramaters to be specified from the command, they must look like this:
[STAThread]
static void Main(params string[] paramaters)
{
you cannot specify more than one paramater, otherwise this will also cause the error reported above.
For some others coming here:
In my case I had copied a .csproj from a sample project which included <EnableDefaultCompileItems>false</EnableDefaultCompileItems> without including the Program.cs file. Fix was to either remove EnableDefaultCompileItems or include Program.cs in the compile explicitly
hellow your main class was deleted so add new class that name set as Main.cs and pest that code or if porblem in window so same problem on that
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace your_PKG_name.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}
A valid entry looks like:
public static class ConsoleProgram
{
[STAThread]
static void Main()
{
Console.WriteLine("Got here");
Console.ReadLine();
}
}
I had issues as I'm writing a web application, but for the dreadly loading time, I wanted to quickly convert the same project to a console application and perform quick method tests without loading the entire solution.
My entry point was placed in /App_Code/Main.cs, and I had to do the following:
Set Project -> Properties -> Application -> Output type = Console Application
Create the /App_Code/Main.cs
Add the code above in it (and reference the methods in my project)
Right click on the Main.cs file -> Properties -> Build Action = Compile
After this, I can set the output (as mentioned in Step 1) to Class Library to start the web site, or Console Application to enter the console mode.
Why I did this instead of 2 separate projects?
Simply because I had references to Entity Framework and other specific references that created problems running 2 separate projects.
For easier solutions, I would still recommend 2 separate projects as the console output is mainly test code and you probably don't want to risk that going out in production code.
If you are using a class library project then set Class Library as output type in properties under application section of project.
Another situation where this occur is when someone (unintentionally) changes Build Action for Program.cs. The value for Build Action should be C# compiler.
I accidentally changed Build Action to None, which removed program.cs from the project and therefore wasn't included when compile started.
Did you accidentally remove the entire Program.cs file?
If you have removed,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ListWievKullanımı
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
This might work for you.
Make sure you are not using void with async like
static async void Main(string[] args)
If yes, then change void to Task like
static async Task Main(string[] args)
If you do indeed have a public static main method it could be your build settings as explained in this question: Troubleshooting "program does not contain a static 'Main' method" when it clearly does...?
I too have faced this problem. Then I realized that I was choosing Console Application(Package) rather than Console Application.
I am using Visual Studio and also had this problem. It took me some time, but in my program it was caused because I accidentally deleted a Class named "Program" that is generated automatically.
For future readers who faced same issue with Windows Forms Application, one solution is to add these lines to your main/start up form class:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyMainForm());
}
Then go to project properties > Application > Startup Object dropdown, should see the namespace.MyMainForm, select it, clean and build the solution. And it should work.
Check to see if the project is set as the "Startup Project"
Right click on the project and choose "Set as Startup Project" from the menu.
If you are like me, then you might have started with a Class Library, and then switched this to a Console Application. If so, change this...
namespace ClassLibrary1
{
public class Class1
{
}
}
To this...
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}
If you use Visual Studio Code change Project Sdk="Microsoft.NET.Sdk.Web" to Project Sdk="Microsoft.NET.Sdk" on csproj file.

Categories

Resources