Tôi đang phát xung quanh với C# và tôi gặp sự cố. Khi tôi cố gắng tạo một tệp mới, chương trình sẽ ngắt và nói rằng tệp đang được sử dụng bởi một quy trình khác. Nó có thể là một cái gì đó ngu ngốc mà tôi bỏ qua, nhưng tôi không thể tìm ra nó!ngoại lệ C#. Tệp đang được sử dụng bởi một quy trình khác
Đây là mã của tôi:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace myNameSpace
{
public partial class MainWindow : Form
{
public static string folderPath = @"fullpath";
public MainWindow()
{
InitializeComponent();
StoredTb.Text = folderPath;
String[] files = Directory.GetFiles(folderPath);
foreach (string file in files)
myDropDown.Items.Add(Path.GetFileNameWithoutExtension(file));
}
private void myDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke(new MethodInvoker(myDropDown_invokedMethod));
}
private void myDropDown_invokedMethod()
{
string fullpath = MainWindow.folderPath + myDropDown.SelectedText + ".txt";
StreamReader sr = new StreamReader(fullpath);
NameTb.Text = myDropDown.SelectedText;
DescriptionTb.Text = sr.ReadToEnd();
sr.Close();
}
private void SaveBtn_Click(object sender, EventArgs e)
{
File.Create(NameTb.Text + ".txt");
TextWriter tw = new StreamWriter(NameTb.Text + ".txt"); /* this is where the problem occurs */
tw.WriteLine("The very first line!");
tw.Close();
}
}
}
Xin lỗi vì đoạn mã dài, nhưng vì tôi không chắc chắn nơi mà vấn đề bắt nguồn từ tôi đã phải bao gồm khá nhiều tất cả mọi thứ.
Cảm ơn! Nó đã làm việc. Tôi sẽ chấp nhận sau 6 phút (SO sẽ không cho phép tôi chấp nhận) –