Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,183 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,122 people online right now. Registration is fast and FREE... Join Now!




file paths?

2 Pages V  1 2 >  
Reply to this topicStart new topic

file paths?

claireabell
29 Aug, 2008 - 04:30 AM
Post #1

New D.I.C Head
*

Joined: 26 Mar, 2007
Posts: 45



Thanked: 1 times
My Contributions

I'm back again, that was quick..

i have the following code-

CODE


public result as new system.IO.Streamwriter ("D:\simulator\result.text",true)





I now need to change this as when my create the executable the program will be put on different machines and people are going to have to be able to define the location of the file to be created.

I have LIMITED experience of file paths and in the past have only used one once to connect to a database not create a file, i'm not even sure if its a file path I need.

Thanks for the help.
User is offlineProfile CardPM
+Quote Post

modi123_1
RE: File Paths?
29 Aug, 2008 - 04:54 AM
Post #2

D.I.C Addict
Group Icon

Joined: 12 Jun, 2008
Posts: 677



Thanked: 25 times
Dream Kudos: 100
My Contributions
I am going to guess this is a windows form application, right? On your main form plunk down a "folder browser dialog" control out of your toolbox. In there you can set parameters and what not..

In your code all you need to do is call <folder browser dialog control's name>.show and this popups on the control.. the user selects it.. and on close of that control you grab the databath they specified... append your file's name.. and throw that string where "D:\simulator\result.text" is.
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: File Paths?
29 Aug, 2008 - 05:50 AM
Post #3

better than jam.
Group Icon

Joined: 6 Feb, 2008
Posts: 5,837



Thanked: 104 times
Dream Kudos: 2700
Expert In: slobbing.

My Contributions
I work with C# but the two are pretty much the same with .NET, right?

Anyway, why not use a SaveFileDialog?

You could have something like this, it would open up a SaveFileDialog, and then write the contents to the file path that the user chose in the dialog.

Here's an extract from some code that I'm working on now:
csharp
			SaveFileDialog sfn = new SaveFileDialog();
sfn.Title = "Save File As...";
sfn.Filter = "Text File (*.txt)|*.txt"; // the |*.txt specifies the format, The Text File (*.txt) is the display on the menu
sfn.InitialDirectory = System.Environment.CurrentDirectory; // set the initial directory to the path of the executable
if (sfn.ShowDialog() == DialogResult.OK)
{
StreamWriter SW;
SW = File.CreateText (@sfn.FileName);
SW.Write (@code.Text);
SW.Close();
currentlyOpen = @sfn.FileName;
}


Hope this helps smile.gif

EDIT:
Here's a stab at VB (don't mock me, I'm clueless in VB)
vb

Dim sfn As New SaveFileDialog()
sfn.Title = "Save File As..."
sfn.Filter = "Text file (*.txt) | *.txt"
sfn.InitialDirectory = System.Environment.CurrentDirectory
if sfn.ShowDialog() == DialogResult.OK ' not sure if there's a keyword instead of == which means "is equal to"
{
StreamWriter SW;' create a stream writer
SW = File.CreateText (sfn.FileName) ' create the file
SW.Write (nameOfStringToWriteToFile) ' write the contents
SW.Close() ' close the stream writer
}

User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: File Paths?
29 Aug, 2008 - 06:26 AM
Post #4

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,063



Thanked: 17 times
Dream Kudos: 950
My Contributions
@gabehabe: Don't worry you're new to c#, but I'll show you the differences.

VB.NET

CODE

Dim sfn As New SaveFileDialog()
sfn.Title = "Save File As..."
sfn.Filter = "Text File (*.txt)|*.txt"
' the |*.txt specifies the format, The Text File (*.txt) is the display on the menu
sfn.InitialDirectory = System.Environment.CurrentDirectory
' set the initial directory to the path of the executable
If sfn.ShowDialog() = DialogResult.OK Then
    Dim SW As StreamWriter
    SW = File.CreateText(sfn.FileName)
    SW.Write(code.Text)
    SW.Close()
    currentlyOpen = sfn.FileName
End If


C#

CODE

    SaveFileDialog sfn = new SaveFileDialog();
    sfn.Title = "Save File As...";
    sfn.Filter = "Text File (*.txt)|*.txt";
    // the |*.txt specifies the format, The Text File (*.txt) is the display on the menu
    sfn.InitialDirectory = System.Environment.CurrentDirectory;
    // set the initial directory to the path of the executable
    if (sfn.ShowDialog() == DialogResult.OK) {
        StreamWriter SW;
        SW = File.CreateText(sfn.FileName);
        SW.Write(code.Text);
        SW.Close();
        currentlyOpen = sfn.FileName;
    }


Hope this helps
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: File Paths?
29 Aug, 2008 - 06:31 AM
Post #5

better than jam.
Group Icon

Joined: 6 Feb, 2008
Posts: 5,837



Thanked: 104 times
Dream Kudos: 2700
Expert In: slobbing.

My Contributions
Meh, I was close enough~

I don't have much of a use for VB anyway. The syntax is vile, "turd x = new turd();" makes more sense to me than "Dim x As New turd()"
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: File Paths?
29 Aug, 2008 - 06:44 AM
Post #6

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,063



Thanked: 17 times
Dream Kudos: 950
My Contributions
Yeah the synax does suck.
User is offlineProfile CardPM
+Quote Post

claireabell
RE: File Paths?
1 Sep, 2008 - 02:08 AM
Post #7

