2014年4月28日月曜日

C# Left, Right and Mid functions

C# Left, Right and Mid functions

I started as a VB programmer and I must saythat i miss using Left, Right and Mid methods since it is not includedin C#. But then again, there is always a suitable replacement. TheSubstring method.
The Substring method retrieves a substringfrom a specified string. In this demo i have decided to show how to usethe substring method to create the Left, Right and Mid functions.
namespace LeftRightMid
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class LeftRightMid
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

//assign a value to our string
string myString = "This is a string";
//get 4 characters starting from the left
Console.WriteLine(Left(myString,4));
//get 6 characters starting from the right
Console.WriteLine(Right(myString,6));
//get 4 characters starting at index 5 of the string
Console.WriteLine(Mid(myString,5,4));
//get the characters from index 5 up to the end of the string
Console.WriteLine(Mid(myString,5));
//display the result to the screen
Console.ReadLine();
}
public static string Left(string param, int length)
{
//we start at 0 since we want to get the characters starting from the
//left and with the specified lenght and assign it to a variable
string result = param.Substring(0, length);
//return the result of the operation
return result;
}
public static string Right(string param, int length)
{
//start at the index based on the lenght of the sting minus
//the specified lenght and assign it a variable
string result = param.Substring(param.Length – length, length);
//return the result of the operation
return result;
}
public static string Mid(string param,int startIndex, int length)
{
//start at the specified index in the string ang get N number of
//characters depending on the lenght and assign it to a variable
string result = param.Substring(startIndex, length);
//return the result of the operation
return result;
}
public static string Mid(string param,int startIndex)
{
//start at the specified index and return all characters after it
//and assign it to a variable
string result = param.Substring(startIndex);
//return the result of the operation
return result;
}
}
}

元ネタ
http://www.csharphelp.com/2007/07/c-left-right-and-mid-functions/

2014年4月15日火曜日

sql express backup

SQLをmasterで実行

CREATE PROCEDURE MYDATABASE_BACKUP
AS
BEGIN
DECLARE @BACKUPTOPPATH NVARCHAR(1024)
DECLARE @WEEKOFDAY NVARCHAR(10)

SELECT
@BACKUPTOPPATH=
'C:\askc\SQLBackup'
-- バックアップ格納先フォルダを指定(自分の環境に合うように修正ください)
,@WEEKOFDAY = CASE DATEPART(WEEKDAY, GETDATE())
WHEN 1 THEN 'SUN'
WHEN 2 THEN 'MON'
WHEN 3 THEN 'TUE'
WHEN 4 THEN 'WED'
WHEN 5 THEN 'THU'
WHEN 6 THEN 'FRI'
WHEN 7 THEN 'SAT' END
DECLARE @DBNAME NVARCHAR(1024)
DECLARE @WORK_BACKUPPATH NVARCHAR(1024)
DECLARE @MEDIANAME NVARCHAR(1024)
DECLARE @SQLCMD NVARCHAR(MAX)

DECLARE cursor1 cursor FOR
select name from sys.databases where name <> 'tempdb';
-- sys.databaseを呼び出して、データベース名を列挙します。
-- tempdbは、バックアップ対象外です。msdb, modelなどのシステムDBもバックアップします
OPEN cursor1
FETCH NEXT FROM cursor1 INTO @DBNAME

WHILE(@@FETCH_STATUS = 0 )
BEGIN
SELECT @WORK_BACKUPPATH=@BACKUPTOPPATH +'\'+ @WEEKOFDAY +'\' + @DBNAME +'.bak'
, @MEDIANAME = 'PATH_OF_FIXED_'+@WEEKOFDAY+'_'+@DBNAME
SELECT @SQLCMD =
'USE ' + @DBNAME + ';'
+ 'BACKUP DATABASE ' + @DBNAME +' '
+ ' TO DISK = '''+@WORK_BACKUPPATH+''''
+ ' WITH FORMAT, MEDIANAME = '''+@MEDIANAME+''','
+ ' NAME = ''Full Backup '+ @WEEKOFDAY +' ' + @DBNAME +''';'
EXECUTE SP_EXECUTESQL @SQLCMD
FETCH NEXT FROM cursor1 INTO @DBNAME
END
CLOSE cursor1
DEALLOCATE cursor1

END

タスクスケジュールで、以下のコマンドを追加
プログラム実行: sqlcmd
パラメータ: -S .\SQLExpress -E -Q "EXEC MYDATABASE_BACKUP"

2014年4月14日月曜日

c# BitmapDecoder

using System.Windows.Media.Imaging;

BitmapDecoder

上記のコーディングを行う場合

"C:\Program Files\Reference Assemblies\Microsofr\Framework\v3.0".

のPresentationCode.dllを追加

Windows Server 2012 リモート デスクトップ環境の構成について ドメイン環境で構築

Windows Server 2012 リモート デスクトップ環境の構成について

2013/09/13 追記。

こんにちは。Windows プラットフォーム サポートの横山です。
本日は、Windows Server 2012 リモート デスクトップ環境の構成に関するコマンドをご紹介いたします。
Windows Server 2012 リモート デスクトップ環境では、RDMS (Remote Desktop Management Service) を利用してリモート デスクトップ サービス関連の設定の変更を行います。

