I don't think it is. I just created a new folder using IO.Directory.CreateDirectory and then checked its attributes and it wasn't read-only. The read-only attribute was set to indeterminate (the box was filled rather than checked) and it said "(Only applies to files in the folder)". I was then able to open the folder and create a new text file in it that was not read-only. Have you tried to create a file in your new folder? Are you able to?
'create folder and move file
Dim DirConstr As String
DirConstr = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\verzonden\" & Date.Now.Year
If (Not System.IO.Directory.Exists(DirConstr)) Then
System.IO.Directory.CreateDirectory(DirConstr).Attributes = IO.FileAttributes.Normal
IO.File.SetAttributes(DirConstr, IO.FileAttributes.Archive)
SetAttr(DirConstr, vbNormal)
'System.IO.DirectoryInfo² = System.IO.Directory.Attributes And Not IO.FileAttributes.ReadOnly
End If
DirConstr = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\verzonden\" & Date.Now.Year & "\" & Date.Today.Month
If (Not System.IO.Directory.Exists(DirConstr)) Then
System.IO.Directory.CreateDirectory(DirConstr).Attributes = IO.FileAttributes.Normal
End If
DirConstr = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\verzonden\" & Date.Now.Year & "\" & Date.Today.Month & "\" & Date.Today.Day
If (Not System.IO.Directory.Exists(DirConstr)) Then
Dim i As New IO.DirectoryInfo(DirConstr)
System.IO.Directory.CreateDirectory(DirConstr)
i.Attributes = IO.FileAttributes.Normal
End If
'move pdf
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden")
Dim aryFi As IO.FileInfo() = di.GetFiles("*.pdf")
Dim fi As IO.FileInfo
For Each fi In aryFi
di.Attributes = IO.FileAttributes.Directory
System.IO.File.Move(fi.FullName, DirConstr)
Next
ListBox1.Items.Clear()
Here is a very stripped down code. Give it a try and tell me if the directry is created as you wanted?
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Try
Directory.CreateDirectory (DirConstr)
Catch
End Try
MsgBox ("Done")
End Sub
Last edited by Siddharth Rout; Nov 21st, 2009 at 06:56 AM.
Reason: typo
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
still the same result.
I run the program on my desktop.
Mickey: i used your code but folder don't get created
New Code:
Code:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim DirConstr As String
DirConstr = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\verzonden\" & Date.Now.Year
Directory.CreateDirectory(DirConstr)
'move pdf
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden")
Dim aryFi As IO.FileInfo() = di.GetFiles("*.pdf")
Dim fi As IO.FileInfo
For Each fi In aryFi
System.IO.File.Move(fi.FullName, DirConstr)
Next
Catch
End Try
MsgBox("Done")
End Sub
End Class
what i want todo is i want create a folder and move all the pdf's to it.
With my code(first post)The folders get created but the files dont get moved.
Same prob on xp,vista,windows 7
With my code(first post)The folders get created but the files dont get moved.
Same prob on xp,vista,windows 7
That's because you have to authorise that 'move' as administrator as correctly guessed by Mickey.
I was under the impression that you meant that the directory was created with it's read only attribute on...
Do you have Administrator Rights? If yes then reboot and Log-on as administrator. Go to folders and secure Full Control as administrator. Also gain Ownership. (right click > properties > Security > Advanced). Then try copying or moving files as administrator with Full Control and Ownership.
You may have to reboot after gaining Full Control and Ownership before trying to copy folders/files.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
The problem i think is not with creating a directory. but moving the file from a to b
I get this error: The process cannot access the file because it is being used by another process.
This is the move code:
Code:
'move pdf
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden")
Dim aryFi As IO.FileInfo() = di.GetFiles("*.pdf")
Dim fi As IO.FileInfo
For Each fi In aryFi
' di.Attributes = IO.FileAttributes.Directory
System.IO.File.Move(fi.FullName, DirConstr)
Next
Regards Me
Ps: I can create copy etc to the dir manually, and the file i try to copy is not in use as far as i now
Last edited by Teckniel; Nov 25th, 2009 at 05:23 PM.
Reason: Added Somthing
From Windows XP and upwards, the rules for creating files has got strict. you can no longer freely create files in the Program Files directory. You should instead create your files in the user's personal folders which is in Documents & Settings\$user$\Application Data.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
He says that can copy the files to the folder manually, and when try to do that by code, it throws the message that the file it's in use, so the problem it's in the code, i think he after sending the email doesn't release the file...
Rate People That Helped You Mark Thread Resolved When Resolved
He says that can copy the files to the folder manually, and when try to do that by code, it throws the message that the file it's in use, so the problem it's in the code, i think he after sending the email doesn't release the file...
It could still be a file access problem rather than something in his code (though I don't deny this).
Windows locks the the Program Files folder in special ways. And if it doesn't allow moving the files, the error might be same.
@Teckniel
Are you sure you are logged in as Administrator, or the user you are logged in as, has Administrator rights?
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
You're sending the email and attaching the files, it takes some time to the files get released...
You can make a workaround, just before the email create a copy of the files in the temp folder and attach those and send, and after sending you can move the real ones.
Rate People That Helped You Mark Thread Resolved When Resolved
Just an example, create a function to create the copy's of the files that you will send and move.
VB.NET Code:
Private Function createCopys() As List(Of String)
Dim aryFi As String() = IO.Directory.GetFiles(System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\", "*.pdf", IO.SearchOption.AllDirectories)
The rest of code you can use the same, i don't know if you need to configure your SMTP and the credentials to send the emails, but if you need you can do something like this:
VB.NET Code:
'Gmail Sample
EmailServer.Port = 587
EmailServer.EnableSsl = True
EmailServer.Credentials = New System.Net.NetworkCredential("[email protected]", "yourpassword")
If you put a breakpoint in EmailServer_SendCompleted and step through line by line, do you face the error?
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
1. Pass a reference of the current email message to the EmailServer_SendCompleted event so that we can do something there.
Code:
Private Sub SendEmail()
Try
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\")
Dim aryFi As IO.FileInfo() = di.GetFiles("*.pdf")
Dim fi As IO.FileInfo
Dim TestEmail As New System.Net.Mail.MailMessage(TextBox2.Text, TextBox3.Text, "Pdf Mailer" + Date.Today, "")
'Attachin pdf to email
For Each fi In aryFi
Dim i As String
i = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\" + fi.Name
TestEmail.Attachments.Add(New System.Net.Mail.Attachment(i))
Next
EmailServer = New System.Net.Mail.SmtpClient(TextBox1.Text)
EmailServer.SendAsync(TestEmail, TestEmail)
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error Sending Email", MessageBoxButtons.OK, MessageBoxIcon.Hand)
End Try
End Sub
Private Sub EmailServer_SendCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles EmailServer.SendCompleted
Try
If e.Error Is Nothing Then
If e.Cancelled Then
MessageBox.Show("Test Email Cancelled", "Email Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
CType(e.UserState, System.Net.Mail.MailMessage).Attachments(0).ContentStream.Close()
'create folder and move file
Dim DirConstr As String
DirConstr = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\verzonden\" & Date.Now.Year
If (Not System.IO.Directory.Exists(DirConstr)) Then
System.IO.Directory.CreateDirectory(DirConstr)
End If
'move pdf
Dim path As String = IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "pdf\nietverzonden")
Dim filesToMove As String() = IO.Directory.GetFiles(path, "*.pdf", IO.SearchOption.AllDirectories) 'or IO.SearchOption.TopDirectoryOnly
For Each file As String In filesToMove
Dim filename As String = IO.Path.GetFileName(file)
Dim newFile As String = IO.Path.Combine(DirConstr, filename)
IO.File.Move(file, newFile)
Next
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
OR,
2. Send email synchronously rather than asynchronous. And after email is sent, close the attachment.
Code:
Private Sub SendEmail()
Try
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\")
Dim aryFi As IO.FileInfo() = di.GetFiles("*.pdf")
Dim fi As IO.FileInfo
Dim TestEmail As New System.Net.Mail.MailMessage(TextBox2.Text, TextBox3.Text, "Pdf Mailer" + Date.Today, "")
'Attachin pdf to email
For Each fi In aryFi
Dim i As String
i = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\" + fi.Name
TestEmail.Attachments.Add(New System.Net.Mail.Attachment(i))
Next
EmailServer = New System.Net.Mail.SmtpClient(TextBox1.Text)
EmailServer.Send(TestEmail)
TestEmail.Attachments(0).ContentStream.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error Sending Email", MessageBoxButtons.OK, MessageBoxIcon.Hand)
End Try
End Sub
Private Sub EmailServer_SendCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles EmailServer.SendCompleted
Try
If e.Error Is Nothing Then
If e.Cancelled Then
MessageBox.Show("Test Email Cancelled", "Email Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
'create folder and move file
Dim DirConstr As String
DirConstr = System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\verzonden\" & Date.Now.Year
If (Not System.IO.Directory.Exists(DirConstr)) Then
System.IO.Directory.CreateDirectory(DirConstr)
End If
'move pdf
Dim path As String = IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "pdf\nietverzonden")
Dim filesToMove As String() = IO.Directory.GetFiles(path, "*.pdf", IO.SearchOption.AllDirectories) 'or IO.SearchOption.TopDirectoryOnly
For Each file As String In filesToMove
Dim filename As String = IO.Path.GetFileName(file)
Dim newFile As String = IO.Path.Combine(DirConstr, filename)
IO.File.Move(file, newFile)
Next
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
What I showed in the above examples is just releasing one attachment. But since you might have multiple attachments, you should loop through all the attachments and close the stream.
Last edited by Pradeep1210; Nov 27th, 2009 at 03:09 PM.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
I will give you guys a update later tonight, first i'm off to the movie (2012)
Thanks for are the help so far!!!
Pradeep: could i send you a pm, i need your help with something.
You can happily send me PM for anything. But I'll discourage anyone sending PM for coding questions as is not conducive to the general learning of the forum members.
If you face any problems with the code I posted above, just post back your problem here and I'll be glad to help in whatever way I can.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering