Search code examples
c#oledbdbf

DBF File name truncated


I used the below code to create a dbf file and filling it, everything works fine . the problem is the name of the file got truncated to 8 characters max. Any idea why or how to make maintain the whole name?

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+";Extended Properties=dBase IV";
OleDbConnection connection = new OleDbConnection(connectionString);
        connection.Open();
cmd.CommandText = @"CREATE TABLE calendfull(
                                date1 datetime ,
                                day1 int ,
                                month1 int ,
                                year1 int ,
                                dow int ,
                                endmonth int  
                                )";

            cmd.ExecuteNonQuery();

  foreach (DataRow row in calend.Rows)
            {
                day = Convert.ToInt32(row["day"]);
                year = Convert.ToInt32(row["year"]);
                month = Convert.ToInt32(row["month"]);
                dow = Convert.ToInt32(row["dow"]);
                endmonth = Convert.ToInt32(row["endmonth"]);
                date1 = Convert.ToDateTime(row["date1"]);

                cmd.CommandText = @"insert into calendFull  values ('" + date1 + "'," + day + "," + month + "," + year + "," + dow + "," + endmonth + ")";
                cmd.ExecuteNonQuery();
            }

Solution

  • DBF file names follows 8.3 naming specification, so there names cannot be more than 8 characters.

    But wait, I have seen some dbf files with names larger that 8 character.

    Yes, it is allowed but their actual name is something like file names when you are looking in command prompts. e.g largef~1

    So how can I read them? you should get their short name using the kernel32.dll

    Here is the code:

        public string GetShortFileName(string fileDirectory, string fileNameWithExtension)
        {
            StringBuilder temp = new StringBuilder(255);
    
            string path = System.IO.Path.Combine(fileDirectory, fileNameWithExtension);
    
            int n = GetShortPathName(path, temp, 255);
    
            if (n == 0)
                throw new NotImplementedException();
    
            string extension = System.IO.Path.GetExtension(path);
    
            return ((temp.ToString().Split('\\')).Last()).ToLower();//.Replace(extension, string.Empty);
        }
    
        [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
                public static extern int GetShortPathName(
            [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]    
            string path,
            [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]    
            StringBuilder shortPath,
            int shortPathLength);