简体   繁体   English

如何解决这些C#错误?

[英]How do I fix these C# errors?

I found some code to help me in a project and when I first ran the code I received an error message indicating: "Visual Studio cannot start debugging because the debug target c:\\path\\'dirInfo.exe' is missing. Please build the project and retry, or set the OutPath and AssemblyName properties appropriately to point at the correct location for the target assembly." 我在项目中找到了一些可以帮助我的代码,当我第一次运行该代码时,我收到一条错误消息,指出:“ Visual Studio无法启动调试,因为缺少调试目标c:\\ path \\'dirInfo.exe'。请构建项目,然后重试,或者适当地设置OutPath和AssemblyName属性以指向目标程序集的正确位置。”

Then I select OK and receive an error message indicating that partial is missing. 然后,我选择确定,并收到一条错误消息,指出缺少该部件。 I add partial to the code and receive 3 more error messages. 我将部分代码添加到代码中,并收到另外3条错误消息。

  1. The type 'RecursiveSearchCS.Form1' already contains a definition for 'components' “ RecursiveSearchCS.Form1”类型已经包含“组件”的定义

    • does this mean I should delete this from the Form1.cs file? 这是否意味着我应该从Form1.cs文件中删除它?
  2. Type 'RecursiveSearchCS.Form1' already defines a member called 'Dispose' with the same parameter types. 类型“ RecursiveSearchCS.Form1”已经定义了具有相同参数类型的名为“ Dispose”的成员。

  3. Type 'RecursiveSearchCS.Form1' already defines a member called 'InitializeComponent' with the same parameter types. 类型“ RecursiveSearchCS.Form1”已经定义了具有相同参数类型的名为“ InitializeComponent”的成员。

(I notice, when I comment out the InitializeComponent line and/or Dispose line, many more error messages populate in ERRORS) (我注意到,当我注释掉InitializeComponent行和/或Dispose行时,更多错误消息填充在ERRORS中)

By they way you can find the original code @ MicrosoftSite . 通过它们,您可以找到原始代码@ MicrosoftSite

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank You 谢谢

Just gut instinct, if you were following along and copy pasting remember one key thing: 只是直觉,如果您遵循并复制粘贴,请记住一件事:

The designer creates two files when you create a form: A "code" file, and a "designer" file. 当您创建表单时,设计器将创建两个文件:“代码”文件和“设计器”文件。 However, when microsoft (and others) release "templates", they like to merge these two files. 但是,当Microsoft(和其他公司)发布“模板”时,他们喜欢合并这两个文件。

Just create a new .cs file and paste the code and all should be good. 只需创建一个新的.cs文件并粘贴代码即可,一切都应该很好。 It's the code basically saying "in the designer, we already have this stuff". 基本上是这样的代码:“在设计器中,我们已经有了这些东西”。 (a good way to note this is the "partial" keyword located before your Form1 declaration) (注意这的一个好方法是在Form1声明之前使用“ partial”关键字)


More info: 更多信息:

The Code file will house all your own implementations. 代码文件将包含您自己的所有实现。 That is click events, methods you personally override, events you bind to, etc. This is the default file when you select "View Code" from either your solution explorer or the dialog itself. 即单击事件,您个人重写的方法,绑定到的事件等。这是从解决方案资源管理器或对话框本身选择“查看代码”时的默认文件。 Within this file is a construct that calls a "hidden" method, ( InitializeComponent ) that if you right click and "Go to Definition" will bring you to the next file: 在此文件中,有一个调用“ hidden”方法的构造( InitializeComponent ),如果右键单击并单击“转到定义”,会将您带到下一个文件:

The Designer file is the IDE's generated file. 设计器文件是IDE的生成文件。 This takes everything you do in the designer and stores it for you. 这样就可以将您在设计器中所做的所有工作都存储起来。 That includes new controls, location and properties of the controls, and the IDisposable implementation. 其中包括新控件,控件的位置和属性以及IDisposable实现。 The idea is to keep the "meat an potatoes" out of the way while you worry only about implementation. 这样做的目的是在您只担心实现问题的同时,避免“麻烦”。

Yes it sounds like you've copied the entire code which includes many things already contained within your Form in a partial class. 是的,听起来您已经复制了整个代码,其中包括部分类中Form中已经包含的许多内容。 Either remove these or remove the partial class and partial class declaration from your Form to get rid of these errors 从您的窗体中删除这些或删除部分类和部分类声明以摆脱这些错误

I went to the Microsoft site to see what you did. 我去了微软的网站,看看你做了什么。 The site shows code for an entire "one file" solution. 该站点显示了整个“一个文件”解决方案的代码。 We've all agreed that Visual Studio creates multi-file solutions, so you're duplicating code. 我们都同意Visual Studio创建多文件解决方案,因此您在复制代码。

I don't know if the current answers/comments have helped you get this sample code working, so I thought I'd add my share. 我不知道当前的答案/评论是否可以帮助您使此示例代码正常工作,所以我想我应该添加自己的份额。 I was able to get this sample working by doing the following: 通过执行以下操作,我能够使此示例工作:

First, where the sample code at the Microsoft site shows declarations for button, textbox, labels, and combobox, rather than attempting to copy that portion, I simply used the toolbox and dragged a button, the labels, the textbox, and the combobox from the toolbox to my form. 首先,Microsoft网站上的示例代码显示了按钮,文本框,标签和组合框的声明,而不是尝试复制该部分,我只是使用工具箱并从中拖动按钮,标签,文本框和组合框。我的表单的工具箱。

You'll probably want to arrange these to your liking. 您可能需要根据自己的喜好安排这些。

This process created my form correctly with the appropriate objects on it. 此过程正确创建了带有适当对象的表单。 All I had to do was use the properties window for each object and rename them according to what they were named in the sample. 我要做的就是对每个对象使用属性窗口,并根据示例中的名称对其进行重命名。 For example, my new button was originally button1 , but I renamed it to btnSearch just as it is named in the Microsoft sample. 例如,我的新按钮最初是button1 ,但我将其重命名为btnSearch,就像在Microsoft示例中一样。

I noticed that the Microsoft sample has an established event handler setup for the Form1_load() event. 我注意到Microsoft示例为Form1_load()事件建立了事件处理程序设置。 I created this same event in my form by clicking the form in the designer, clicking properties, clicking on the Events button in that properties, and double-clicking the "Load" event. 通过在设计器中单击表单,单击属性,单击该属性中的“事件”按钮,然后双击“加载”事件,在表单中创建了同一事件。 This automatically generated the appropriate code. 这会自动生成适当的代码。

In a similar way, I had to create the btnSearch_Click() event. 以类似的方式,我必须创建btnSearch_Click()事件。 I did this by simply double-clicking the button in the designer. 我只是通过双击设计器中的按钮来完成此操作。

After that, all I had to do was manually copy and paste from the specific sections of the sample to my code -- fill in the Form1_Load() event with what was in the sample. 之后,我要做的就是手动将样本的特定部分复制并粘贴到我的代码中,并使用样本中的内容填充Form1_Load()事件。 Copy the DirSearch() method over. 复制DirSearch()方法。 Fill in the btnSearch_Click() event. 填写btnSearch_Click()事件。 That was it. 就是这样。

I hope this helps solve the overall issue and gives you more insight into how you can avoid these problems in the future. 我希望这有助于解决整体问题,并让您对将来如何避免这些问题有更多的了解。

您在类中具有重复的功能,您已经使用该功能自动生成了一个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM