C#中FolderBrowserDialog fbd = new FolderBrowserDialog();下如何解决新建文件夹重命名并保存文件的问题!急!
private BlockLength _BlockLength;
private Byte _BlockCount;
private Boolean _AutoRun;
private Thread _thread;
private String _FilePath;
private void btn_ReadAndSave_Click(object sender, EventArgs e)
{
if (btn_ReadAndSave.Text.Equals("Inventory Read And Save"))
{
if (cmd_Length_6.SelectedIndex < 0)
{
AddList("错误:请选择正确的数据块长度!");
cmd_Length_6.Focus();
return;
}
else
{
_BlockLength = (BlockLength)Byte.Parse(cmd_Length_6.Text.Trim());
}
try
{ _BlockCount = Byte.Parse(txt_Number_6.Text.Trim()); }
catch
{
AddList("错误:请填写您要读取的数据块数量!");
txt_Number_6.Focus();
return;
}
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
_FilePath = fbd.SelectedPath;
}
else
{
return;
}
_AutoRun = true;
_thread = new Thread(new ThreadStart(InventoryReadSave));
_thread.IsBackground = true;
_thread.Start();
btn_ReadAndSave.Text = "Stop Inventory And Read";
}
else
{
_AutoRun = false;
Thread.Sleep(500);
_thread.Abort();
_thread.Join(500);
_thread = null;
btn_ReadAndSave.Text = "Inventory Read And Save";
}
}
///以上是淡季按钮后显示系统文件菜单浏览,能选择文件夹并保存
private void InventoryReadSave()
{
Int32 TagCount = 0;
String[] TagNumber = new String[0];
Byte value;
String RvsTag;
while (_AutoRun)
{
value = reader.Inventory(ModulateMethod.ASK, InventoryModel.Multiple, ref TagCount, ref TagNumber);
if (value != 0x00)
{
continue;
}
AddList(String.Format("Inventory执行成功,找到了{0}个卡片:", TagCount));
foreach (String Tag in TagNumber)
{
RvsTag = "";//文件名
for (int i = 0; i < 8; i++)
{ RvsTag += Tag.Substring((7 - i) * 2, 2); }
String FileName = _FilePath +"\\"+ RvsTag + ".txt";
using (FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
{
//Byte BlockAddress = 0;
Byte[] BlockData = new Byte[1];
AddList(String.Format("开始读取标签:{0}", RvsTag));
sw.WriteLine(String.Format("*{0}*", RvsTag));
for (Byte i = 0; i < _BlockCount; i++)
{
value = reader.ReadSingleBlock(Tag, _BlockLength, i, ref BlockData);
if (value == 0x00)
{
String data = "";
for (Int32 ii = 0; ii < BlockData.Length; ii++)
{
data += BlockData[ii].ToString("X2");
}
AddList(String.Format("第{0}块数据读取成功:{1}", i, data));
sw.WriteLine(data);
}
else
{
AddList(String.Format("ReadSingleBlock命令执行失败!错误代码:0x{0:X2}", value));
}
}
}
}
}
AddList("标签读取完毕,请移除所有标签,然后放入新的标签。\r\n1秒钟后将开始新的读取。");
Thread.Sleep(1000);
}
--------------------------------------上面是文件保存并写入,但时出现了问题文件写入成功,但是却存不到前面新建的文件夹里而是和文件夹在同一级目录并存,二新建文件夹里没文件。