I Have a folder in this path
C:\Users\XXX\Desktop\Original\XXX\bin\Debug\Backup
And when I save my project with "XXX" name the same time I need to change the Backup Folder using that save filedialog name and it shouldn't overwrite it.
Can anyone suggest me how to do this:
Here is the code how I am doing it and it didn't work for me:
Private Sub SaveProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveProject.Click
Using sfdlg As New Windows.Forms.SaveFileDialog
sfdlg.OverwritePrompt = True
sfdlg.InitialDirectory = "C:\"
sfdlg.FileName = "Untitled"
sfdlg.DefaultExt = "amk"
sfdlg.Filter = "AquaMark Project|*.amk"
If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim SaveData As New gCanvasData
With SaveData
frmDisplay.GCanvas1.UnselectCurrentAnotate()
.gAnnotates = frmDisplay.GCanvas1.gAnnotates
.Image = frmDisplay.GCanvas1.Image
End With
Using objStreamWriter As New StreamWriter(sfdlg.FileName)
Dim x As New XmlSerializer(GetType(gCanvasData))
x.Serialize(objStreamWriter, SaveData)
objStreamWriter.Close()
End Using
End If
sfdlg.Dispose()
System.IO.Path.GetFileNameWithoutExtension(sfdlg.FileName)
IO.Directory.Move(Application.StartupPath + "\Backup\", Application.StartupPath + "\Backup\" & System.IO.Path.GetFileNameWithoutExtension(sfdlg.FileName))
End Using
End Sub
But can anyone clearly mention me how to do it?
Based on the error, you most likely won't be able to save the file and change the backup folder name through the same SaveFileDialog.
Break it into two steps:
Save the file through the SaveFileDialog. Be sure to capture the filename from SaveFileDialog so you can use it in step 2, as it may be out of scope after you close the SaveFileDialog window.
Rename the backup folder using the command above, but after you've closed the SaveFileDialog so the handle is released.
EDIT
As I noted in step 1, you need to save the filename somewhere so you can use it in step 2.
Private Sub SaveProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveProject.Click
' Set up a variable to hold the filenam
Dim fileName As String
Using sfdlg As New Windows.Forms.SaveFileDialog
sfdlg.OverwritePrompt = True
sfdlg.InitialDirectory = "C:\"
sfdlg.FileName = "Untitled"
sfdlg.DefaultExt = "amk"
sfdlg.Filter = "AquaMark Project|*.amk"
If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim SaveData As New gCanvasData
' Store the filename from the SaveFileDialog
fileName = sfdlg.FileName
With SaveData
frmDisplay.GCanvas1.UnselectCurrentAnotate()
.gAnnotates = frmDisplay.GCanvas1.gAnnotates
.Image = frmDisplay.GCanvas1.Image
End With
Using objStreamWriter As New StreamWriter(sfdlg.FileName)
Dim x As New XmlSerializer(GetType(gCanvasData))
x.Serialize(objStreamWriter, SaveData)
objStreamWriter.Close()
End Using
End If
'Calling Dispose is redundant since sfdlg was in a Using block
'sfdlg.Dispose()
' You can't use sfdlg.FileName here as the object is out of scope
'System.IO.Path.GetFileNameWithoutExtension(sfdlg.FileName)
'IO.Directory.Move(Application.StartupPath + "\Backup\", Application.StartupPath + "\Backup\" & System.IO.Path.GetFileNameWithoutExtension(sfdlg.FileName))
' Use the value in fileName from above
System.IO.Directory.Move(Application.StartupPath + "\Backup\", Application.StartupPath + "\Backup\" & System.IO.Path.GetFileNameWithoutExtension(fileName))
End Using
End Sub
Related
I want the user to have the option to select multiple files through openfiledialog which I have in my code, but then if the user selects a file from one folder he is then restricted to select another file only from this specific folder. What is the best way to approach to this problem?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim openfiledialog1 As New OpenFileDialog
With openfiledialog1
.Title = "Select your models"
.Filter = "Solidworks Files|*.sldprt;"
.Multiselect = True
End With
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
For Each mfile As String In openfiledialog1.FileNames
'' Add all filenames in a txt file, in a column
Next
End If
End Sub
As far as I know, OpenFileDialog doesn't offer a way to prevent the user from navigating to another directory.
One approach would be to create a class-level variable that holds the value of the recently used directory path. Then, whenever a new file is selected, you check its directory path against the one previously stored. If it matches, continue. If not, break the operation and report to the user.
Here's a complete example:
Private openFileDialog1 As New OpenFileDialog
Private modelsDirectory As String = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
With openFileDialog1
.Title = "Select your models"
.Filter = "Solidworks Files|*.sldprt;"
.Multiselect = True
.FileName = Nothing
' Open the dialog and navigate to the previous direcoty by default.
.InitialDirectory = modelsDirectory
End With
If openFileDialog1.ShowDialog <> DialogResult.OK Then Exit Sub
Dim dirPath As String = IO.Path.GetDirectoryName(openFileDialog1.FileName)
If modelsDirectory IsNot Nothing AndAlso
Not dirPath.Equals(modelsDirectory, StringComparison.OrdinalIgnoreCase) Then
MessageBox.Show("Models must be selected from the following directory:" &
vbNewLine & modelsDirectory, "Restricted Directory")
Exit Sub
End If
' Store the value of the current directory path.
modelsDirectory = dirPath
For Each filePath As String In openFileDialog1.FileNames
' TODO: use the selected files as you see fit.
Next
End Sub
You might want to remove this restriction at some point (e.g., if you clear the list of selected files). You can achieve that by simply setting modelsDirectory to Nothing:
Private Sub ClearFilesList
' TODO: clear the files.
modelsDirectory = Nothing
End Sub
So, basically I'm doing a "save as" button and when the file is been saved, I want the path from the file that is been saved goes to another txt file tottaly different.
Private Sub saveas_Click(sender As Object, e As EventArgs) Handles Saveas.Click
SaveFileDialog1.InitialDirectory = "C:\Users\marce"
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
SaveFileDialog1.FilterIndex = 2
SaveFileDialog1.ShowDialog()
Dim W As New IO.StreamWriter(SaveFileDialog1.FileName)
W.Write(RichTextBox1.Text)
W.Close()
End Sub
So, your problem is writing to a different file as well? Or somehow, returning two different filenames from the SaveFileDialog? If it's the latter, I don't believe this can be done.
If it's the former, you already know how to write to text files, so this answer seems redundant. Still, the following code (which assumes that the "totally different txt file" is named by appending ".tmp" to the original filename) will save the original path to the second file:
Private Sub saveas_Click(sender As Object, e As EventArgs) Handles Saveas.Click
SaveFileDialog1.InitialDirectory = "C:\Users\marce"
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
SaveFileDialog1.FilterIndex = 2
SaveFileDialog1.ShowDialog()
Dim W As New IO.StreamWriter(SaveFileDialog1.FileName)
W.Write(RichTextBox1.Text)
W.Close()
'new code
'get new filename by appending .tmp to the original filename
Dim tmpFilePath As String = SaveFileDialog1.FileName & ".tmp"
IO.File.WriteAllText(tmpFilePath, SaveFileDialog1.FileName)
End Sub
To add onto #Spyros P's answer is, I would store SaveFileDialog1.ShowDialog() into a variable because if you cancel or X out of the Save window it will still continue onto the saving of the file. Possibly do something like this:
Private Sub saveas_Click(sender As Object, e As EventArgs) Handles Saveas.Click
SaveFileDialog1.InitialDirectory = "C:\Users\marce"
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
SaveFileDialog1.FilterIndex = 2
Dim temp = SaveFileDialog1.ShowDialog()
If temp = False Then Return
Dim W As New IO.StreamWriter(SaveFileDialog1.FileName)
W.Write(RichTextBox1.Text)
W.Close()
'new code
'get new filename by appending .tmp to the original filename
Dim tmpFilePath As String = SaveFileDialog1.FileName & ".tmp"
IO.File.WriteAllText(tmpFilePath, SaveFileDialog1.FileName)
End Sub
Over all #Spyros P. is correct, all I changed was I added the variable for the SaveFileDialog1.ShowDialog
Here is my code:
Private CurrentRecipient As String
Private User As String = "Name"
Dim UserFolder As String
Dim FileName As String = Format(Now, "MMMMDDDDYYYYHHNNSS") & ".txt"
Private Sub SendButton_Click(sender As Object, e As EventArgs) Handles
SendButton.Click
If CurrentRecipient = "Edward" Then
UserFolder = "C:\Users\bele\Desktop\Edward'sFolder" & FileName
ElseIf CurrentRecipient = "Criziel" Then
UserFolder = "C:\Users\bele\Desktop\Criziel'sFolder" & FileName
End If
' I received no error but once I have send a message It does not doe anything :(
Try this. Add the following Imports above your Form Class.
Imports System
Imports System.IO
Imports System.Text
You will need to add a TextBox control named txtFolderName to enter the folder names (Edward's or Criziel)for this to work. Enter the name of the folder on the Desktop (Edward's or Criziel) into txtFolderName, Click the button named Button1 and it will create the file inside the correct folder.
I'm guessing this is just testing code ideas, because you should also make the Directory Path a variable that you can enter in a TextBox
Private Sub SaveFile()
Dim UserFolder As String = ""
Dim FileName As String = DateTime.Now.ToString("MMddyyyyHHmmss") & ".txt"
Dim fs As FileStream
UserFolder = "C:\Users\bele\Desktop\" & txtFolderName.Text & "\" & FileName
' Create a file
fs = File.Create(UserFolder)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SaveFile()
End Sub
Good Luck!
Comment out the fs = File.Create(UserFolder) line and add this code just below it.
Using sw As StreamWriter = File.CreateText(UserFolder)
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
End Using
I'm looking to make a program that will open a file and upon saving it, the program will save it into a new folder, leaving the original file unchanged.
However, none of the codes I've tried using have worked.
How do I make a new folder that will go into the FilePath of the loaded file?
How do I then save into that folder?
I tried this and got the error.. "False was not found or could not be accessed"
Private Sub SaveChangesToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveChangesToolStripMenuItem1.Click
newFilePath = FilePath + "\" + newFolderName + "Folder"
Try
Dim Writer As New PackageIO.Writer(newFilePath = orgFilePath + "\" + newFolderName, "Folder 1", PackageIO.Endian.Big)
System.IO.Directory.CreateDirectory(newFilePath)
I tried this code here and it seemed to be the closest to working, but said the file path wasn't valid; I'm assuming because it's including the file they selected, not just the folders involved.
Private Sub SaveChangesToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveChangesToolStripMenuItem1.Click
newFilePath = FilePath + "\" + newFolderName + "Folder"
Try
Dim Writer As New PackageIO.Writer(newFilePath, PackageIO.Endian.Big)
System.IO.Directory.CreateDirectory(newFilePath)
Try something along these lines:
Dim orgFilePath as String
Dim newFolderName as String
Dim newFilePath as String
'you will need to set the path of the original file and give a name for the new folder
newFilePath = orgFilePath + "\" + newFolderName
'creates new directory (folder)
System.IO.Directory.CreateDirectory(newFilePath)
'then put the code to save your file to the newFilePath variable
Hope that helps
This program extracts files from a folder that has been modified today, and after the files are placed into another folder a batch file then deletes the rest of the non-modified files in that source folder.
The last thing my program is supposed to do is copy files from separate folder, and place them back into that source folder.
But my program only extracts the modified files, deletes the rest of the files in that folder, but when I run the program to also copy and place the new files into the source folder it just doesn't do it. Does anyone know why?
Imports System.IO
Public Class frmExtractionator
' Dim txtFiles1 As Control
Dim sourceDirectory As String = "F:\CopierFolderforTestDriveCapstone"
Dim archiveDirectory As String = "F:\FilesExtracted"
Dim originalDirectory As String = "F:\OriginalTestFiles"
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Try
Dim txtFiles = Directory.EnumerateFiles(sourceDirectory)
If (Not System.IO.Directory.Exists(archiveDirectory)) Then
System.IO.Directory.CreateDirectory(archiveDirectory)
End If
For Each currentFileLoc As String In txtFiles
Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
If (IO.File.GetLastWriteTime(currentFileLoc).ToString("MM/dd/yyyy") = DateTime.Now.ToString("MM/dd/yyyy")) Then
MessageBox.Show(currentFileLoc & " moved", "Moved Succesfully")
File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))
End If
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
System.Diagnostics.Process.Start("F:\poop.bat")
Try
Dim txtFiles2 = Directory.EnumerateFiles(originalDirectory)
For Each currentFileLoc2 As String In txtFiles2
Dim fileName = currentFileLoc2.Substring(originalDirectory.Length + 1)
File.Move(currentFileLoc2, Path.Combine(sourceDirectory, fileName))
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
End Sub
End Class
Change
Dim fileName = currentFileLoc2.Substring(originalDirectory.Length + 1)
to
Dim FileName = IO.Path.GetFileName(currentFileLoc2)