SaveFileDialog コンポーネントを使用すると、ユーザーはファイル システムを参照し、保存するファイルを選択できます。 ダイアログ ボックスは、ユーザーがダイアログ ボックスで選択したファイルのパスと名前を返します。 ただし、実際にファイルをディスクに書き込むには、コードを記述する必要があります。
SaveFileDialog コンポーネントを使用してファイルを保存するには
[ファイルの保存] ダイアログ ボックスを表示し、ユーザーが選択したファイルを保存するメソッドを呼び出します。
SaveFileDialog コンポーネントの OpenFile メソッドを使用してファイルを保存します。 このメソッドは、書き込み可能な Stream オブジェクトを提供します。
次の例では、DialogResult プロパティを使用してファイルの名前を取得し、OpenFile メソッドを使用してファイルを保存します。 OpenFile メソッドは、ファイルの書き込みストリームを提供します。
次の例では、イメージが割り当てられた Button コントロールがあります。 ボタンをクリックすると、SaveFileDialog コンポーネントが、.gif、.jpeg、および .bmpの種類のファイルを許可するフィルターでインスタンス化されます。 [ファイルの保存] ダイアログ ボックスでこの種類のファイルを選択すると、ボタンのイメージが保存されます。
重要
FileName プロパティを取得または設定するには、アセンブリには、System.Security.Permissions.FileIOPermission クラスによって付与された特権レベルが必要です。 部分的な信頼コンテキストで実行している場合、権限が不十分なため、プロセスが例外を投げる可能性があります。 詳細については、「コード アクセス セキュリティの基本」を参照してください。
この例では、フォームに Button コントロールがあり、その Image プロパティが .gif、.jpeg、または .bmpの種類のファイルに設定されていることを前提としています。
手記
FileDialog クラスの FilterIndex プロパティ (継承により、SaveFileDialog クラスの一部) は、1 から始まるインデックスを使用します。 これは、データを特定の形式で保存するコードを記述する場合に重要です (たとえば、ファイルをプレーン テキスト形式とバイナリ形式で保存する場合など)。 このプロパティは、次の例で紹介しています。
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click ' Displays a SaveFileDialog so the user can save the Image ' assigned to Button2. Dim saveFileDialog1 As New SaveFileDialog() saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif" saveFileDialog1.Title = "Save an Image File" saveFileDialog1.ShowDialog() ' If the file name is not an empty string open it for saving. If saveFileDialog1.FileName <> "" Then ' Saves the Image via a FileStream created by the OpenFile method. Dim fs As System.IO.FileStream = Ctype _ (saveFileDialog1.OpenFile(), System.IO.FileStream) ' Saves the Image in the appropriate ImageFormat based upon the ' file type selected in the dialog box. ' NOTE that the FilterIndex property is one-based. Select Case saveFileDialog1.FilterIndex Case 1 Me.button2.Image.Save(fs, _ System.Drawing.Imaging.ImageFormat.Jpeg) Case 2 Me.button2.Image.Save(fs, _ System.Drawing.Imaging.ImageFormat.Bmp) Case 3 Me.button2.Image.Save(fs, _ System.Drawing.Imaging.ImageFormat.Gif) End Select fs.Close() End If End Sub
private void button2_Click(object sender, System.EventArgs e) { // Displays a SaveFileDialog so the user can save the Image // assigned to Button2. SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; saveFileDialog1.Title = "Save an Image File"; saveFileDialog1.ShowDialog(); // If the file name is not an empty string open it for saving. if(saveFileDialog1.FileName != "") { // Saves the Image via a FileStream created by the OpenFile method. System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile(); // Saves the Image in the appropriate ImageFormat based upon the // File type selected in the dialog box. // NOTE that the FilterIndex property is one-based. switch(saveFileDialog1.FilterIndex) { case 1 : this.button2.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2 : this.button2.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp); break; case 3 : this.button2.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Gif); break; } fs.Close(); } }
private: System::Void button2_Click(System::Object ^ sender, System::EventArgs ^ e) { // Displays a SaveFileDialog so the user can save the Image // assigned to Button2. SaveFileDialog ^ saveFileDialog1 = new SaveFileDialog(); saveFileDialog1->Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; saveFileDialog1->Title = "Save an Image File"; saveFileDialog1->ShowDialog(); // If the file name is not an empty string, open it for saving. if(saveFileDialog1->FileName != "") { // Saves the Image through a FileStream created by // the OpenFile method. System::IO::FileStream ^ fs = safe_cast\<System::IO::FileStream*>( saveFileDialog1->OpenFile()); // Saves the Image in the appropriate ImageFormat based on // the file type selected in the dialog box. // Note that the FilterIndex property is one based. switch(saveFileDialog1->FilterIndex) { case 1 : this->button2->Image->Save(fs, System::Drawing::Imaging::ImageFormat::Jpeg); break; case 2 : this->button2->Image->Save(fs, System::Drawing::Imaging::ImageFormat::Bmp); break; case 3 : this->button2->Image->Save(fs, System::Drawing::Imaging::ImageFormat::Gif); break; } fs->Close(); } }
(Visual C# と Visual C++)フォームのコンストラクターに次のコードを配置して、イベント ハンドラーを登録します。
this.button2.Click += new System.EventHandler(this.button2_Click);
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
ファイル ストリームの書き込みの詳細については、「BeginWrite と Write」を参照してください。
手記
RichTextBox コントロールなどの特定のコントロールには、ファイルを保存する機能があります。
関連項目
.NET Desktop feedback