2015-04-03 66 views
-3

我想这类型的自动编写一个程序,从1000至9999,当每个数字输入,程序应该按enter键模拟在C#中一个按键

例如,如果我运行该程序,然后打开记事本,输出应该是:

1000 
 
1001 
 
1002 
 
1003 
 

 
. 
 
. 
 
. 
 

 
9998 
 
9999

编辑: 我只是想给一个键,按程序的虚拟代码,并释放键。 就是这样。

坦对你的答案:)

+1

你有什么试过?如果你不显示任何努力,人们不会帮助你。 – 2015-04-03 13:15:15

+0

你不需要模拟按键,你只需要在每个数字后加一个换行。即'.... Write(num + Environment.NewLine)' – Jonesopolis 2015-04-03 13:16:18

+0

我是一个新用户。请帮我:( – Cyanogen 2015-04-03 13:17:59

回答

0
 int i; 
     int number = 999; 
     for (i = 0; i < 9000; i++) 
     { 
      number += 1; 
      Console.WriteLine(number); 
     } 
     Console.ReadKey(); 

,你可以把它写与有System.IO.File类文件,或者你可以将其列表添加到列表,并写入文件

好吧,如果我们加:

using System.IO; 

这将使可使用此.NET类在我们 类接下来让做一个列表,所有的数字写入。

List<string> print = new List<string>(); 

现在,填补了我国在循环内部列表中,我们可以添加

print.add(number.ToString()); 

好我们的名单已满,我们所要做的就是将它保存到一个文件

System.IO.File.WriteAllLines(@"C:\users\public\numbers.txt", print); 

这将创建我们的文件,我们应该做的!打开它在C:\用户\公用文件夹中

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 

namespace ConsoleApplication14 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     int i; 
     int number = 999; 
     List<string> print = new List<string>(); 
     for (i = 0; i < 9000; i++) 
     {    
      number += 1; 
      print.Add(number.ToString()); 
     } 
     System.IO.File.WriteAllLines(@"C:\users\public\numbers.txt", print); 
     Console.WriteLine("Press a button and open your text document"); 
     Console.ReadKey(); 
    } 
} 
} 
+0

不,我不想在DOS模式下写入数字 – Cyanogen 2015-04-03 13:26:45

+0

请阅读System.IO.File类的一些内容,我将稍微编辑一些这方面的知识给你。 – Scriven 2015-04-03 13:29:16

+0

这个答案可以怎样接受?没有按键模拟它... – Orace 2015-04-03 15:02:53