首页 > 解决方案 > How to call shell script in C# vs VB.Net

问题描述

I am new to C#. I used a converter to convert a VB.Net project to C#. I am looking at the errors I am getting in C# now. VB Code:


        Dim oShell As Object
        Dim oLink As Object
        'you don’t need to import anything in the project reference to create the Shell Object
        Try
                oShell = CreateObject("WScript.Shell")
                oLink = oShell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & "Cost Allocation Files" & ".lnk")

                oLink.TargetPath = "C:\CostAllocationFiles"
                oLink.WindowStyle = 1
                oLink.Description = "Cost Allocation Files Directory"
                oLink.Save()
        Catch ex As Exception
                MessageBox.Show("Desktop Cost Allocation Files Link CREATION Failed")
        End Try

translates to C# code:


        object oShell;
        object oLink;
        try
                {
                oShell = Interaction.CreateObject("WScript.Shell");
                oLink = oShell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + "Cost Allocation Files" + ".lnk");
                oLink.TargetPath = @"C:\CostAllocationFiles";
                oLink.WindowStyle = 1;
                oLink.Description = "Cost Allocation Files Directory";
                oLink.Save();
                }
                catch (Exception ex)
                {
                MessageBox.Show("Desktop Cost Allocation Files Link CREATION Failed");
                }

But it produces errors: CS0103 C# The name 'Interaction' does not exist in the current context 'object' does not contain a definition for 'CreateShortcut' and no accessible extension method 'CreateShortcut' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

So how do you handle something like this (call to shell script, etc...) in C#?

标签: c#vb.netshell

解决方案


推荐阅读