After spending the last 4 years writing in VB.Net I have decided to bite the bullet and all code written at home will be written in C#. Given time I think I might try to move into a C# position if things go smoothly.
Anyway in the past I did some training in Java so the syntax is reasonably familiar and VB and C# are closer than ever now.
So the first question is quite simple. It is regarding the use of @ in C#. If I put the line:
It would fail, this is to do with the backspace. It is simple to fix it:
What I am not sure of is what the @ is actually doing here, can someone explain.
The next question regards some other code I wrote using a fileSystemWatcher that is used to delete files/folders etc. The first thing is the codes does everything it is supposed to do, so I am happy with it in that respect. I just wondered if it could be cleaner than the way I am doing it.
The problem I came across was breaking out of the recursive function. I VB I would have probably used exit sub etc. In C# I used return; instead but if you look at the code you will see I had to do quite a lot to unwind the recursion. Anyway have a laugh!
I am preparing myself for some criticism but if it improves me I can take it.
Cheers, Dave.
Anyway in the past I did some training in Java so the syntax is reasonably familiar and VB and C# are closer than ever now.
So the first question is quite simple. It is regarding the use of @ in C#. If I put the line:
Code:
folderToDelete = folderToDelete.Substring(0, folderToDelete.LastIndexOf("\"));
Code:
folderToDelete = folderToDelete.Substring(0, folderToDelete.LastIndexOf(@"\"));
The next question regards some other code I wrote using a fileSystemWatcher that is used to delete files/folders etc. The first thing is the codes does everything it is supposed to do, so I am happy with it in that respect. I just wondered if it could be cleaner than the way I am doing it.
The problem I came across was breaking out of the recursive function. I VB I would have probably used exit sub etc. In C# I used return; instead but if you look at the code you will see I had to do quite a lot to unwind the recursion. Anyway have a laugh!
Code:
private void fswSource_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
if (Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["AllowDelete"]))
{
if (e.Name.Contains(@"."))
{
//This is a file we are deleting.
if (File.Exists(_DestinationFolder + e.Name))
{
try
{
File.Delete(_DestinationFolder + e.Name);
}
catch (Exception)
{
//Consume exception
}
}
}
else
{
//This is a folder we are deleting a folder.
if (Directory.Exists(_DestinationFolder + e.Name))
{
try
{
//Make reference of rootfolder, to compare to later.
_RootFolder = _DestinationFolder + e.Name;
//Call sub to delete folders/files etc.
Delete(_DestinationFolder + e.Name);
//Reset flag.
_stopDeleting = false;
}
catch (Exception)
{
//Consume exception.
}
}
}
}
}
/// Deletes file/s / folder/s using recursion.
/// </summary>
private void Delete(string folderToDelete)
{
while (folderToDelete != _RootFolder && _stopDeleting == false)
{
//The code in this loop applies to all child folders of the folder we are deleting.
//Attempt to delete any empty folders.
foreach (string d in Directory.GetDirectories(folderToDelete))
{
try
{
Directory.Delete(d);
}
catch (Exception)
{
//Consume exception.
}
}
//Attempt to delete the current folder in the case of it being empty.
try
{
Directory.Delete(folderToDelete);
//Move up to parent folder when no more files are left to delete.
folderToDelete = folderToDelete.Substring(0, folderToDelete.LastIndexOf(@"\"));
if (_stopDeleting == false)
{
Delete(folderToDelete);
if (_stopDeleting == true)
{
return;
}
}
else
{
return;
}
}
catch (Exception)
{
//Consume exception.
}
//Recursively move through child folders.
foreach (string d in Directory.GetDirectories(folderToDelete))
{
if (_stopDeleting == false)
{
Delete(d);
if (_stopDeleting == true)
{
return;
}
}
else
{
return;
}
}
//Delete any files in folder.
foreach (string f in Directory.GetFiles(folderToDelete))
{
try
{
File.Delete(f);
}
catch (Exception)
{
//Consume exception.
}
}
try
{
//Move up to parent folder when no more files are left to delete.
folderToDelete = folderToDelete.Substring(0, folderToDelete.LastIndexOf(@"\"));
if (_stopDeleting == false)
{
Delete(folderToDelete);
if (_stopDeleting == true)
{
return;
}
}
else
{
return;
}
}
catch (Exception)
{
//Consume exception.
}
}
//This code applies to the parent folder.
//Attempt to go into any child folders.
foreach (string d in Directory.GetDirectories(folderToDelete))
{
try
{
Delete(d);
}
catch (Exception)
{
//Consume exception.
}
}
//Delete any files in folder.
foreach (string f in Directory.GetFiles(folderToDelete))
{
try
{
File.Delete(f);
}
catch (Exception)
{
//Consume exception.
}
}
//Attempt to delete the current folder in the case of it being empty.
try
{
Directory.Delete(folderToDelete);
}
catch (Exception)
{
//Consume exception.
}
_stopDeleting = true;
return;
}
I am preparing myself for some criticism but if it improves me I can take it.
Cheers, Dave.