Can't get Process.Start to work in C# - c#

I've been trying to run a command from an app written in C#, and all I've read had to do with using System.Diagnostics.Process.Start to run it. However, whenever I try to use this function, the compiler throws an error telling me that 'The type or namespace name 'Process' does not exist in the namespace System.Diagnostics'.
When I try to write the command, intellisense doesn't show anything about "Process" inside the System.Diagnostics namespace.
This doesn't work:
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using System.Diagnostics;
namespace App2
{
sealed partial class App : Application
{
public App()
{
}
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
System.Diagnostics.Process.Start("CMD.exe", "help"); // This is giving me an error
}
}
}
EDIT: I just tried creating a new Windows Forms project, and I could find System.Diagnostics.Process.Start. Does anyone know how can I run a command from an Universal Windows App, since it's apparently not supported?

Related

How can I add a static 'Main' method suitable for an entry point?

I am getting an error on my code that says "Error CS5001
Program does not contain a static 'Main' method suitable for an entry point"
I am coding in C# using Microsoft Visual Studio and .NET. This is my code.
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
class projectsummer
{
[CommandMethod("OpenDrawing", CommandFlags.Session)]
public static void OpenDrawing()
{
string strFileName = "C:\\DRAFT.dwg";
DocumentCollection acDocMgr = Application.DocumentManager;
if (File.Exists(strFileName))
{
acDocMgr.Open(strFileName, false);
}
else
{
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + strFileName +
" does not exist.");
}
}
}
I am not sure how to go about this error. Thank you!
Looking at this post and your previous question, let's try and break down what's going on.
You created a new Console application in Visual Studio. You did not tick "Do not use top level statements". This gave you a Program.cs file that was essentially empty (there was no "Main" method visible).
You erased the Hello World code given to you, and went to make a static method - the code from your previous question.
Damien_The_Unbeliever commented that based on the error, you put your method inside a "top level statement" file, and to put your method inside a class.
You wrap your method (which is still inside Program.cs) in a class, and now suddenly you get a Can't Find Entry Point error.
User Ryan Pattillo posted a great explanation of the original issue - where your method was "by itself" in the Program.cs file. You should follow their advice, but you should also ensure that this class is in its own file.
You should end up with this:
Program.cs
// this is the entire contents of the file
using ConsoleApp1;
ProjectSummer.OpenDrawing();
ProjectSummer.cs
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
namespace ConsoleApp1
{
public class ProjectSummer
{
[CommandMethod("OpenDrawing", CommandFlags.Session)]
public static void OpenDrawing()
{
// ...
}
}
}
Change ConsoleApp1 to the name of your project.
The entry point of your application, which right now is the only file that has "top level statements", remains Program.cs, thus you fix the Can't Find Entry Point error.
Another adjustment you can make, which seeing you're new to C# might be useful, is to not use top level statements at all. Modify your Program.cs to this:
namespace ConsoleApp1
{
internal static class Program
{
// this is your program's entry point
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
ProjectSummer.OpenDrawing();
}
}
}
Change ConsoleApp1 to the name of your project.
You cannot use the AutoCAD .NET API out of process. To be able to use the AutoCAD .NET libraries, you have to build a "class library" project (DLL) and NETLOAD this DLL from a running AutoCAD process. See this topic about in-process vs out-of-process and you can start from this other one to see how to create an AutoCAD .NET project.

Cannot launch Repl() (Xamarin)

