Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hello folks,

 

The transition from VB.Net 2005 to C# 2005 is not always a smooth proccess, even though the synthax is almost identical in some cases. The code below exemplifies a MS Access Database Backup routine I'm trying to tranlsate to C# 2005:

 

---------------------------------------------------------------------------------------------------------

   Private Sub BackupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackupToolStripMenuItem.Click

       Dim Time As String = Format(Now, " dddd dd-MM-yyyy  à's' HH'h' mm'min' ss'seg'")
       Dim FileName As String = "myDB " & Time & ".mdb"
       Dim SavePath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\myDB\Backups\"

       If Not My.Computer.FileSystem.DirectoryExists(SavePath) Then
           My.Computer.FileSystem.CreateDirectory(SavePath)
           My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath & "\myDB.mdb", SavePath & FileName, True)
           MsgBox("Backup performed succesfully.   ", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Ready")
       Else
           My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath & "\myDB.mdb", SavePath & FileName, True)
           MsgBox("Backup performed succesfully.   ", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Ready")
       End If

   End Sub

 

---------------------------------------------------------------------------------------------------------

 

Can anyone help me out on this one? Thanks in advance!

 

Best regards,

 

JC :)

Edited by PlausiblyDamp
  • Administrators
Posted (edited)

Probably the closest literal conversion is

private void BackupToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
string Time = DateTime.Now.ToString(" dddd dd-MM-yyyy  à's' HH'h' mm'min' ss'seg'");
string FileName = "myDB " + Time + ".mdb";
string SavePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "myDBBackups";

if (!System.IO.Directory.Exists(SavePath))
   {
   System.IO.Directory.CreateDirectory(SavePath);
   System.IO.File.Copy(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\myDB.mdb", SavePath + FileName, true);

   MessageBox.Show("Backup performed succesfully.   ", "Ready", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
else
    {
   System.IO.File.Copy(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\myDB.mdb", SavePath + FileName, true);
   MessageBox.Show("Backup performed succesfully.   ", "Ready", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }
}

 

personally I would tend to avoid the VB specific functionality like MsgBox as it does make conversion / working with both languages easier.

 

The following is the same but slightly tweaked to use more of the .Net functionality (mainly Path.Combine) to remove a chance of error when paths may or may not have leading / trailing slashes.

 

EDIT: ignore the following code, fixed code is here

private void BackupToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
string Time = DateTime.Now.ToString(" dddd dd-MM-yyyy  à's' HH'h' mm'min' ss'seg'");
string FileName = "myDB " + Time + ".mdb";
string SavePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myDBBackups");

if (!System.IO.Directory.Exists(SavePath))
   System.IO.Directory.CreateDirectory(SavePath);
           
System.IO.File.Copy(System.IO.Path.GetDirectoryName(
               System.IO.Path.Combine(Application.ExecutablePath), "myDB.mdb"), SavePath + FileName, true);

MessageBox.Show("Backup performed succesfully.   ", "Ready", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

Dear PlausiblyDamp,

 

The tweaked code didn't work, but your literal translation of the code I posted worked out beautifuly.

 

By the way, I will be more careful when I create new threads by paying attention to the correct subforum they are supposed to go to.

 

Thank you very very much for your kind help and have a great day!

 

Best regards,

 

JC :)

Edited by jcrcarmo
Posted

I could be wrong, but I think what PlausiblyDamp meant was...

System.IO.File.Copy(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "myDB.mdb"), SavePath + FileName, true);

but I see no reason this can't be shortend to

System.IO.File.Copy(System.IO.Path.Combine(Application.StartupPath, "myDB.mdb"), SavePath + FileName, true);

Also its worth noting that my VisualStudio doesn't recognise the following, perhaps its not part of the 1.1 framework?

Environment.SpecialFolder.MyDocuments

Anybody looking for a graduate programmer (Midlands, England)?
  • Administrators
Posted

Ignore the code above, try the following - you just need to shove a using System.IO at the top of the source file.

private void BackupToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
string Time = DateTime.Now.ToString(" dddd dd-MM-yyyy  à's' HH'h' mm'min' ss'seg'");
string FileName = "myDB " + Time + ".mdb";
string SavePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myDBBackups");

if (!Directory.Exists(SavePath))
   Directory.CreateDirectory(SavePath);

File.Copy(Path.Combine(Application.StartupPath, "myDB.mdb"), Path.Combine(SavePath, FileName), true);

MessageBox.Show("Backup performed succesfully.   ", "Ready", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...