Replacing Text -Need Help-

Discussion in 'Programming & Scripting' started by trc, Jun 30, 2009 with 3 replies and 214 views.

  1. trc

    trc Newbie

    Messages:
    18
    Ratings:
    0
    Okay im trying to insert/replace text in a code with what a user types in textboxx1

    heres what it does
    button click>creates batch files in c:\ >and runs it > then deletes it>

    ex: "
    net user /add (I WANT TEXTBOX1.TEXT HERE)
    net localgroup administrators (I WANT TEXTBOX1.TEXT HERE) /add
    "


    So i want the text in the lines then it to create the file then run then delete it

    can someone give me a code to do it
     
  2. Ibdc

    Ibdc VIP VIP Retired

    Messages:
    5,100
    Ratings:
    2,072
    Dim batch as String = "net user /add" & Textbox1.Text
    System.IO.File.Create("C:\blah.bat", batch)

    That should work, haven't tested it though, that's just off the top of my head.
     
  3. Luxurious Meat

    Luxurious Meat Enthusiast

    Messages:
    615
    Ratings:
    120
    No, it won't. There is no overload for File.Create that accepts 2 string arguments, use this.

    Code:
    Dim w as New StreamWriter(File.Create("C:\batch.bat"))
    w.WriteLine(String.Format("net user /add {0}", TextBox1.Text))
    w.Close()
    
     
  4. Ibdc

    Ibdc VIP VIP Retired

    Messages:
    5,100
    Ratings:
    2,072
    My way works if you use WriteAllText instead of Create

    Dim batch As String = "net user /add " & TextBox1.Text
    System.IO.File.WriteAllText("C:\blah.bat", batch)
    Process.Start("C:\blah.bat")
    System.Threading.Thread.Sleep(5000)
    System.IO.File.Delete("C:\blah.bat")

    The last 3 lines run it, wait 5 seconds to make sure it's done, and then delete it.
     

Share This Page