しかしながら、RDMS は "リモート デスクトップ サービスのインストール" を使用してリモート デスクトップ サービスのインストールを行わない限り、インストールされません。
また、"リモート デスクトップ サービスのインストール" は、非ドメイン環境では実行できず、ドメイン環境でのみ実行可能であり、ドメイン環境であってもドメイン コントローラー単一では、"リモート デスクトップ サービスのインストール" が実行できません。
その為、ドメイン コントローラーや WORKGROUP 環境の Windows Server 2012 にリモート デスクトップ セッション ホストの各種設定を行うには、WMI コマンドの利用やレジストリの編集が必要です。
※ ドメイン コントローラーへの "リモート デスクトップ サービスのインストール" については、KB2871777 にて公開されている更新プログラムをインストールして再起動することにより、実行可能となります。

今回は、PowerShell を利用したリモート デスクトップ セッション ホスト / リモート デスクトップ ライセンスのインストール、ライセンス サーバーの設定、また、RemoteApp の公開、RemoteApp 接続を行うための .rdp ファイルの作成方法についてお伝えいたします。
- リモート デスクトップ セッション ホストのインストール
1. [ツール] より、[Windows PowerShell] を起動します。
 
2. [リモート デスクトップ セッション ホスト] 役割サービスをインストールします。
   > Add-WindowsFeature -Name RDS-RD-Server
3. [デスクトップ エクスペリエンス] をインストールします。
   > Add-WindowsFeature -Name Desktop-Experience
4. [ライセンス診断] ツールをインストールします。
   > Add-WindowsFeature -Name RSAT-RDS-Licensing-Diagnosis-UI
5. システムの再起動を行います。

- リモート デスクトップ ライセンスのインストール、ライセンス サーバーの設定
1. [ツール] より、[Windows PowerShell] を起動します。
2. [リモート デスクトップ ライセンス] の役割サービスをインストールします。
   > Add-WindowsFeature -Name RDS-Licensing
3. [RD ライセンス マネージャー] をインストールします。
   > Add-WindowsFeature -Name RDS-Licensing-UI
4. [リモート デスクトップ ライセンス モード] を変更します。
   > (gwmi -Class Win32_TerminalServiceSetting -Namespace root\cimv2\TerminalServices).ChangeMode(4)
   ※ 4 は [接続ユーザー数] モードです。[接続デバイス数] モードは 2 です。
  
5. [リモート デスクトップ ライセンス サーバー] を指定します。
   > New-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\TermService\Parameters\LicenseServers" -Name SpecifiedLicenseServers -Value "<IP アドレス>" -PropertyType MultiString
        また、以下のコマンドを実行しても同様の処理が行えます。
   > (gwmi -Class Win32_TerminalServiceSetting -Namespace root\cimv2\TerminalServices).SetSpecifiedLicenseServerList("<IP アドレス>")
6. 以下のコマンドで正常にライセンス サーバーが指定されていることを確認します。
   > Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\TermService\Parameters\LicenseServers" -Name SpecifiedLicenseServers
   ※ 以下のコマンドを実行しても同様の処理が行えます。
   > (gwmi -Class Win32_TerminalServiceSetting -Namespace root\cimv2\TerminalServices).GetSpecifiedLicenseServerList()

- RemoteApp の公開方法、RemoteApp 接続用の .rdp ファイルの作成方法
1. 下記コマンドを実行し、RemoteApp として公開したいアプリケーションを登録します。
   ※ 以下は mspaint.exe を登録する手順です。
   > $regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server\TSAppAllowList\Applications"
   > New-Item $regPath -Name mspaint
   > New-ItemProperty $regPath\mspaint -Name CommandLineSetting -Value 0 -PropertyType Dword
   > New-ItemProperty $regPath\mspaint -Name Name -Value Paint -PropertyType String
   > New-ItemProperty $regPath\mspaint -Name Path -Value C:\Windows\System32\mspaint.exe -PropertyType String
   > New-ItemProperty $regPath\mspaint -Name RequiredCommandLine -PropertyType String
   > New-ItemProperty $regPath\mspaint -Name SecurityDescriptor -PropertyType String
   > New-ItemProperty $regPath\mspaint -Name ShowInTSWA -Value 1 -PropertyType Dword
2. クライアントには下記内容を含む .rdp ファイルを配布します。
   =========================================
   redirectclipboard:i:1
   redirectposdevices:i:0
   redirectprinters:i:1
   redirectcomports:i:1
   redirectsmartcards:i:1
   devicestoredirect:s:*
   drivestoredirect:s:*
   redirectdrives:i:1
   session bpp:i:32
   prompt for credentials on client:i:1
   span monitors:i:1
   use multimon:i:1
   remoteapplicationmode:i:1
   server port:i:3389
   allow font smoothing:i:1
   promptcredentialonce:i:1
   authentication level:i:2
   gatewayusagemethod:i:2
   gatewayprofileusagemethod:i:0
   gatewaycredentialssource:i:0
   full address:s:<サーバー名もしくは IP アドレス>
   alternate shell:s:||mspaint <------------ レジストリ キー名
   remoteapplicationprogram:s:||mspaint <--- レジストリ キー名
   gatewayhostname:s:
   remoteapplicationname:s:Paint <---------- Name に記載のデータ
   remoteapplicationcmdline:s:
   =========================================
   ※ <------ とその後の文字列は削除してください。
 
- 参考情報
Win32_TerminalServiceSetting class (Windows)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383640(v=vs.85).aspx
Windows Server 2012 ドメイン コントローラーで "リモート デスクトップ サービスのインストール" ができない。
http://support.microsoft.com/kb/2795837
A servicing stack update is available for Windows RT, Windows 8, and Windows Server 2012: September 2013
http://support.microsoft.com/kb/2871777