[原创]让C#驱动机器,为我们奏响电子音乐

创建一个控制台应用程序
引用 System.Runtime.InteropServices 和 System类库

    class MachineMusic
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>


        [DllImport(
"kernel32.dll")]
        
public static extern bool Beep(int frequency,int duration);

        [STAThread]
        
static void Main(string[] args)
        
{
            Music();
        }


        
private static void Music()
        
{
            Random random
=new Random();
            
for(int i =0;i<10;i++)
            
{
                Beep(random.Next(
10000),100);
            }


            Console.WriteLine(
"Play again? Press y to again");
            
string key=Console.ReadLine();
            
if (key=="y")
            
{
                Music();
            }

        }

    }
posted on 2006-09-30 20:29  雪无止境  阅读(337)  评论(1编辑  收藏  举报