My code works but i am missing the first 2 characters of file name.
Any idea why????
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Threading;
- namespace dummy2
- {
- public partial class Form1 : Form
- {
- int rfidTagHandle;
- Win32Methods.WIN32_FIND_DATA FindFileData;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- rfidTagHandle = Win32Methods.FindFirstFile("\\*.*", out FindFileData);
- //"\\RFID\\[.]"
- if (rfidTagHandle == Win32Methods.INVALID_HANDLE_VALUE)
- {
- MessageBox.Show("FindFirstFile failed");
- }
- else
- {
- //Byte[] fileName =FindFileData.cFileName;
- //string filen = System.Text.UnicodeEncoding.Unicode.GetString(fileName, 0, fileName.Length);
- //StringBuilder cb = new StringBuilder(256);
- //cb = FindFileData.cFileName;
- string filen = FindFileData.cFileName;
- textBox1.Text = textBox1.Text + filen + "\r\n";
- }
- while (Win32Methods.FindNextFile(rfidTagHandle, out FindFileData) != false)
- {
- //Byte[] fileName = FindFileData.cFileName;
- //string filen = System.Text.UnicodeEncoding.Unicode.GetString(fileName, 0, fileName.Length);
- //StringBuilder cb = new StringBuilder(256);
- //cb = FindFileData.cFileName;
- string filen = FindFileData.cFileName;
- textBox1.Text = textBox1.Text + filen + "\r\n";
- }
- MessageBox.Show("FindnextFile failed");
- Win32Methods.FindClose(rfidTagHandle);
- }
- }
- public class Win32Methods
- {
- public const int INVALID_HANDLE_VALUE = -1;
- //, CharSet = CharSet.Unicode
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
- public struct WIN32_FIND_DATA
- {
- public uint dwFileAttributes;
- public FILETIME ftCreationTime;
- public FILETIME ftLastAccessTime;
- public FILETIME ftLastWriteTime;
- public uint nFileSizeHigh;
- public uint nFileSizeLow;
- public uint dwReserved0;
- public uint dwReserved1;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
- public string cFileName;
- //[MarshalAs(UnmanagedType.LPStr)]
- //public StringBuilder cFileName;
- //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 260)]
- //public Byte[] cFileName;
- //[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
- //public string cAlternateFileName;
- };
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
- public struct FILETIME
- {
- public uint dwLowDateTime;
- public uint dwHighDateTime;
- };
- //Findfirstfile
- [DllImport("coredll.dll", CharSet = CharSet.Auto,SetLastError = true)]
- public static extern int FindFirstFile(string szSearchFile,out WIN32_FIND_DATA lpFindFileData);
- //FindNextFile
- [DllImport("coredll.dll",CharSet = CharSet.Auto, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool FindNextFile(int hFindFile,out WIN32_FIND_DATA lpFindFileData);
- //Findclose
- [DllImport("coredll.dll",CharSet = CharSet.Auto, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool FindClose(int hFindFile);
- }
- }