Question Saving Files issue

QADUDE

Member
Joined
Dec 30, 2009
Messages
12
Programming Experience
3-5
The following code creates folders named "Index" and inside index a folder named "A" and works ok

Dim Path As String = "C:\Users\Dave's\Documents\CardIndex"

My.Computer.FileSystem.CreateDirectory(Path)
Dim A As String = "\A"
SavePath = String.Concat(Path + A)
My.Computer.FileSystem.CreateDirectory(SavePath)

I then want to save text from richTextBox the following code

RichTextBox1.SelectAll()
RTBA = RichTextBox1.SelectedText
SavePath = "C:\Users\Dave's\Documents\CardIndex\A"
RichTextBox1.SaveFile(SavePath)

At this point I get this error

System.UnauthorizedAccessException was unhandled
Message="Access to the path 'C:\Users\Dave's\Documents\CardIndex\A' is denied."

What do I need to do to remedy this?
 
You need to use a streamwriter and create a file. There is no mention in your code to where you are trying to save the richtextbox text to.
Also rather than hardcode your documents folder, use the environment specialfolders so anyone who runs it can create the directories.

VB.NET:
        'Path variable
        Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\CardIndex\A"

        'Create directories
        IO.Directory.CreateDirectory(path)

        'Create File From Rich Text Box
        Dim sWriter As New IO.StreamWriter(path & "\text.txt")

        'Write text from richtextbox
        sWriter.Write(RichTextBox1.Text)

        'close writer / save file
        sWriter.Close()
 
SavePath = "C:\Users\Dave's\Documents\CardIndex\A"
RichTextBox1.SaveFile(SavePath)
You gave it a folder path, but forgot to specify the file name.
 
You need to use a streamwriter and create a file. There is no mention in your code to where you are trying to save the richtextbox text to.
Also rather than hardcode your documents folder, use the environment specialfolders so anyone who runs it can create the directories.

VB.NET:
        'Path variable
        Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\CardIndex\A"

        'Create directories
        IO.Directory.CreateDirectory(path)

        'Create File From Rich Text Box
        Dim sWriter As New IO.StreamWriter(path & "\text.txt")

        'Write text from richtextbox
        sWriter.Write(RichTextBox1.Text)



        'close writer / save file
        sWriter.Close()

Thanks Lotok worked a treat
PS Like your blog
Best QADUDE
 
Back
Top