New D.I.C Head
*

Joined: 26 Mar, 2007
Posts: 45



Thanked: 1 times
My Contributions
QUOTE(gbertoli3 @ 29 Aug, 2008 - 07:26 AM) *

@gabehabe: Don't worry you're new to c#, but I'll show you the differences.

VB.NET

CODE

Dim sfn As New SaveFileDialog()
sfn.Title = "Save File As..."
sfn.Filter = "Text File (*.txt)|*.txt"
' the |*.txt specifies the format, The Text File (*.txt) is the display on the menu
sfn.InitialDirectory = System.Environment.CurrentDirectory
' set the initial directory to the path of the executable
If sfn.ShowDialog() = DialogResult.OK Then
    Dim SW As StreamWriter
    SW = File.CreateText(sfn.FileName)
    SW.Write(code.Text)
    SW.Close()
    currentlyOpen = sfn.FileName
End If


C#

CODE

    SaveFileDialog sfn = new SaveFileDialog();
    sfn.Title = "Save File As...";
    sfn.Filter = "Text File (*.txt)|*.txt";
    // the |*.txt specifies the format, The Text File (*.txt) is the display on the menu
    sfn.InitialDirectory = System.Environment.CurrentDirectory;
    // set the initial directory to the path of the executable
    if (sfn.ShowDialog() == DialogResult.OK) {
        StreamWriter SW;
        SW = File.CreateText(sfn.FileName);
        SW.Write(code.Text);
        SW.Close();
        currentlyOpen = sfn.FileName;
    }


Hope this helps



i'm having a few problems with this code (vb), i'm not sure if its just me being thick or what..

where you have the code sfn.initialdirectory=system.environment.currentdirectory

is this just a straight copy of the line or do i need to substitute my own variables into it, in which case which ones.. im lost

also for some reason the streamwriter is not being accepted in my code, it saying that it is an undefined type.

last one... sw = file.createtext(sfn.filename) the file . bit is not liked either says file is not declared

im quite confused lol

sorry and thanks.

This post has been edited by claireabell: 1 Sep, 2008 - 02:30 AM
User is offlineProfile CardPM
+Quote Post

dineeshd
RE: File Paths?
1 Sep, 2008 - 02:22 AM
Post #8

D.I.C Addict
Group Icon

Joined: 30 Jun, 2008
Posts: 579



Thanked: 16 times
Dream Kudos: 575
My Contributions
Just replace the sfn with your SaveFileDialog's declaration name.
User is offlineProfile CardPM
+Quote Post

claireabell
RE: File Paths?
1 Sep, 2008 - 02:42 AM
Post #9

New D.I.C Head
*

Joined: 26 Mar, 2007
Posts: 45



Thanked: 1 times
My Contributions
ok this is what code i have

CODE

Dim result As New SaveFileDialog()
        result.Title = " Save File As..."
        result.Filter = "Text File(*.txt)|*.txt"
        'the |*.txt specifies the format, the text file  (*.txt) is the display on the menu
        result.InitialDirectory = System.Environment.CurrentDirectory
        'set the initial directory to the path of the executable

        If result.ShowDialog() = DialogResult.OK Then

            Dim result1 As System.IO.StreamWriter

            result1 =FILE.createtext(result.FileName)
            For pa = 0 To pa
                result1.Write(source1percentagethickness(pa))
                result1.Write(",")
                result1.Write(source2percentagethickness(pa))
                result1.Write(",")
                result1.Write(source3percentagethickness(pa))
                result1.Write(",")
                result1.Write(source4percentagethickness(pa))
                result1.Write(",")
                result1.Write(source5percentagethickness(pa))
                result1.Write(",")
                result1.Write(source6percentagethickness(pa))
                result1.Write(vbNewLine)

            Next
            result1.Close()
           CURRENTLYOPEN = result.FileName
        End If


the bits in CAPS are the bits where errors are being thrown

any help would be appreciated.
User is offlineProfile CardPM
+Quote Post

dineeshd
RE: File Paths?
1 Sep, 2008 - 03:03 AM
Post #10

D.I.C Addict
Group Icon

Joined: 30 Jun, 2008
Posts: 579



Thanked: 16 times
Dream Kudos: 575
My Contributions
What error you are getting? And what is this CURRENTLYOPEN? is it a string variable?
User is offlineProfile CardPM
+Quote Post

claireabell
RE: File Paths?
1 Sep, 2008 - 03:09 AM
Post #11

New D.I.C Head
*

Joined: 26 Mar, 2007
Posts: 45



Thanked: 1 times
My Contributions
QUOTE(dineeshd @ 1 Sep, 2008 - 04:03 AM) *

What error you are getting? And what is this CURRENTLYOPEN? is it a string variable?



FILE.

returns- name 'file' is not declared

as for the currently open bit, i have no idea it was in the code snippet in the 4th post and i just assumed it was needed.

lol

This post has been edited by claireabell: 1 Sep, 2008 - 03:11 AM
User is offlineProfile CardPM
+Quote Post

dineeshd
RE: File Paths?
1 Sep, 2008 - 03:21 AM
Post #12

D.I.C Addict
Group Icon

Joined: 30 Jun, 2008
Posts: 579



Thanked: 16 times
Dream Kudos: 575
My Contributions
Error message very clearly saying that the file is not declared. So you may need to insert the below code in to your module.

vb
Dim file As System.IO.File



User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:55AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month