Compare two Folder Contents using c# - c#

I need to compare two folder contents based on
1.No of files
2.Size of files
3 date
and i got an error index out of bound exception in this piece of code
private void SeekFiles(string Root)
{
string[] Files = System.IO.Directory.GetFiles(Root);
string[] Folders = System.IO.Directory.GetDirectories(Root);
FileInfo File;
for(int i=0; i< Folders.Length; i++)
{
File = new FileInfo(Files[i]);
FolderSize += File.Length;
}
for(int i=0; i< Folders.Length-1; i++)
{
SeekFiles(Folders[i]);
}
}
Any Suggestion??

Looks like you are using wrong index on a wrong collection :
for(int i=0; i< Folders.Length; i++)
{
File = new FileInfo(Files[i]);
FolderSize += File.Length;
}
Should be :
for(int i=0; i< **Files.Length**; i++)
{
File = new FileInfo(Files[i]);
FolderSize += File.Length;
}

You are using Files[i] but i < Folders.Length in the first for.

for(int i=0; i< Folders.Length; i++)
{
File = new FileInfo(Files[i]);
FolderSize += File.Length;
}
This should be Files.Length

Related

How to load data into program as double? (not string) in c# (window form application - visual studio)

I dont know how to ask, but i want to load data in double not in string as you can see in the code. Can i change them in double? i want to store them in array because i want to find maximum value from the data. cannot find them in string.
private void button1_openfile_Click(object sender, EventArgs e)
{
//load data from text file
string[] lines = File.ReadAllLines(#"C:\Users\Siti Nurhazwani\Desktop\table.txt");
string[] values;
for (int i = 0; i < lines.Length; i++)
{
values = lines[i].ToString().Split('/');
string[] row = new string[values.Length];
for (int j = 0; j < values.Length; j++)
{
row[j] = values[j];
}
table.Rows.Add(row);
}
}
can someone share code to me how to change the string to double? please use simple terms i am a beginner in c#.
Change these lines in your code to double.
string[] row = new string[values.Length];
for (int j = 0; j < values.Length; j++)
{
row[j] = values[j];
}
Instead
double[] row = new double[values.Length];
for (int j = 0; j < values.Length; j++)
{
row[j] = Convert.ToDouble(values[j]);
}
I'm not sure if I understood your question correctly. Can you do the change from string to double inside the loop? If so, this should be quite easy to solve, try this for example:
var result = Convert.ToDouble(originalValue)
https://learn.microsoft.com/en-us/dotnet/api/system.convert.todouble?view=net-5.0

How to Save multiple files in asp.net 4.0 by Fileupload1 Control with multiple fileupload2 control?

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);
}
}

Create files in multiple folders

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);
}
}

Can't read CSV file in using FileUpload in ASP.NET

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

Adding a text file into a list and then into a Two Dimensional array

I have a text file that has the following in it: (Without quotation marks and "Empty Space")
##############
# Empty Space#
# Empty Space#
# Empty Space#
# Empty Space#
##############
I want to add this whole file row by row into a list:
FileStream FS = new FileStream(#"FilePath",FileMode.Open);
StreamReader SR = new StreamReader(FS);
List<string> MapLine = new List<string>();
foreach (var s in SR.ReadLine())
{
MapLine.Add(s.ToString());
}
foreach (var x in MapLine)
{
Console.Write(x);
}
Here comes my problem: I want to add this into a Two dimensional array. I tried:
string[,] TwoDimentionalArray = new string[100, 100];
for (int i = 0; i < MapLine.Count; i++)
{
for (int j = 0; j < MapLine.Count; j++)
{
TwoDimentionalArray[j, i] = MapLine[j].Split('\n').ToString();
}
}
I am still new to C# so please any help will be appreciated.
You can try with this:
// File.ReadAllLines method serves exactly the purpose you need
List<string> lines = File.ReadAllLines(#"Data.txt").ToList();
// lines.Max(line => line.Length) is used to find out the length of the longest line read from the file
string[,] twoDim = new string[lines.Count, lines.Max(line => line.Length)];
for(int lineIndex = 0; lineIndex < lines.Count; lineIndex++)
for(int charIndex = 0; charIndex < lines[lineIndex].Length; charIndex++)
twoDim[lineIndex,charIndex] = lines[lineIndex][charIndex].ToString();
for (int lineIndex = 0; lineIndex < lines.Count; lineIndex++)
{
for (int charIndex = 0; charIndex < lines[lineIndex].Length; charIndex++)
Console.Write(twoDim[lineIndex, charIndex]);
Console.WriteLine();
}
Console.ReadKey();
This is going to save each character of the file content into it's own position in a 2-dimensional array. For that purpose char[,] might have also been used.
Currently, you are going through all the lines of your file, and for every line of your file, you go through all the lines of your file again, to split them on \n which is already done by you putting them in MapLine.
If you want every single char of a line array'd, and that in an array again, it should approximately look like this:
string[,] TwoDimentionalArray = new string[100, 100];
for (int i = 0; i < MapLine.Count; i++)
{
for (int j = 0; j < MapLine[i].length(); j++)
{
TwoDimentionalArray[i, j] = MapLine[i].SubString(j,j);
}
}
I did this without testing so it might be faulty. The point is you need to iterate through each line first, then through each letter in that line. From there, you can use SubString.
Also, I hope I've understood your question correctly.

Categories

Resources