得到非常奇怪的启动错误

时间:2013-03-12 19:01:46

标签: c# visual-studio msbuild

我有一个非常简单的程序:

using System;
using System.Windows.Forms;
using System.Text;
using System.IO;

namespace NS
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                Console.SetIn(new StreamReader(Console.OpenStandardInput(2000)));
                //get some content using Console.ReadLine() and prompting using Console.WriteLine();
                string HTML =
                string.Format(@"HTML redacted", someFormatString, w/e);
                Clipboard.SetText(HTML);
                Console.ReadKey(true);
                string JS = string.Format(@"JS redacted", someFormatString, w/e);
                Clipboard.SetText(JS);
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

旁注:是的我知道在Main周围包装一个try / catch不是首选,但我现在需要它用于调试目的。

我正在引用所有正确的程序集,这里是csproj中的程序集引用:

<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />

但是,当我尝试从资源管理器加载它时,它会出现0xc000142。不过,我不知道这意味着什么。

如果我从CMD加载它,那就完美了。

有什么想法吗?我已经尝试添加引用,更改为System.Windows剪贴板类,创建一个新项目并复制代码,没有任何效果。

编辑:因为这似乎并不清楚:这是一个控制台应用程序,需要使用形式的剪贴板类。它不是一个成为FORMS / CLI APP的尝试。

2 个答案:

答案 0 :(得分:2)

评论后编辑

听起来您的机器存在问题。这是我的测试程序的发布版本,它在我的机器上运行。如果它没有运行在你的上面,那你就有一个腐败的.net安装或类似的东西。我建议也试试另一台电脑。

这是.net 4.5 btw。

如果其他人能证实它有效,而且我并不疯狂,那就太棒了:)。

http://23.23.250.9/ClipboardInConsole.zip

请注意,只有剪贴板才能工作,您只需要参考

System
System.Windows.Forms

不确定你需要所有其他的,但对于剪贴板,这2个就足够了,项目仍然可以构建和运行。祝你好运!


我原来的回答:

需要单线程单元模式才能正常工作。添加以下属性,这应该工作。 请注意,此答案是在OP编辑之前发布的,在问题中添加了[STAThread]。

    [STAThread]
    static void Main(string[] args)

注意:如果在catch块中添加Console.ReadKey();,则会看到该错误。您还只需要对System.Windows.Forms的引用。

注2:如果它仍然无效,则会出现另一个您没有看到的错误。试试我在Catch {}块中添加Console.ReadKey()的建议并查看异常。

我的(你的)编译和工作的完整代码:

using System;
using System.Windows.Forms;
using System.Text;
using System.IO;

namespace NS
{

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                Console.SetIn(new StreamReader(Console.OpenStandardInput(2000)));
                //get some content using Console.ReadLine() and prompting using Console.WriteLine();
                string HTML = "<html>";
                //string.Format(@"HTML redacted", someFormatString, w / e);
                Clipboard.SetText(HTML);
                Console.ReadKey(true);
                string JS = "JS { }";//string.Format(@"JS redacted", someFormatString, w / e);
                Clipboard.SetText(JS);
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.ReadKey();
            }
        }
    }
}

答案 1 :(得分:0)

我有几个选择:

1)尝试在管理员模式下运行Visual Studio。右键单击VS图标 - &gt;以管理员身份运行。如果这样可以,请告诉我。

2)检查项目设置并确保选择“任何CPU”构建它。