Created
July 21, 2012 21:59
-
-
Save thinkAmi/3157339 to your computer and use it in GitHub Desktop.
Skypeを起動してログインするプログラム (ただし、ユーザー名とパスワードが必要)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
namespace SkypeLogin | |
{ | |
public partial class Form1 : Form | |
{ | |
// 「Environment.SpecialFolder.ProgramFilesX86」 | |
// 「Environment.Is64BitOperatingSystem」を使っているため、.NET4 が必要です | |
private string _enabledMachine = <使用する端末名>; | |
private string _enabledUser = <使用する端末のユーザー名>; | |
private string _user = <Skypeユーザー名>; | |
private string _password = <Skypeパスワード>; | |
private string _programPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); | |
private string _skypePath = @"\Skype\Phone\Skype.exe"; | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
if (!(Environment.MachineName == _enabledMachine && | |
Environment.UserName == _enabledUser)) | |
{ | |
CloseForm("この端末・ユーザーでは動作しません。"); | |
return; | |
} | |
// 64bit OSの場合、デフォルトパスは(x86)になるため、上書きしておく | |
if (Environment.Is64BitOperatingSystem) | |
{ | |
_programPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); | |
} | |
// Skypeファイルの有無を確認 | |
if (!(System.IO.File.Exists(_programPath + _skypePath))) | |
{ | |
CloseForm("Skypeがインストールされていないようです。"); | |
return; | |
} | |
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName("Skype"); | |
bool hasRun = ps.Any(); | |
// WindowsフォームがなくてもSkypeは起動できるので、フォーム自体は閉じておく | |
if (hasRun) | |
{ | |
CloseForm("Skypeを一度終了させてから、再度Skypeへログインします。"); | |
} | |
else | |
{ | |
CloseForm("Skypeを起動しています。"); | |
} | |
// Skypeの停止 | |
if (hasRun) | |
{ | |
// Skypeを落とす:ログインできてなくても起動しているため | |
string shutdownArgs = "/shutdown"; | |
System.Diagnostics.Process.Start(_programPath + _skypePath, shutdownArgs); | |
for (int i = 0; i < 100; i++) | |
{ | |
if (i == 99) | |
{ | |
CloseForm("Skypeを終了させられませんでした。"); | |
return; | |
} | |
if (!(System.Diagnostics.Process.GetProcessesByName("Skype").Any())) | |
{ | |
break; | |
} | |
System.Threading.Thread.Sleep(1000); | |
} | |
} | |
// Skypeの起動 | |
string args = "/username:" + _user + " /password:" + _password; | |
System.Diagnostics.Process.Start(_programPath + _skypePath, args); | |
} | |
private void CloseForm(string message) | |
{ | |
MessageBox.Show(message); | |
this.Close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment