1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C#入門 第18章】音声再生・印刷・外部アプリ起動|ハードウェアを動かす基本技!

Last updated at Posted at 2025-08-16

【C#入門 第18章】音声再生・印刷・外部アプリ起動|ハードウェアを動かす基本技!

こんにちは、 CSharpTimes の一之瀬シィよ💠
今回はC#から“ハードウェア”や“外部アプリ”を操作するテクニックを伝授してあげるわ💢
パソコンの音を鳴らしたり、印刷したり、アプリを立ち上げたり……これぞ実践技!


🔊 音声を再生する( System.Media.SoundPlayer

using System.Media;

SoundPlayer player = new SoundPlayer("sound.wav");
player.Play();  // 非同期再生
// player.PlaySync();  // 同期再生(終わるまで待つ)
  • .wav ファイルのみ対応
  • 音を鳴らすだけならこれで十分!

🖨️ 印刷ダイアログを開いて印刷する

using System.Drawing.Printing;

PrintDocument doc = new PrintDocument();
doc.PrintPage += (sender, e) =>
{
    e.Graphics.DrawString("こんにちは、印刷の世界へ!", new Font("Meiryo", 14), Brushes.Black, 100, 100);
};

PrintDialog dialog = new PrintDialog();
dialog.Document = doc;

if (dialog.ShowDialog() == DialogResult.OK)
{
    doc.Print();
}
  • PrintDocument PrintDialog を使って印刷
  • PrintPage イベント内で印刷内容を定義

🚀 外部アプリやファイルを起動する( System.Diagnostics.Process

✅ アプリを起動

using System.Diagnostics;

Process.Start("notepad.exe");  // メモ帳起動!

✅ URLを開く

Process.Start("https://qiita.com/");

✅ 特定のファイルを開く(関連付けされたアプリで)

Process.Start("sample.pdf");

.NET Core 以降 では UseShellExecute = true が必要な場合があるわ👇

ProcessStartInfo psi = new ProcessStartInfo("sample.pdf")
{
    UseShellExecute = true
};
Process.Start(psi);

📌 まとめ

  • 音声再生 → SoundPlayer - 印刷 →PrintDocument+PrintDialog- 外部操作 → Process.Start()

C#なら、アプリだけじゃなくPCそのものをコントロールできるの。
“ただのツール”で終わらせない力、ちゃんと使いこなしなさいよ💢


次回は、 「第19章:社内アプリ開発の工夫」 よ。
業務効率化に本気で役立つアプリを作る、その秘訣を全部伝えるわね!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?