471,078 Members | 844 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,078 software developers and data experts.

Missing first 2 charcters of file name in win ce(.net cf 2.0) using findfirstfile

4
hi,

My code works but i am missing the first 2 characters of file name.
Any idea why????

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10.  
  11.  
  12. namespace dummy2
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         int rfidTagHandle;
  17.         Win32Methods.WIN32_FIND_DATA FindFileData;
  18.  
  19.  
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void button1_Click(object sender, EventArgs e)
  27.         {
  28.  
  29.             rfidTagHandle = Win32Methods.FindFirstFile("\\*.*", out FindFileData);
  30.             //"\\RFID\\[.]"
  31.             if (rfidTagHandle == Win32Methods.INVALID_HANDLE_VALUE)
  32.             {
  33.                 MessageBox.Show("FindFirstFile failed");
  34.  
  35.             }
  36.             else
  37.             {
  38.  
  39.                 //Byte[] fileName =FindFileData.cFileName;
  40.                 //string filen = System.Text.UnicodeEncoding.Unicode.GetString(fileName, 0, fileName.Length);
  41.                 //StringBuilder cb = new StringBuilder(256);
  42.                 //cb = FindFileData.cFileName;
  43.                 string filen = FindFileData.cFileName;
  44.                 textBox1.Text = textBox1.Text + filen + "\r\n";
  45.             }
  46.  
  47.             while (Win32Methods.FindNextFile(rfidTagHandle, out FindFileData) != false)
  48.             {
  49.                 //Byte[] fileName = FindFileData.cFileName;
  50.                 //string filen = System.Text.UnicodeEncoding.Unicode.GetString(fileName, 0, fileName.Length);
  51.                 //StringBuilder cb = new StringBuilder(256);
  52.                 //cb = FindFileData.cFileName;
  53.                 string filen = FindFileData.cFileName;
  54.                 textBox1.Text = textBox1.Text + filen + "\r\n";
  55.  
  56.  
  57.             }
  58.             MessageBox.Show("FindnextFile failed");
  59.             Win32Methods.FindClose(rfidTagHandle);
  60.  
  61.         }
  62.  
  63.  
  64.     }
  65.  
  66.     public class Win32Methods
  67.     {
  68.  
  69.         public const int INVALID_HANDLE_VALUE = -1;
  70.         //, CharSet = CharSet.Unicode
  71.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  72.         public struct WIN32_FIND_DATA
  73.         {
  74.             public uint dwFileAttributes;
  75.             public FILETIME ftCreationTime;
  76.             public FILETIME ftLastAccessTime;
  77.             public FILETIME ftLastWriteTime;
  78.             public uint nFileSizeHigh;
  79.             public uint nFileSizeLow;
  80.             public uint dwReserved0;
  81.             public uint dwReserved1;
  82.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  83.             public string cFileName;
  84.             //[MarshalAs(UnmanagedType.LPStr)]
  85.             //public StringBuilder cFileName;
  86.             //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 260)]
  87.             //public Byte[] cFileName;
  88.             //[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  89.             //public string cAlternateFileName;
  90.  
  91.         };
  92.  
  93.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  94.         public struct FILETIME
  95.         {
  96.             public uint dwLowDateTime;
  97.             public uint dwHighDateTime;
  98.         };
  99.  
  100.         //Findfirstfile
  101.         [DllImport("coredll.dll", CharSet = CharSet.Auto,SetLastError = true)]
  102.         public static extern int FindFirstFile(string szSearchFile,out WIN32_FIND_DATA lpFindFileData);
  103.  
  104.         //FindNextFile
  105.         [DllImport("coredll.dll",CharSet = CharSet.Auto, SetLastError = true)]
  106.         [return: MarshalAs(UnmanagedType.Bool)]
  107.         public static extern bool FindNextFile(int hFindFile,out WIN32_FIND_DATA lpFindFileData);
  108.         //Findclose
  109.         [DllImport("coredll.dll",CharSet = CharSet.Auto, SetLastError = true)]
  110.         [return: MarshalAs(UnmanagedType.Bool)]
  111.         public static extern bool FindClose(int hFindFile);
  112.     }
  113.  
  114. }
Nov 28 '07 #1
3 1681
Plater
7,872 Expert 4TB
Why are you doing all of that with unmanaged code?
What was wrong with:
Expand|Select|Wrap|Line Numbers
  1. Directory.GetFiles(Path, pattern);
  2.  
???
System.IO is still in CF2
Nov 28 '07 #2
gudiya
4
actually I have a handheld reader which has a file system and starts reading rfid tags using the command findfirst file
Nov 29 '07 #3
Plater
7,872 Expert 4TB
EDIT: WOW I can't believe I missed that.
You're using a Unicode conversion, it's probably treating the first two characters as the byte-order mark.
Try using the ASCII encoding and see what you get?
Nov 29 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Corepaul | last post: by
2 posts views Thread by tom | last post: by
1 post views Thread by Paulo Eduardo | last post: by
14 posts views Thread by tshad | last post: by
13 posts views Thread by Andrew | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.