jcrcarmo Posted December 15, 2005 Posted December 15, 2005 (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 December 15, 2005 by PlausiblyDamp Quote
Administrators PlausiblyDamp Posted December 15, 2005 Administrators Posted December 15, 2005 (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 January 6, 2006 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jcrcarmo Posted December 16, 2005 Author Posted December 16, 2005 (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 December 17, 2005 by jcrcarmo Quote
Cags Posted December 16, 2005 Posted December 16, 2005 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 Quote Anybody looking for a graduate programmer (Midlands, England)?
Administrators PlausiblyDamp Posted December 16, 2005 Administrators Posted December 16, 2005 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); } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jcrcarmo Posted December 17, 2005 Author Posted December 17, 2005 Dear PlausiblyDamp, The last code you've posted works great! Thanks a lot for your time and help. It was very educational. Bye for now, JC :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.