Returning 2nd column of a data table to a filename doesn't get written to the copied file.

  • Thread starter Thread starter SlenderMan564
  • Start date Start date
S

SlenderMan564

Guest
Using .NET 4.5 I’m having a problem writing the 2'nd column of a data table to a filename that is being renamed in a drag drop box. I’m using a method ReadSecondDataColumn() to return and delimit the second column with “_” but it is not being written into the file name that get's copied.

The method is being called in RenameFile(string targetDirectory).

Obviously I’m not clear on the logic. Any pointers, please? Here's the code.


using System;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

namespace CannotReadDataTableInFilename
{
public partial class Form1 : Form
{
#region Declarations

static string[] _dropppedFiles;

string _strSourcePath;
string _strTargetPath;
string _strfileName = string.Empty;
string _strDestFile = string.Empty;

public static DataTable _dt = new DataTable();
#endregion

#region Initializers
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Populate_DataTable_with_dummy_data();
}
#endregion Initializers

#region Events

private void Form1_DragDrop(object sender, DragEventArgs e)
{
//TAKE dropped items and store in array.
_dropppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop);

//LOOP through all droppped items and display them
foreach (string file in _dropppedFiles)
{
listBox1.Items.Add(file);
}
}

private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
e.Effect = DragDropEffects.All;
}
}

private void btnRenameFiles_Click(object sender, EventArgs e)
{
_strTargetPath = txtFolderPath.Text;
RenameFile(_strTargetPath);
}
#endregion

#region Methods

public void RenameFile(string targetDirectory)
{
//Get file regardless of specific extension.
foreach (string file in _dropppedFiles)
{
if (!listBox1.Items.Contains(_dropppedFiles.Length))
{
//GET file extension.
var extension = Path.GetExtension(file);
//BUG! Can't read the second column.
_strfileName = File.GetCreationTime(file).ToString(ReadSecondDataColumn()

+ "yyyy'-'MM'-'dd hh'\u0589'mm'\u0589'ss tt", CultureInfo.InvariantCulture);
_strfileName += extension;
_strSourcePath = getDirectoryName(file);
//GET Creation Date and time from file.
_strDestFile = Path.Combine(targetDirectory, _strfileName);
//Copy file with extension to output directory.
File.Copy(file, _strDestFile, true);
}

else
{
Debug.WriteLine("Source path does not exist!");
}
}
}

private string getFileName(string path)
{
return Path.GetFileName(path);
}

private string getDirectoryName(string path)
{
return Path.GetDirectoryName(path);
}

private void Populate_DataTable_with_dummy_data()
{
_dt.Columns.Add("Description", typeof(string));
_dt.Columns.Add("Data", typeof(string));
string[] row0 = { "Address", "76 Douglas St Wakecorn" };
string[] row1 = { "Property name", "Wakecorn University" };
string[] row2 = { "Building", "C Block" };
string[] row3 = { "Room", "C2.18" };

_dt.Rows.Add(row0);
_dt.Rows.Add(row1);
_dt.Rows.Add(row2);
_dt.Rows.Add(row3);
}

public DataTable ReadSecondDataColumn()
{
string currentCellValue = string.Empty;
foreach (DataRow dr in _dt.Rows)
{
currentCellValue = dr[1].ToString() + "_";
}
return _dt;
}
#endregion Methods
}
}

Continue reading...
 
Back
Top