C#中FolderBrowserDialog fbd = new FolderBrowserDialog();下如何解决新建文件夹重命名并保存文件的问题!急!

copymader 2014-02-27 08:27:23
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);
}
--------------------------------------上面是文件保存并写入,但时出现了问题文件写入成功,但是却存不到前面新建的文件夹里而是和文件夹在同一级目录并存,二新建文件夹里没文件。
...全文
330 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyun0112 2014-02-28
  • 打赏
  • 举报
回复
你这有个continue; 当然不往下走了,又跳到上一级

http://msdn.microsoft.com/zh-cn/library/923ahwt1.aspx

*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
copymader 2014-02-28
  • 打赏
  • 举报
回复
找不到回复的骂我后来找到原因,在 FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { _FilePath = fbd.SelectedPath+"//"; //加一个“//” } 但是跳到 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) { //AddList(String.Format("错误:Inventory命令执行失败,错误代码:0x{0:X2}!", value)); Thread.Sleep(500); ///到这就不往下走了,又跳到上一级,郁闷 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 = String.Format("{0}{1}.txt", _FilePath + "\\", RvsTag); using (FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