Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Transfer a txt file

Transfer a txt file

Scheduled Pinned Locked Moved C#
game-dev
10 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bar3000
    wrote on last edited by
    #1

    Hi, I am trying to transfer a txt file by loading it and saving it with a save file dialog.

    SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.AddExtension = true;
    saveDialog.FileName = "Checkers Game";
    saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
    saveDialog.OverwritePrompt = true;
    saveDialog.Title = "Save game";
    saveDialog.ValidateNames = true;
    saveDialog.ShowDialog();
    if (saveDialog.FileName != "")
    {
    writer.Close();
    file.Close();
    FileStream fs = new FileStream(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", FileMode.Open);
    fs = (System.IO.FileStream)saveDialog.OpenFile();

            }
    

    but when i go to the saved file i get an empty file even though the text i loaded isn't empty. Thanks:)

    A C L L M 5 Replies Last reply
    0
    • B bar3000

      Hi, I am trying to transfer a txt file by loading it and saving it with a save file dialog.

      SaveFileDialog saveDialog = new SaveFileDialog();
      saveDialog.AddExtension = true;
      saveDialog.FileName = "Checkers Game";
      saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
      saveDialog.OverwritePrompt = true;
      saveDialog.Title = "Save game";
      saveDialog.ValidateNames = true;
      saveDialog.ShowDialog();
      if (saveDialog.FileName != "")
      {
      writer.Close();
      file.Close();
      FileStream fs = new FileStream(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", FileMode.Open);
      fs = (System.IO.FileStream)saveDialog.OpenFile();

              }
      

      but when i go to the saved file i get an empty file even though the text i loaded isn't empty. Thanks:)

      A Offline
      A Offline
      Alan Balkany
      wrote on last edited by
      #2

      I don't see any statement where you actually write the text to the file. It looks like the statements in the 'if' clause are in the wrong order and incomplete.

      B 1 Reply Last reply
      0
      • B bar3000

        Hi, I am trying to transfer a txt file by loading it and saving it with a save file dialog.

        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.AddExtension = true;
        saveDialog.FileName = "Checkers Game";
        saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
        saveDialog.OverwritePrompt = true;
        saveDialog.Title = "Save game";
        saveDialog.ValidateNames = true;
        saveDialog.ShowDialog();
        if (saveDialog.FileName != "")
        {
        writer.Close();
        file.Close();
        FileStream fs = new FileStream(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", FileMode.Open);
        fs = (System.IO.FileStream)saveDialog.OpenFile();

                }
        

        but when i go to the saved file i get an empty file even though the text i loaded isn't empty. Thanks:)

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        What is wrong with File.Copy ?

        Christian Graus Driven to the arms of OSX by Vista.

        B 1 Reply Last reply
        0
        • A Alan Balkany

          I don't see any statement where you actually write the text to the file. It looks like the statements in the 'if' clause are in the wrong order and incomplete.

          B Offline
          B Offline
          bar3000
          wrote on last edited by
          #4

          the statements in the if clause close a different stream. The one which i want to open with the new stream. Google came up with FileStream fs = new FileStream(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", FileMode.Open); for opening txt files.

          1 Reply Last reply
          0
          • C Christian Graus

            What is wrong with File.Copy ?

            Christian Graus Driven to the arms of OSX by Vista.

            B Offline
            B Offline
            bar3000
            wrote on last edited by
            #5

            I don't know the destination file. he user desides where he wants to save it with the save dialog.

            V 1 Reply Last reply
            0
            • B bar3000

              Hi, I am trying to transfer a txt file by loading it and saving it with a save file dialog.

              SaveFileDialog saveDialog = new SaveFileDialog();
              saveDialog.AddExtension = true;
              saveDialog.FileName = "Checkers Game";
              saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
              saveDialog.OverwritePrompt = true;
              saveDialog.Title = "Save game";
              saveDialog.ValidateNames = true;
              saveDialog.ShowDialog();
              if (saveDialog.FileName != "")
              {
              writer.Close();
              file.Close();
              FileStream fs = new FileStream(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", FileMode.Open);
              fs = (System.IO.FileStream)saveDialog.OpenFile();

                      }
              

              but when i go to the saved file i get an empty file even though the text i loaded isn't empty. Thanks:)

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, your code seems completely wrong to me. Here are some of the problems: - a SaveFileDialog is meant for the user to enter a new filename or choose an existing filename, or to cancel the intended operation. To do that correctly you should compare the return value of ShowDialog() against DialogResult.OK (you compare FileName with empty string, I doubt that is equivalent) and use the FileName value as the path of the output file (you don't use the value at all). - (the part shown of) your code does not declare, create or use writer and file except for closing it, so at best something gets written somewhere else, and some (which?) file gets closed; anyway changing the state of writer and file should not be a side effect of the saveDialog.FileName != "" test, the writer and the file operation should only exist inside the code block following that test, and nowhere else. - declaring a FileStream and opening a file inside the code block of the if statement, while not using that stream, does not make any sense. Once the if-block is done, the stream is out of scope, hence useless. - if your code creates a FileDialog, it should also dispose of it; the using statement is the easiest way of doing it right. I suggest you have a look at some CodeProject articles; a lot of them describe small applications that do load and save some data from and to a file, using OpenFileDialog and SaveFileDialog. Just use these as search terms in the CP search facility. If you don't fully understand the FileDialog class, read its documentation; if you still feel uncomfortable, go buy and study an introductory book on the language of your choice. It will teach you the basics in a systematic way. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              modified on Sunday, June 12, 2011 8:38 AM

              1 Reply Last reply
              0
              • B bar3000

                Hi, I am trying to transfer a txt file by loading it and saving it with a save file dialog.

                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.AddExtension = true;
                saveDialog.FileName = "Checkers Game";
                saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
                saveDialog.OverwritePrompt = true;
                saveDialog.Title = "Save game";
                saveDialog.ValidateNames = true;
                saveDialog.ShowDialog();
                if (saveDialog.FileName != "")
                {
                writer.Close();
                file.Close();
                FileStream fs = new FileStream(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", FileMode.Open);
                fs = (System.IO.FileStream)saveDialog.OpenFile();

                        }
                

                but when i go to the saved file i get an empty file even though the text i loaded isn't empty. Thanks:)

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You can use this code at your own risk. I wrote it without being really awake.

                using (SaveFileDialog saveDialog = new SaveFileDialog())
                {
                saveDialog.AddExtension = true;
                saveDialog.FileName = "Checkers Game";
                saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
                saveDialog.OverwritePrompt = true;
                saveDialog.Title = "Save game";
                saveDialog.ValidateNames = true;
                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                File.Copy(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", saveDialog.FileName, true);
                }
                }

                L 1 Reply Last reply
                0
                • L Lost User

                  You can use this code at your own risk. I wrote it without being really awake.

                  using (SaveFileDialog saveDialog = new SaveFileDialog())
                  {
                  saveDialog.AddExtension = true;
                  saveDialog.FileName = "Checkers Game";
                  saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
                  saveDialog.OverwritePrompt = true;
                  saveDialog.Title = "Save game";
                  saveDialog.ValidateNames = true;
                  if (saveDialog.ShowDialog() == DialogResult.OK)
                  {
                  File.Copy(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", saveDialog.FileName, true);
                  }
                  }

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Code looks good, however spoon feeding is bad. :^)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                  modified on Sunday, June 12, 2011 8:39 AM

                  1 Reply Last reply
                  0
                  • B bar3000

                    I don't know the destination file. he user desides where he wants to save it with the save dialog.

                    V Offline
                    V Offline
                    vaghelabhavesh
                    wrote on last edited by
                    #9

                    bar3000 wrote:

                    I don't know the destination file

                    You are using the SaveFileDialog and you don't know the destination file name?:confused:

                    1 Reply Last reply
                    0
                    • B bar3000

                      Hi, I am trying to transfer a txt file by loading it and saving it with a save file dialog.

                      SaveFileDialog saveDialog = new SaveFileDialog();
                      saveDialog.AddExtension = true;
                      saveDialog.FileName = "Checkers Game";
                      saveDialog.InitialDirectory = @"C:\Documents and Settings\Bar\My Documents\";
                      saveDialog.OverwritePrompt = true;
                      saveDialog.Title = "Save game";
                      saveDialog.ValidateNames = true;
                      saveDialog.ShowDialog();
                      if (saveDialog.FileName != "")
                      {
                      writer.Close();
                      file.Close();
                      FileStream fs = new FileStream(@"C:\Documents and Settings\Bar\My Documents\savegame.txt", FileMode.Open);
                      fs = (System.IO.FileStream)saveDialog.OpenFile();

                              }
                      

                      but when i go to the saved file i get an empty file even though the text i loaded isn't empty. Thanks:)

                      M Offline
                      M Offline
                      Megidolaon
                      wrote on last edited by
                      #10

                      The FileDilaog classes only allow the user to select a path, they don't actually do anything with files. An easy way to actually write or open a file is using the Filename property of the OpenFileDialog and SaveFileDialog and save that in a string variable. Then you use File.ReadAllText() method to read a file and File.WriteAllText() method to write the text to a file. These work even without giving the user a possibility to specify the location or name of the file. Both methods have a string parameter called path, that's where you can put the path you just got from the FileDialogs (or a fixed path).

                      modified on Thursday, February 26, 2009 8:11 AM

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups