この度初めてC#を仕事で使うことになり独学で勉強しているのですが、現在このコードを使用すると、ファイルは作成されるがテキストが保存されていない状態になってしまってます。
ボタンを押すと選択された日付と、入力されたテキストが保存され、ファイルを開く日付とテキストがメモ帳で文字として見れるようしようとしています・・・
以下が書いてみたコードになります。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; using System.Windows.Forms; namespace WindowsFormsApp33 { public partial class Form1 : Form { public Form1() { InitializeComponent(); DateTime dtToday = DateTime.Today; comboBox1.Items.Add(dtToday); DateTime dtTomorrow = dtToday.AddDays(1); comboBox1.Items.Add(dtTomorrow); DateTime dtDAT = dtToday.AddDays(2); comboBox1.Items.Add(dtDAT); DateTime dt3DL = dtToday.AddDays(3); comboBox1.Items.Add(dt3DL); } private void button2_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "ファイルを保存する"; sfd.InitialDirectory = @"C:\Users\xx\Documents"; sfd.Filter = "(*.testF)|*.testF|" + "All files(*.*)|*.*"; sfd.FilterIndex = 1; sfd.FileName = "新しいファイル.testF"; DialogResult result = sfd.ShowDialog(); string fname = sfd.FileName; FileStream fs = new FileStream(sfd.FileName, FileMode.Create); FilesIO filesIO = new FilesIO(); filesIO.File_Save(comboBox1.Text,textBox1.Text,fname); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { } private void saveFileDialog1_FileOk(object sender, CancelEventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } } class FilesIO:Form1 { public int File_Save(string a, string b,string c) { Form1 form1 = new Form1(); StreamWriter sr = null; try { sr = new StreamWriter(@"C:\Users\xx\Desktop.txt", false, Encoding.GetEncoding("shift_jis")); sr.WriteLine(a); sr.Write(b); sr.Close(); return 0; } catch { return 1; } } } }
パスの名前は変更していますがエラーは出ていないのを確認しています。
テキストを書き込めるようにするにはどうしたらよいかご助言お願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/26 05:55
2018/06/27 00:22