Is it possible to read date taken of a video not the Created Date or Modified Date ... i tried to check the video details but i always see the Date Taken is Empty
List<string> arrHeaders = new List<string>();
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder objFolder;
objFolder = shell.NameSpace(directoryPath);
for (int i = 0; i < short.MaxValue; i++)
{
string header = objFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
}
foreach (Shell32.FolderItem2 item in objFolder.Items())
{
for (int i = 0; i < arrHeaders.Count; i++)
{
Console.WriteLine("{0}\t{1}: {2}", i, arrHeaders[i], objFolder.GetDetailsOf(item, i));
}
}
Related
Maybe it's a bad day or I am a stupid. I was trying to create a data table from a file upload (.txt).
10|00309|IN3136|EKM|110|13-12-2019|V1.1|||||
20|1|01|02|13122019120157_1||Please Enter Mother's First Name|
20|2|01|02|13122019120157_2||Please Enter Mother's First Name|
20|3|01|02|13122019120157_3||Please Enter Mother's First Name|
is the data inside the text file.
var data = File.ReadAllText(FilePath);
string[] strArr = null;
int count = 0;
char[] splitchar = { '|' };
strArr = data.Split(splitchar);
DataTable dt2 = new DataTable();
dt2.Clear();
dt2.Columns.Add("Col0");
dt2.Columns.Add("Col1");
dt2.Columns.Add("Col2");
dt2.Columns.Add("Col3");
dt2.Columns.Add("Col4");
dt2.Columns.Add("Col5");
dt2.Columns.Add("Col6");
for (count = 0; count <= strArr.Length - 1; count++)
{
if (count >= 11)
{
DataRow _dr2 = dt2.NewRow();
for (int i = 0; i <= 6; i++)
{
if (i == 0)
_dr2["Col0"] = strArr[count+i];
else if (i == 1)
_dr2["Col1"] = strArr[count+i];
else if (i == 2)
_dr2["Col2"] = strArr[count+i];
else if (i == 3)
_dr2["Col3"] = strArr[count+i];
else if (i == 4)
_dr2["Col4"] = strArr[count+i];
else if (i == 5)
_dr2["Col5"] = strArr[count+i];
else if (i == 6)
_dr2["Col6"] = strArr[count+i];
}
dt2.Rows.Add(_dr2);
}
}
I am trying to make it like this
my loops are completely stupid, I know, please point me in the right direction..
That's essentially a CSV file using | as the field separator. Instead of writing your own code, you could use a library like CsvHelper with a custom field separator. You can even parse string values directly into numbers or dates.
CsvHelper offers the CsvReader for reading individual fields or full records and CsvDataReader to load the CSV file as an IDataReader that can be used to load a DataTable or import the data into the database using eg SqlBulkCopy
Borrowing from the documentation example :
using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader))
{
// Do any configuration to `CsvReader` before creating CsvDataReader.
csv.Configuration.Delimiter="|";
using (var dr = new CsvDataReader(csv))
{
var dt = new DataTable();
dt.Load(dr);
}
}
Instead of using File.ReadAllText ,use File.ReadAllLines which is easy to traverse between all the lines as each line is one record .
var lines = File.ReadAllLines(**#Path to the File**);
for (int i = 0; i < lines.Length; i++)
{
var str = lines[i];
var strarray = str.Split('|');
}
The below code will give you the expected result:
Data table filled with the lines from CSV file.
DataTable dt2 = new DataTable();
dt2.Columns.Add("Col0");
dt2.Columns.Add("Col1");
dt2.Columns.Add("Col2");
dt2.Columns.Add("Col3");
dt2.Columns.Add("Col4");
dt2.Columns.Add("Col5");
dt2.Columns.Add("Col6");
var lines = File.ReadAllLines(#"file path"); //C:\ToBeDeleted\test.txt
for (int i = 0; i < lines.Length; i++)
{
var str = lines[i];
var strarray = str.Split('|');
DataRow drow = dt2.NewRow();
drow["Col0"] = strarray[0];
drow["Col1"] = strarray[1];
drow["Col2"] = strarray[2];
drow["Col3"] = strarray[3];
drow["Col4"] = strarray[4];
drow["Col5"] = strarray[5];
drow["Col6"] = strarray[6];
dt2.Rows.Add(drow);
}
To display your expected result use the below console code:
//Displaying as a table.
for (int i = 0; i < dt2.Columns.Count; i++)
{
Console.Write(dt2.Columns[i].ColumnName + " \t |");
}
Console.WriteLine();
for (int j = 0; j < dt2.Rows.Count; j++)
{
for (int i = 0; i < dt2.Columns.Count; i++)
{
Console.Write(dt2.Rows[j].ItemArray[i] + " \t | ");
}
Console.WriteLine();
}
Below is the output of the above code.
Finally, I end up doing this. It's working for me (at least for now :D)
private DataTable ConvertToDataTable(string FilePath)
{
var lines = File.ReadAllLines(FilePath);
DataTable dt2 = new DataTable();
dt2.Clear();
dt2.Columns.Add("Col0");
dt2.Columns.Add("Col1");
dt2.Columns.Add("Col2");
dt2.Columns.Add("Col3");
dt2.Columns.Add("Col4");
dt2.Columns.Add("Col5");
dt2.Columns.Add("Col6");
for (int i = 0; i < lines.Length; i++)
{
var str = lines[i];
if (i > 0)
{
var strarray = str.Split('|');
DataRow _dr2 = dt2.NewRow();
for (int count = 0; count <= strarray.Length - 1; count++)
{
if (count == 0)
_dr2["Col0"] = strarray[count];
else if (count == 1)
_dr2["Col1"] = strarray[count];
else if (count == 2)
_dr2["Col2"] = strarray[count];
else if (count == 3)
_dr2["Col3"] = strarray[count];
else if (count == 4)
_dr2["Col4"] = strarray[count];
else if (count == 5)
_dr2["Col5"] = strarray[count];
else if (count == 6)
_dr2["Col6"] = strarray[count];
}
dt2.Rows.Add(_dr2);
}
}
return dt2;
}
How to Save multiple files in asp.net 4.0 by Fileupload Control with multiple fileupload control?
I have two fileupload control one for image and second for thumbimage. So I want to save multiple image and thumbimage ?
You are actually overwriting the fist file with the second one when you are in the loop. I would suggest you to create a list for the files and add to the list in the loop like below. You will then have a list of file when you can use firstOrDefault() for the first item as well as use Skip() and Take() to select any item you want.
HttpFileCollection uploadedFiles = Request.Files;
List<HttpPostedFile> fileList1 = new List<HttpPostedFile>();
List<HttpPostedFile> fileList2 = new List<HttpPostedFile>();
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile hpf = uploadedFiles[i];
var hpfKey = uploadedFiles.Keys[i];
if (hpfKey.IndexOf("FileUpload1") > 0)
{
fileList1.Add(hpf);
}
if (hpfKey.IndexOf("FileUpload2") > 0)
{
fileList2.Add(hpf);
}
}
Update:
Now to get the first file you call FirstOrDefault() on the list like below:
fileList1.FirstOrDefault();
And to get the second file :
fileList1.Skip(1).FirstOrDefault();
HttpFileCollection uploadedFiles = Request.Files;
int i = uploadedFiles.Count;
List<HttpPostedFile> fileList1 = new List<HttpPostedFile>();
List<HttpPostedFile> fileList2 = new List<HttpPostedFile>();
if (i > 0)
{
for (int j = 0; j < i/2; j++)
{
fileList1.Add(uploadedFiles[j]);
}
}
if (i > 0)
{
for (int j = i / 2; j < i; j++)
{
fileList2.Add(uploadedFiles[j]);
}
}
int filecount = fileList1.Count;
if (filecount > 0)
{
for (int j = 0; j < filecount; j++)
{
string image = fileList1[j].FileName;
fileList1[j].SaveAs(imagepath);
string image = fileList2[j].FileName;
fileList2[j].SaveAs(imagepath);
}
}
I've created a for loop that creates n folders. I would like to create a text file in each folder. How do i do it?
for (int i = 1; i < 17; i++)
{
System.IO.Directory.CreateDirectory(
String.Format(#"C:\Users\xxx\Desktop\xx\Test{0:d2}", i));
}
I found a better solution.
for (int i = 1; i < 17; i++)
{
Directory.CreateDirectory(String.Format(#"C:\Users\xxx\Desktop\xx\Test"+i, i));
if (!File.Exists(string.Format(#"C:\Users\xxx\Desktop\xx\Test{0}/Test.txt", i)))
{
File.WriteAllText(string.Format(#"C:\Users\xxx\Desktop\xx\Test{0}/Test.txt", i), " ");
}
Try this
for (int i = 1; i < 17; i++)
{
var folder = System.IO.Directory.CreateDirectory(String.Format(#"C:\Users\xxx\Desktop\xx\Test{0:d2}", i));
System.IO.File.WriteAllText(folder.FullName + #"\WriteText.txt", "your text content");
}
Update
if you want more than one file
for (int i = 1; i < 17; i++)
{
var folder = System.IO.Directory.CreateDirectory(String.Format(#"C:\Users\xxx\Desktop\xx\Test{0:d2}", i));
System.IO.File.WriteAllText(folder.FullName + #"\WriteText1.txt", "your text content 1");
System.IO.File.WriteAllText(folder.FullName + #"\WriteText2.txt", "your text content 2");
}
Try this:
var desktop_path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
for (int i = 1; i < 17; i++)
{
var folder_path = System.IO.Path.Combine(desktop_path, String.Format(#"xx\Test{0:d2}", i));
var file_path = System.IO.Path.Combine(folder_path, "file.txt");
System.IO.Directory.CreateDirectory(folder_path);
System.IO.File.WriteAllText(file_path, "content");
}
This code finds the current user's desktop path, and then uses System.IO.Path.Combine to ensure that paths are correctly concatenated together.
for (int i = 1; i < 17; i++)
{
var dir = System.IO.Directory.CreateDirectory
(String.Format(#"C:\Users\xxx\Desktop\xx\Test{0:d2}", i));
System.IO.File.Create(dir.FullName+ #"\MyFile.txt");
}
To create add content on the file, we can use FileStream object returned by File.Create()
Try this above,
I hope this may easy for you
string path = #"d:\\dummyfolder";
for (int i = 0; i < 17; i++)
{
string _folderPath = string.Format("{0}\\{1}", path, i);
if (!Directory.Exists(_folderPath))
{
//creating folder
Directory.CreateDirectory(_folderPath);
//creating text file
string _filePath = string.Format("{0}\\{1}\\{1}.txt", path, i);
string text = i + " " + "Content of the text file ";
File.WriteAllText(_filePath, text);
}
}
On one of my WebPages I have placed FileUpload1 item, in which I would like to store CSV file and then analyze it. However I can't read it in, an Exception is thrown. It seems like the filepath changes (I have file store on Desktop, whereas it displays that the file is searched in C:\Program Files (x86)\IIS Express).
protected void Button2_Click(object sender, EventArgs
{
Response.Write("Jestem w petli file.hasfile");
if (FileUpload1.PostedFile != null)
{
string filePath = FileUpload1.PostedFile.FileName;
StreamReader reader = new StreamReader(filePath);
string[] readLines = new string[7];
int dayOfWeek = 0;
while (dayOfWeek < 7)
{
do
{
string textLine = reader.ReadToEnd();
readLines[dayOfWeek] = textLine;
}
while (reader.Peek() != -1);
dayOfWeek++;
}
for(int i = 0; i < 7; i++)
{
generateData(readLines[i], i);
}
reader.Close();
}
for(int i = 0; i < 7; i++)
{
TableRow tblRow = new TableRow();
Table1.Rows.Add(tblRow);
for (int j = 0; j < 2; j++)
{
TableCell tCell = new TableCell();
tCell.Text = positionsInWtr[i, j];
tblRow.Cells.Add(tCell);
}
}
}
Could you tell me what I am doing wrong?
Thanks in advance,
Kokos
I have a DataGridView in a .Net application (V4 C# VS2010) & want to copy all the data to the clipboard on the click of a button. No problem -
private void copyToClipboard()
{
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
Problem is that the user might already have some cells, rows etc selected on the DataGrid & I don't really want to change that selection. The above obviously selects everything. I could dataGridView1.ClearSelection(); at the end which is marginally better but still doesn't achieve what's required.
I can save the selected cells:
var mySelectedCells = dataGridView1.SelectedCells;
but how do I get those selected cells reselected on the DataGrid after the copy? Is there an easy way to get the selected cells collection back into the DataGrid? Perhaps there is a better way to get the whole grid copied to the clipboard in the first place without affecting presently selected cells?
I suppose if you just wanted to represent the contents of the cells as text and copy them to the clipboard, tab-delimited, you could do something like:
var newline = System.Environment.NewLine;
var tab = "\t";
var clipboard_string = "";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i=0; i < row.Cells.Count; i++)
{
if(i == (row.Cells.Count - 1))
clipboard_string += row.Cells[i].Value + newline;
else
clipboard_string += row.Cells[i].Value + tab;
}
}
Clipboard.SetText(clipboard_string);
The output seems pretty similar to that of the GetClipboardContent(), but be careful for any DataGridViewImageColumns or any type that isn't implicitly a string.
Edit: Anthony is correct, use StringBuilder to avoid allocating a new string for every concatenation. The new code:
var newline = System.Environment.NewLine;
var tab = "\t";
var clipboard_string = new StringBuilder();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
if (i == (row.Cells.Count - 1))
clipboard_string.Append(row.Cells[i].Value + newline);
else
clipboard_string.Append(row.Cells[i].Value + tab);
}
}
Clipboard.SetText(clipboard_string.ToString());
Here is a version of the VB code in C# with options to copy headers and to only copy selected rows.
private void CopyDataGridViewToClipboard(DataGridView dgv, bool includeHeaders = true, bool allRows = false)
{
// copies the contents of selected/all rows in a data grid view control to clipboard with optional headers
try
{
string s = "";
DataGridViewColumn oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
if (includeHeaders)
{
do
{
s = s + oCurrentCol.HeaderText + "\t";
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
}
while (oCurrentCol != null);
s = s.Substring(0, s.Length - 1);
s = s + Environment.NewLine; //Get rows
}
foreach (DataGridViewRow row in dgv.Rows)
{
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
if (row.Selected || allRows)
{
do
{
if (row.Cells[oCurrentCol.Index].Value != null) s = s + row.Cells[oCurrentCol.Index].Value.ToString();
s = s + "\t";
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
}
while (oCurrentCol != null);
s = s.Substring(0, s.Length - 1);
s = s + Environment.NewLine;
}
}
Clipboard.SetText(s);
}
catch (Exception ex)
{
toolStripStatusLabel2.Text = #"Error: " + ex.Message;
}
}
You might want to include the column headers:
private void copyAllToClipboard()
{
var newline = System.Environment.NewLine;
var tab = "\t";
var clipboard_string = new StringBuilder();
int i;
for (i = 0; i < this.Columns.Count - 1; i++)
{
clipboard_string.Append(this.Columns[i].Name);
clipboard_string.Append(tab);
}
clipboard_string.Append(this.Columns[i].Name);
clipboard_string.Append(newline);
foreach (DataGridViewRow row in this.Rows)
{
for ( i = 0; i < row.Cells.Count - 1; i++)
{
clipboard_string.Append(row.Cells[i].Value);
clipboard_string.Append(tab);
}
clipboard_string.Append(row.Cells[i].Value);
clipboard_string.Append(newline);
}
Clipboard.SetText(clipboard_string.ToString());
}
I think below method will exactly do what you want. Just call this method with the DataGridView name at the button click event.
Private Sub CopyDataGridViewToClipboard(ByRef dgv As DataGridView)
Try
Dim s As String = ""
Dim oCurrentCol As DataGridViewColumn 'Get header
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
Do
s &= oCurrentCol.HeaderText & Chr(Keys.Tab)
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, _
DataGridViewElementStates.Visible, DataGridViewElementStates.None)
Loop Until oCurrentCol Is Nothing
s = s.Substring(0, s.Length - 1)
s &= Environment.NewLine 'Get rows
For Each row As DataGridViewRow In dgv.Rows
oCurrentCol = dgv.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
Do
If row.Cells(oCurrentCol.Index).Value IsNot Nothing Then
s &= row.Cells(oCurrentCol.Index).Value.ToString
End If
s &= Chr(Keys.Tab)
oCurrentCol = dgv.Columns.GetNextColumn(oCurrentCol, _
DataGridViewElementStates.Visible, DataGridViewElementStates.None)
Loop Until oCurrentCol Is Nothing
s = s.Substring(0, s.Length - 1)
s &= Environment.NewLine
Next 'Put to clipboard
Dim o As New DataObject
o.SetText(s)
Clipboard.SetDataObject(o, True)
Catch ex As Exception
ShowError(ex, Me)
End Try
End Sub
DataGridView columns can be visible/invisible and also can be displayed in different order then the order they were created. This code takes care of both:
public static void CopyGridViewToClipboard(DataGridView gvCopy)
{
if (gvCopy == null) return;
StringBuilder s = new StringBuilder();
int offset = gvCopy.ColumnHeadersVisible ? 1 : 0;
int visibleColumnsCount = 0;
//count visible columns and build mapping between each column and it's display position
Dictionary<int, int> indexMapping = new Dictionary<int, int>();
int currIndex = 0;
int lastFoundMinDisplayIndex = -1;
for (int j = 0; j < gvCopy.ColumnCount; j++)
{
//find min DisplayIndex >= currIndex where column is visible
int minDisplayIndex = 100000;
int minDisplayIndexColumn = 100000;
for (int k = 0; k < gvCopy.ColumnCount; k++)
{
if ((gvCopy.Columns[k].Visible) && (gvCopy.Columns[k].DisplayIndex >= currIndex) && (gvCopy.Columns[k].DisplayIndex > lastFoundMinDisplayIndex))
{
if (gvCopy.Columns[k].DisplayIndex < minDisplayIndex)
{
minDisplayIndex = gvCopy.Columns[k].DisplayIndex;
minDisplayIndexColumn = k;
}
}
}
if (minDisplayIndex == 100000) break;
indexMapping.Add(minDisplayIndexColumn, currIndex);
lastFoundMinDisplayIndex = minDisplayIndex;
currIndex++;
}
visibleColumnsCount = currIndex;
//put data in temp array -- required to position columns in display order
string[,] data = new string[gvCopy.RowCount + offset, visibleColumnsCount];
if (gvCopy.ColumnHeadersVisible)
{
for (int j = 0; j < gvCopy.ColumnCount; j++)
{
if (gvCopy.Columns[j].Visible)
{
data[0, indexMapping[j]] = gvCopy.Columns[j].HeaderText;
}
}
}
for (int i = 0; i < gvCopy.RowCount; i++)
{
for (int j = 0; j < gvCopy.ColumnCount; j++)
{
if (gvCopy.Columns[j].Visible)
{
data[i + offset, indexMapping[j]] = gvCopy[j, i].FormattedValue.ToString();
}
}
}
//copy data
for (int i = 0; i < gvCopy.RowCount + offset; i++)
{
for (int j = 0; j < visibleColumnsCount; j++)
{
s.Append(data[i, j]);
s.Append("\t");
}
s.Append("\r\n");
}
Clipboard.SetDataObject(s.ToString());
}
You should change multiselect property of DataGridView. Here is the code:
private void copyToClipboard()
{
dataGridView1.MultiSelect = True;
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}