Results 1 to 8 of 8

Thread: How to modify this code to make my StreamWriter file READ ONLY

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    How to modify this code to make my StreamWriter file READ ONLY

    Does anyone have a clue as to how to modify the code below so the file is saved to the Hard Disk with the “Read Only” attribute.


    Code:
     ' This Sub uses a StreamWriter object to create a .txt file 
        'With Text coming out of the Serial port. The name of the file
        'is composed of the Machine Serial#, Date and Time String (time ‘includes seconds)
    
        Sub SerialCopyWriter()
    
            Dim myStreamWriter As StreamWriter
            Dim myPath As String = "..\Readings Backup\"
            Dim myMachine As Integer = 3035
            Dim myName As String = myMachine & "+" & Now.Month & "_" & Now.Day & "_" _
            & Now.Year.ToString & "+" & Now.Hour & "_" & Now.Minute & "_" & Now.Second & ".txt"
            Dim myFileName As String = myPath + myName
    
            'Resulting file name 3035+8_2_2003+7_38_41
    
            Try
                ' Create the StreamWriter
                myStreamWriter = File.CreateText(myFileName)
                myStreamWriter.Write(txtFileText.Text)
                myStreamWriter.Flush()
    
            Catch exc As Exception
                ' Show the error to the user.
                MsgBox("A copy of the serial port Data-Reading for this Coffee Machine could not be Created:" + _
                    vbCrLf + vbCrLf + "Exception: " + exc.Message)
            Finally
    
                If Not myStreamWriter Is Nothing Then
                    myStreamWriter.Close()
                End If
            End Try
        End Sub

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Not that I have any VB.Net experience, but I would imagine you would then have to manually change the file permissions yourself - i.e. not have the StreamWriter do it ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376
    plenderj

    Thanks for your quick reply, I cannot do that, there are too many files involved to do it manualy.

    I will try this instead:

    File.SetAttributes(myFileName, FileAttributes.ReadOnly)

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Sorry yeah that's what I meant, that you'd have to use another line or two of code to set the permissions on the file after you've finished writing, as opposed to have the permissions set while writing.

    ... which if you think about it you wouldn't be able to do anyway ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376
    plenderj:


    Thanks for your help and time. I appreciated.

    Andy

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376
    plenderj:

    Can you take a look at my other questions?

    Thanks

    Andy

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    a working example of readonly is this ...
    VB Code:
    1. [color=blue]Private Sub[/color] Button4_Click([color=blue]ByVal[/color] sender [color=blue]As[/color] System.Object, [color=blue]ByVal[/color] e [color=blue]As[/color] System.EventArgs) [color=blue]Handles[/color] Button4.Click
    2.         [color=blue]Dim[/color] sReader [color=blue]As New[/color] StreamWriter([color=blue]New[/color] FileStream("C:\somestuff.txt", FileMode.OpenOrCreate, FileAccess.Write))
    3.         [color=blue]With[/color] sReader
    4.             .WriteLine("some stuff!")
    5.             .Close()
    6.             File.SetAttributes("C:\somestuff.txt", FileAttributes.ReadOnly)
    7.         [color=blue]End With[/color]
    8.     [color=blue]End Sub[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376
    dynamic_sysop:

    Thanks pal, God bless your twins.

    Andy

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