0

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.

Community
  • 1
  • 1
  • Probably if you add the code you have written to the question above we could pinpoint the error. – Steve Sep 02 '12 at 23:07
  • Hi Steve. There is a bunch of auto generated code, but basically what I have is the following: – Miguel Angel Sep 03 '12 at 03:22
  • private async void click_on_button(object sender, RoutedEventArgs e) { Process.Start("cmd", "/C notepad.exe"); } I also have the using System.Diagnostics, using System, using System.ComponentModel. However, no matter how many usings I have, the "Type or namespace does not exist" error does not go away... it always appears underlining "Process". – Miguel Angel Sep 03 '12 at 03:29

1 Answers1

1

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

Philipp Munin
  • 5,610
  • 7
  • 37
  • 60
  • Hi Philipp. When I open the Reference Manager (right click on the References directory in the Solution Explorer the Add Reference), it says that "All of the Framework assemblies are already referenced". Same thing when I move to Solution, Windows or Browse tabs. Is it in this window where I should follow your indications or is it in another one? Thanks. – Miguel Angel Sep 03 '12 at 03:36
  • You can set Target framework in Properties of the project (in solution explorer right click on project, select properties) on "Application" tab). Then check your Reference folder of the project, select System.dll, in right click menu select Properties and in properties box check the Path, make sure it points to the same framework. – Philipp Munin Sep 03 '12 at 06:29
  • One more thing - if you already have System.Diagnostics in "using" section, then why you explicitly define "System.Diagnostics.Process" in your code? You can just use Process, unless you have another class Process visible in the scope. – Philipp Munin Sep 03 '12 at 06:32
  • Hi Philipp. Forgot to mention that when I start typing "System.Diagnostics.Pr" Intellisense shows the dropdown but the "Process" option does not appear. I added the "using System.Diagnostics.Process" because I was little frustrated and i thought that would help but did not and forgot to remove it. But even after removing it I still get the error. – Miguel Angel Sep 03 '12 at 18:31
  • I got to the Project Properties box/Application tab and found out that the "Target framework" select box is disabled with nothing selected. Could not find a way to enable it. Is there any? Only available fields there are Assembly Name, Default namespace and Output type. In Reference Paths tab (in properties box too), the Folder field was empty as well as the Reference paths section so I added the .NETFramework\v4.5\ (where the System.dll is). Saved changes, restarted VS and now the error says that "The name Process does not exist in the current context" and still got the Intellisense issue. – Miguel Angel Sep 03 '12 at 18:33
  • How come for project with C# code Target Framework can be disabled?? **What type of project you're working with?** I assume it is not Class Library, not Console Application, not WPF... – Philipp Munin Sep 03 '12 at 19:27
  • You should leave "Reference Paths" tab empty. I was talking about property Path in property editor box, when you select reference "System" in your solution explorer. – Philipp Munin Sep 03 '12 at 19:28
  • 1
    It is a Windows Store project with CS and XAML, you know Metro interface style and all that stuff (File -> New -> Project -> Templates -> Visual C# -> Windows Store). The problem I am facing is within the CS file. The XAML file which is the presentation layer is working fine so far. – Miguel Angel Sep 03 '12 at 20:16
  • I just found this post where this Dave Smits guy says it is not going to be possible to run cmd commands in a Metro app even if you're using CS. http://social.msdn.microsoft.com/Forums/da-DK/winappswithcsharp/thread/cbaa5856-64dd-4312-b244-b7dd4ac5db32 – Miguel Angel Sep 03 '12 at 20:38
  • Project type should have been mentioned in the question. It is separate .NET framework type. I'm going to update question and answer for that – Philipp Munin Sep 04 '12 at 17:38
  • They rejected my try to update your question, I would recommend you to do it then – Philipp Munin Sep 04 '12 at 20:02
  • Done. I added the Windows Store project part. – Miguel Angel Sep 04 '12 at 23:00