前提・実現したいこと
C#のエラーをなくしたいです。
発生している問題・エラーメッセージ
①現在のコンテキストにfoldernameは存在しません
②現在のコンテキストにtextnameは存在しません
というエラーが出てプログラムが動かなくなっていますが、恥ずかしながらどの行のことを指しているのかわかりません。
該当のソースコード
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e) { //practice02よりOK、キャンセルの選択肢 //practice22よりテキストファイルの作成 //テキストの名前を自由に設定可能に //practice13より確認音の再生 System.Media.SystemSounds.Asterisk.Play(); DialogResult dr3; dr3 = MessageBox.Show("カレントディレクトリの" + foldername.Text + "フォルダを作成しますか?", "フォルダの作成", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (dr3 == DialogResult.OK) { if (System.IO.File.Exists(foldername.Text + ".txt")) { //同じ名前のフォルダにある場合エラーメッセージを表示 MessageBox.Show("既に同じ名前のフォルダが存在します。"); } else { { string CurDir = System.IO.Directory.GetCurrentDirectory(); System.Diagnostics.Process.Start(@CurDir); MessageBox.Show(CurDir + "に" + foldername.Text + "フォルダが作成されます。\n フォルダを開いて確認します。"); System.IO.Directory.CreateDirectory(foldername.Text); } } } else if (dr3 == DialogResult.Cancel) //キャンセルボタンを押した場合実行されない MessageBox.Show("キャンセルボタンが押されました"); } private void button3_Click(object sender, EventArgs e) { //practice22より System.Media.SystemSounds.Asterisk.Play(); DialogResult dr2; dr2 = MessageBox.Show("フォルダ内の" + textname.Text + ".txt" + "ファイルを開きますか?", "テキストを開く", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (dr2 == DialogResult.OK) { if (System.IO.File.Exists(textname.Text + "\.txt")) { System.Diagnostics.Process.Start(@System.IO.Directory.GetCurrentDirectory()); System.Diagnostics.Process p = System.Diagnostics.Process.Start("notepad.exe", System.IO.Directory.GetCurrentDirectory() + textname.Text + ".txt"); } else { //ファイルが見つからない場合エラーメッセージを表示 MessageBox.Show("ファイルが見つかりませんでした。"); } } else if (dr2 == DialogResult.Cancel) { MessageBox.Show("キャンセルボタンが押されました"); } } private void button4_Click(object sender, EventArgs e) { string TextFileName = System.IO.Directory.GetCurrentDirectory() + textname.Text + "\.txt"; System.IO.StreamWriter textFile; System.Diagnostics.Process.Start(@System.IO.Directory.GetCurrentDirectory()); if (System.IO.File.Exists(textname.Text + ".txt")) { MessageBox.Show("ファイルが見つかりました。ファイルにテキストボックスの文字列を書き込みます。"); textFile = new System.IO.StreamWriter(textname.Text + ".txt"); textFile.Write(textBox1.Text); textFile.WriteLine(); textFile.Close(); } else { MessageBox.Show("ファイルが見つかりませんでした。"); } } private void button1_Click(object sender, EventArgs e) { string TextFileName = System.IO.Directory.GetCurrentDirectory() + textname.Text + "\.txt"; System.IO.StreamWriter textFile; System.Media.SystemSounds.Asterisk.Play(); DialogResult dr; //practice02よりOK、キャンセルの選択肢 //practice31よりテキストファイルの作成 //テキストの名前を自由に設定可能に //practice13より確認音の再生 dr = MessageBox.Show(System.IO.Directory.GetCurrentDirectory() + "フォルダに" + textname.Text + ".txtを作成しますか?", "テキストの作成", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (dr == DialogResult.OK) { if (System.IO.File.Exists(textname.Text + ".txt")) { //同じ名前のテキストがフォルダにある場合エラーメッセージを表示 MessageBox.Show("既に同じ名前のテキストが存在します。"); } else { //同じ名前のテキストがフォルダにない場合テキストを作成 MessageBox.Show(System.IO.Directory.GetCurrentDirectory() + "フォルダに" + textname.Text + ".txtを作成します。\n フォルダを開いて確認します。"); System.Diagnostics.Process.Start(@System.IO.Directory.GetCurrentDirectory()); textFile = System.IO.File.CreateText(textname.Text + ".txt"); textFile.Close(); } } else if (dr == DialogResult.Cancel) { //キャンセルボタンを押した場合実行されない MessageBox.Show("キャンセルボタンが押されました"); } }
試したこと
補足情報(FW/ツールのバージョンなど)
visualstudio2015