Shell32 and UI freezing

  • Thread starter Thread starter CorinthiFy223
  • Start date Start date
C

CorinthiFy223

Guest
So before anything else, I'm not a programmer. I just learned this programming language in a college course.

For some background, my program lists the properties of a file and then put them in a text file. Aside from that, it also creates an empty file that acts like a placeholder for the file. Basically, it's for archiving the information about the files that are about to be deleted.

This is how it looks like after the files have been processed. Apparently I can't upload an image or include a link so here: i(dot)imgur(dot)com/4XIxDec(dot)png

Like what I've mentioned, I"m not a programmer nor someone proficient at this language or for any other programming language for that matter. I just searched anything I needed. Basically I was just frankensteining the whole program. While searching, I found about Shell32. While it worked, it freezes the UI and it's kind of annoying. I'm not sure but I think the freezing happens when Shell32 lists the properties of the files. I thought about trying BackgroundWorker but it didn't work because apparently Shell32 has to run on the STA or main thread (I'm not sure, sorry). What can I do to stop the freezing?

This is the piece of code responsible for that. This is without the BackgroundWorker thing.

// I drop the files I want to be processed on a datagridview
if(dataGridView_SourcePath.Rows.Count < 1)
{
return;
}


foreach (DataGridViewRow row in dataGridView_SourcePath.Rows)
{
string sourcePath = row.Cells[0].Value.ToString();

// My program creates 2 files. A .deleted file which is basically an empty file. It's like a placeholder for the file. The other one is text file that contains the properties of the file.
string deletedFilePath = sourcePath + ".deleted";

File.Create(deletedFilePath).Close();

// Creating DateTime variables that contain the Date properties of the source file.
DateTime sourceFileCreatedDate = File.GetCreationTime(@sourcePath);
DateTime sourceFileModifiedDate = File.GetLastWriteTime(@sourcePath);
DateTime sourceFileAccessedDate = File.GetLastAccessTime(@sourcePath);

// Setting the Date properties of the new file using the Date properties of the source file.
File.SetCreationTime(deletedFilePath, sourceFileCreatedDate);
File.SetLastWriteTime(deletedFilePath, sourceFileModifiedDate);
File.SetLastAccessTime(deletedFilePath, sourceFileAccessedDate);

// The text file the contains the properties of the file.
string textFileName = deletedFilePath+".txt";

try
{
// Check if file already exists. If yes, delete it.
if (File.Exists(textFileName))
{
File.Delete(textFileName);
}

// Create a new file
using (FileStream fs = File.Create(textFileName))
{
// This variable is basically for containing all possible properties that a file can have.
// Based on my testing, all properties available are 300+. [ref2353]
List<string> arrHeaders = new List<string>();

// I'm not sure what this is but it's included in the code I copied so didn't remove it[ref2234]
List<Tuple<int, string, string>> attributes = new List<Tuple<int, string, string>>();

Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder objFolder = shell.NameSpace(Path.GetDirectoryName(sourcePath));
Shell32.FolderItem folderItem = objFolder.ParseName(Path.GetFileName(sourcePath));

// Getting the list of properties... [ref2353]
for (int i = 0; i < 305; i++)
{
string header = objFolder.GetDetailsOf(null, i);
arrHeaders.Add(header);

}

// Adding a date to the deletion details text file.
string textDatePerformed = string.Format("Operation performed on: {0:yyyy_MM_dd}, {1}, {2:hh_mm_ss tt}", DateTime.Now, DateTime.Now.DayOfWeek, DateTime.Now);
Byte[] textDatePerformedByte = new UTF8Encoding(true).GetBytes(textDatePerformed + "\n\n");
fs.Write(textDatePerformedByte, 0, textDatePerformedByte.Length);


// Finding out the available properites of the file.
// Only those non-blanks are included.
for (int i = 0; i < arrHeaders.Count; i++)
{
var attrName = arrHeaders;
var attrValue = objFolder.GetDetailsOf(folderItem, i);
var attrIdx = i;


// Part 2 of that code that I don't know the purpose of. [ref2234]
attributes.Add(new Tuple<int, string, string>(attrIdx, attrName, attrValue));

if (!string.IsNullOrEmpty(attrValue) && !string.IsNullOrEmpty(attrName))
{
string textLine = string.Format("{0}\t{1}: {2}", i, attrName, attrValue);
Byte[] textFileContent = new UTF8Encoding(true).GetBytes(textLine + "\n");
fs.Write(textFileContent, 0, textFileContent.Length);
}
}
}

}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}

// Setting the Date properties of the .deleted file using the Date properties of the source file.
File.SetCreationTime(@textFileName, sourceFileCreatedDate);
File.SetLastWriteTime(@textFileName, sourceFileModifiedDate);
File.SetLastAccessTime(@textFileName, sourceFileAccessedDate);

This whole code is inside the click event of a button.


Also, I already the solutions stackoverflow(dot)com/questions/31403956/exception-when-using-shell32-to-get-file-extended-properties but I wasn't able to make it work.

Continue reading...
 
Back
Top