I'm writing UITests on Xamarin. I'm trying to launch the Repl window, but it doesn't launch.
My code:
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xamarin.UITest.Queries;
namespace MurakamiKiev.UITests
{
[TestFixture]
public class Tests
{
AndroidApp app;
[SetUp]
public void BeforeEachTest ()
{
app = ConfigureApp.Android.StartApp();
}
[Test]
public void ClickingButtonTwiceShouldChangeItsLabel ()
{
app.Repl();
}
}
}
What's wrong with my code? Thanks for the help.
I had the same issue, the problem was that app.Repl(); was already running in the background, from the previous session.(When you are debugging if you terminate debugger it wont close automatically)
The error I got was:
System.Exception: 'Error while performing Repl()
Inner exception
IOException: The process cannot access the file 'C:\Users\daniel\AppData\Local\Temp\uitest\repl\ICSharpCode.NRefactory.CSharp.dll because it is being used by another process.
So you just need to close app.Repl(); command prompt that is running in the background
Nothing is wrong with that code. I have seen this happen before but now can't recall the exact resolution. Might have been using NUnit version >= 3. Can you see if you still get the same issue using NUnit version 2.6.3?

C# "await" error when using WinRT from Desktop app

I trying to get my GPS position from a Desktop app, using Windows Runtime, in C#.
I followed this how-to to set up Visual Studio and this code sample to create the following trivial code as a C# console app :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime;
using System.Windows;
using Windows;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
namespace GeoLocCS
{
public sealed partial class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.Read();
}
}
public class Test
{
private Geolocator geolocator;
public Test()
{
Console.WriteLine("test");
geolocator = new Geolocator();
this.getPos();
}
async void getPos()
{
Geoposition pos = await geolocator.GetGeopositionAsync();
Console.WriteLine(pos.Coordinate.Latitude.ToString());
}
}
}
When trying to compile, I get the error :
Error 1 'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Devices.Geolocation.Geoposition>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?
I tried to reference every DLL that I could, like "System.runtime", "System.Runtime.WindowsRuntime", "Windows.Foundation"...
What I am missing ?
Thanks for the answer,
Serge
I finally figured out my problem :
I am on 8.1 so I changed this in the .csproj file (targetingPlatform) instead of targeting 8.0
After that modification, I could reference "Windows"
I manually referenced the two following DLLs :
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll
If you are On Win8.1 Please Use:
<TargetPlatformVersion>8.1</TargetPlatformVersion>
and not 8.0.
Adding to the existing answers:
I suddenly got this problem with a multiproject solution in WPF (still don't know why). I tried switching between TargetPlatformVersions 8.1 and 10.0, tried using the v4.5- and v4.5.1-System.Runtime.WindowsRuntime.dll, always alternating between BadImageFormatException and FileNotFoundException.
Turned out to be references in some of the app.config-files (not the .csproj-files!). Visual Studio 2017 must have added them at some point and as soon as I removed these entries, the above solutions finally worked.

Run Selenium C# Test from a Window Form

I am trying to launch my Selenium Launch when a button is press on my windows form. I am unsure on how to link the class library with the Windows Form. I am getting stuck because for the script/class to run correctly the project Output Type must be set to "Class Library" otherwise it gives out loads of errors.
Here is the class I am trying to launch :
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumTest
{
[TestClass]
public class SeleniumTest
{
[TestMethod]
public void TestMethod1()
{
// Set what browser to use
ChromeDriver driver = new ChromeDriver(#"C:\Users\Alex\Documents\Selenium");
// Set the base website
string baseURL = "http://kd.svr-webdev-01.df.local";
driver.Navigate().GoToUrl(baseURL + "/");
driver.Close();
}
}
}
All you need to do is put your unit test (TestMethod1) body into a button click event. Easiest way to make that event is just to drop a button onto the form using the designer and double click it.
Visual Studio will create a blank event for you, and you just need to copy/paste your current code into there.
Even, to make it simpler, create a console application and stick that body into the the "Main" method.

Could not load file or assembly 'ConsoleApplication1'

I just open a new C# project , but when i want to compile it i give the title error . i search for this error but didn't find any result .
what am i going to do ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello world!");
}
}
}
Try cleaning the project.
Make sure that the startup item isn't set
to a non-existant ConsoleApplication1.
Whatever the main form of
your project is, right click on it in Solution Explorer and select
"Set as Start Up...".
Make sure that the Program.cs (or Program.vb)
file is not attempting to load ConsoleApplication1.

Categories