Click or drag to resize

DataSourceImporterReadDataTableFromTextFile Method

Read table data from a file (.txt,.csv,.xls,.xlsx,etc.).

Namespace:  Demo3D.Utilities
Assembly:  WpfControls (in WpfControls.dll) Version: 15.0.2.11458
Syntax
C#
public static DataTable ReadDataTableFromTextFile(
	string path
)

Parameters

path
Type: SystemString
The path to the file containing the table data.

Return Value

Type: DataTable
A DataTable containing the columns and rows from the file.
Examples
C#
[Auto] CustomPropertyValue<UserResourceReference> Data;

[Auto] void OnReset(BoxVisual sender) {
    if (!sender.HasProperty("Data")) return;

    var ur = document.FindUserResource(this.Data);
    var localFile = WriteUserResourceToTemporaryFile( ur );

    if (localFile == "") return;

    var dataTable =
        Demo3D.Utilities.DataSourceImporter.ReadDataTableFromTextFile( localFile );

    File.Delete( localFile );

    foreach (var colName in dataTable.Columns) {
        print(colName);
    }
}

string WriteUserResourceToTemporaryFile(UserResource ur) {
    if (ur == null) return "";

    var ins   = ur.GetInputStream(null);
    var len   = ins.Length;
    var bytes = new byte[len];

    ins.Read( bytes, 0, (int)len );

    var tmpFile = 
        Path.Combine( Path.GetTempPath(), Path.GetFileName(ur.LocalPath) );

    File.WriteAllBytes( tmpFile, bytes );

    return tmpFile;
}
See Also