C#播放声音的两个方法 + 流读写文件

来源:互联网 发布:apache bench 教程 编辑:程序博客网 时间:2024/06/07 01:50

利用API播放声音文件 

using System.Runtime.InteropServices;  在调用API时先引用

      

[DllImport("winmm")]
public static extern bool PlaySound(string szSound, IntPtr hMod, int i);   //声明API:PlaySound

 

调用:

PlaySound(@"声音文件路径",IntPtr.Zero,1);

 

利用windows media player播放声音

System.Media.SoundPlayer  media = new System.Media.SoundPlayer(@"声音文件路径");
 mdeia.Play();

 

流写文件

            System.IO.StreamWriter strwri = File.CreateText(@"d:/111.log");
            strwri.WriteLine("abc");
            strwri.WriteLine("def");
            strwri.Flush();
            strwri.Dispose();

流读文件

            StreamReader dd = new StreamReader(@"d:/11.txt");
           MessageBox.Show(dd.ReadLine());

原创粉丝点击