【发布时间】:2014-12-17 15:01:13
【问题描述】:
大家好,首先对不起我的英语,但我希望你能理解我.. 我在 VS2008-Smart Device Project-WinCE 5.0 Project 上做一个项目。 我需要在程序主目录下的 DATA 文件夹中创建文本文件。
有我的代码,没有错误消息,但它没有创建文本文件。我的目录总是返回 null。 那个代码有什么问题?
if (Form2.dosya_adi != null)
{
string cfile = Form2.chosenfile;
path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\DATA\\" + cfile+ ".txt";
}
else
{
path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)+"\\DATA";
}
try
{
StreamReader Read_File= File.OpenText(path);//Dosyayı açmaya çalış olmaz ise catch bloğuna geç
ReadFile.Close();
}
catch
{
StreamWriter Write_File= File.CreateText(path+ i.ToString()+".txt");// yeni dosya oluştur.
Write_File.Close();
}`
这里是包含列表框和列表框的form2显示目录是否有文件..
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
path = path + "\\DATA";
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] rgFiles = di.GetFiles();
foreach (FileInfo fi in rgFiles)
{
listBox1.Items.Add(fi.Name);
}
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
chosen_file = listBox1.GetItemText(listBox1.SelectedItem);
Form1 form1 = new Form1();
form1.Show();
this.Hide();
}
else
{
MessageBox.Show("HATA:Hiçbir Değer Seçilmedi!"); // That means error:no value chosen!
}
}
【问题讨论】:
-
你调试过吗?变量中的值是什么?
-
什么时候?你应该在我可以在你的代码中看到的所有可能的地方得到一个
NullReferenceException。
标签: c# file text path windows-ce