|
-
Aug 5th, 2003, 07:08 AM
#1
Thread Starter
Hyperactive Member
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
-
Aug 5th, 2003, 07:34 AM
#2
Retired VBF Adm1nistrator
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]
-
Aug 5th, 2003, 07:39 AM
#3
Thread Starter
Hyperactive Member
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)
-
Aug 5th, 2003, 07:53 AM
#4
Retired VBF Adm1nistrator
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]
-
Aug 5th, 2003, 07:56 AM
#5
Thread Starter
Hyperactive Member
plenderj:
Thanks for your help and time. I appreciated.
Andy
-
Aug 5th, 2003, 07:57 AM
#6
Thread Starter
Hyperactive Member
plenderj:
Can you take a look at my other questions?
Thanks
Andy
-
Aug 5th, 2003, 08:03 AM
#7
a working example of readonly is this ...
VB Code:
[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
[color=blue]Dim[/color] sReader [color=blue]As New[/color] StreamWriter([color=blue]New[/color] FileStream("C:\somestuff.txt", FileMode.OpenOrCreate, FileAccess.Write))
[color=blue]With[/color] sReader
.WriteLine("some stuff!")
.Close()
File.SetAttributes("C:\somestuff.txt", FileAttributes.ReadOnly)
[color=blue]End With[/color]
[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]
-
Aug 5th, 2003, 08:07 AM
#8
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|