Hello.
When i create a directory the 'Read only' attribute is always on :(
How can i change this so i can write to my created dir?
Regards Me
Printable View
Hello.
When i create a directory the 'Read only' attribute is always on :(
How can i change this so i can write to my created dir?
Regards Me
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?
As ask't here is the code
Code:'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()
Try to use only the create directory and leave the attributes alone...
The application it's installed where? Do you have permissions to write there directly by the explorer? What windows version are you using?
Yes try like Mickey suggested.
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
still the same result.
I run the program on my desktop.
Mickey: i used your code but folder don't get created
New Code:
what i want todo is i want create a folder and move all the pdf's to it.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
With my code(first post)The folders get created but the files dont get moved.
Same prob on xp,vista,windows 7
If you go directly to the folder and create the folder manually and try to copy files to the folder, do you have any problem?
It looks like a permission issue...
That's because you have to authorise that 'move' as administrator as correctly guessed by Mickey.Quote:
With my code(first post)The folders get created but the files dont get moved.
Same prob on xp,vista,windows 7
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.
Ok i will try suggestions when i get back
Hello
I'm Back :)
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:
Regards MeCode:'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
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
Maybe the problem it's because the FileInfo class...
Try this instead:
vb.net Code:
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
In your code you access the files?
Or is just here when you trying to "move" them?
i just try to move it after the file have been emailed
Can you post here the code where do you email de file!?
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.
I found this link for you:
http://stackoverflow.com/questions/1...m-files-folder
@Pradeep1210
I think the problem it's other...
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...
Teckniel, did you try what I suggested in post 9?
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?
Yes i'm in admin mode and tested it on other pc's but same error
I will make a app that only mails and move and attach it to forum
Regards Me
Sample Project Attached
Regards Me
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.
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) Dim tempFile As String Dim filesTemp As New List(Of String) For Each file As String In aryFi tempFile = IO.Path.Combine(IO.Path.GetTempPath, IO.Path.GetFileName(file)) IO.File.Copy(file, tempFile, True) filesTemp.Add(tempFile) Next Return filesTemp End Function
The in your main function in the first lines replace your code by this one:
VB.NET Code:
Dim listFiles As List(Of String) listFiles = createCopys() 'Attachin pdf to email For Each fi As String In listFiles TestEmail.Attachments.Add(New System.Net.Mail.Attachment(fi)) Next ...
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")
Good luck =)
*EDIT* - Link with Info
If you put a breakpoint in EmailServer_SendCompleted and step through line by line, do you face the error?
I got the same error here, when sending the files async and sync, after disposing the objects, after cleaning the attachments.
So the only solution that i see to move the files immediately after the send it's this way... or something similar.
Do one of these two things:
1. Pass a reference of the current email message to the EmailServer_SendCompleted event so that we can do something there.
OR,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
2. Send email synchronously rather than asynchronous. And after email is sent, close the attachment.
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.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
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.
Thanks for the help guys
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. :)
Oke.
Thanks all for the help!!
-Solved-
Oke.
Thanks all for the help!!
-Solved-