Results 1 to 33 of 33

Thread: -=Solved=- Create Directory Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    -=Solved=- Create Directory Problem

    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
    Last edited by Teckniel; Dec 1st, 2009 at 07:20 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Create Directory Problem

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Directory Problem

    Quote Originally Posted by Teckniel View Post
    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
    Show the code that you are using...
    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    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()

  5. #5
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Create Directory Problem

    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?

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Directory Problem

    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
    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    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

  8. #8
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Create Directory Problem

    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...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Directory Problem

    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

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    Ok i will try suggestions when i get back

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    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:

    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

  12. #12
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Create Directory Problem

    Maybe the problem it's because the FileInfo class...

    Try this instead:
    vb.net Code:
    1. Dim path As String = IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "pdf\nietverzonden")
    2.         Dim filesToMove As String() = IO.Directory.GetFiles(path, "*.pdf", IO.SearchOption.AllDirectories) 'or IO.SearchOption.TopDirectoryOnly
    3.         For Each file As String In filesToMove
    4.             Dim filename As String = IO.Path.GetFileName(file)
    5.             Dim newFile As String = IO.Path.Combine(DirConstr, filename)
    6.             IO.File.Move(file, newFile)
    7.         Next

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    Quote Originally Posted by mickey_pt View Post
    Maybe the problem it's because the FileInfo class...

    Try this instead:
    vb.net Code:
    1. Dim path As String = IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "pdf\nietverzonden")
    2.         Dim filesToMove As String() = IO.Directory.GetFiles(path, "*.pdf", IO.SearchOption.AllDirectories) 'or IO.SearchOption.TopDirectoryOnly
    3.         For Each file As String In filesToMove
    4.             Dim filename As String = IO.Path.GetFileName(file)
    5.             Dim newFile As String = IO.Path.Combine(DirConstr, filename)
    6.             IO.File.Move(file, newFile)
    7.         Next
    Nop dos not work i still get this error: The process cannot access the file because it is being used by another process.

    Regards Me

  14. #14
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Create Directory Problem

    In your code you access the files?

    Or is just here when you trying to "move" them?

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    i just try to move it after the file have been emailed

  16. #16
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Create Directory Problem

    Can you post here the code where do you email de file!?

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  17. #17
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Create Directory Problem

    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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  18. #18
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Create Directory Problem

    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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  19. #19
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Create Directory Problem

    @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...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  20. #20
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Create Directory Problem

    Teckniel, did you try what I suggested in post 9?
    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

  21. #21
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Create Directory Problem

    Quote Originally Posted by mickey_pt View Post
    @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...
    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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    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

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    Sample Project Attached

    Regards Me
    Attached Files Attached Files

  24. #24
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Create Directory Problem

    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

  25. #25
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Create Directory Problem

    Just an example, create a function to create the copy's of the files that you will send and move.


    VB.NET Code:
    1. Private Function createCopys() As List(Of String)
    2.         Dim aryFi As String() = IO.Directory.GetFiles(System.AppDomain.CurrentDomain.BaseDirectory() & "pdf\nietverzonden\", "*.pdf", IO.SearchOption.AllDirectories)
    3.         Dim tempFile As String
    4.         Dim filesTemp As New List(Of String)
    5.         For Each file As String In aryFi
    6.             tempFile = IO.Path.Combine(IO.Path.GetTempPath, IO.Path.GetFileName(file))
    7.             IO.File.Copy(file, tempFile, True)
    8.             filesTemp.Add(tempFile)
    9.         Next
    10.         Return filesTemp
    11.     End Function

    The in your main function in the first lines replace your code by this one:
    VB.NET Code:
    1. Dim listFiles As List(Of String)
    2.             listFiles = createCopys()
    3.  
    4.             'Attachin pdf to email
    5.             For Each fi As String In listFiles
    6.                 TestEmail.Attachments.Add(New System.Net.Mail.Attachment(fi))
    7.             Next
    8. ...

    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:
    1. 'Gmail Sample
    2. EmailServer.Port = 587
    3.             EmailServer.EnableSsl = True
    4.             EmailServer.Credentials = New System.Net.NetworkCredential("[email protected]", "yourpassword")

    Good luck =)

    *EDIT* - Link with Info
    Last edited by mickey_pt; Nov 26th, 2009 at 06:47 PM. Reason: Info

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  26. #26
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Create Directory Problem

    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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  27. #27
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Create Directory Problem

    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.

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  28. #28
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Create Directory Problem

    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.

    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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    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.

  30. #30

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    Thanks for the help guys

  31. #31
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Create Directory Problem

    Quote Originally Posted by Teckniel View Post
    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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    Oke.

    Thanks all for the help!!

    -Solved-

  33. #33

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Belgium
    Posts
    167

    Re: Create Directory Problem

    Oke.

    Thanks all for the help!!

    -Solved-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width