XNA Creators Club Online
Page 1 of 1 (14 items)
Sort Posts: Previous Next

How to I add a text file to content

Last post 9/11/2009 8:44 PM by XXIst Century Boy. 13 replies.
  • 1/10/2009 7:54 AM

    How to I add a text file to content

    How to I add a text file to content?
    How to set the Content Importer and the Content Processor for text file?

    string output = System.IO.File.ReadAllText("FileContainingAllMyGameText.txt");
  • 1/11/2009 9:37 PM In reply to

    Re: How to I add a text file to content

    Add the text file to the Content folder in your project workspace. Set its 'Copy to Output Folder' property. Then you can load it from "Content/myfilename.txt" in the game.
    "I must Create a System, or be enslaved by another Man's. I will not Reason & Compare; my business is to Create." - William Blake
  • 1/11/2009 10:07 PM In reply to

    Re: How to I add a text file to content

    If it's just going to be a text string file, you could also use a Resource file instead (.resx) since then you'll get compile-time validation on the string keys and a resource file can store other objects like pictures. This way you can support language translation of in-game textures such as sign posts.
  • 1/12/2009 4:51 PM In reply to

    Re: How to I add a text file to content

    Count:
    Add the text file to the Content folder in your project workspace. Set its 'Copy to Output Folder' property. Then you can load it from "Content/myfilename.txt" in the game.


    If I just add text file to Content and do not select Content Importer and Content Processor, it will appear the error as below:

    Content\aaa.txt : error : Cannot autodetect which importer to use for "aaa.txt". There are no importers which handle this file type. Specify the importer that handles this file type in your project.
    Done building project "Content.contentproj" -- FAILED.
  • 1/12/2009 5:14 PM In reply to

    Re: How to I add a text file to content

    I've got some text files in my content directory.   I just drop them in, open properites, set Build Action to None, Copy to Output Directory to "Copy Always" (( mine change often )) and leave the other fields blank & it works fine for me.
    ..shaders make you feel... powerful, or very very stupid.
    http://drjbn.spaces.live.com/
  • 1/13/2009 12:49 PM In reply to

    Re: How to I add a text file to content

    Byron Nelson:
    I've got some text files in my content directory.   I just drop them in, open properites, set Build Action to None, Copy to Output Directory to "Copy Always" (( mine change often )) and leave the other fields blank & it works fine for me.


    Thank you,Byron Nelson!! I had solved this problem by your way.
  • 1/25/2009 2:48 PM In reply to

    Re: How to I add a text file to content

    hi byron!

    I also have the same errors when adding text files in my projects. but when i followd your solution to the problem, another exception appeared. here is the copy of the exception:

    System.IO.FileNotFoundException was unhandled
      Message="Could not find file 'C:\\Users\\Nico\\Documents\\Visual Studio 2008\\Projects\\ZombieSmashers\\MapEditor\\bin\\x86\\Debug\\maps'."
      Source="mscorlib"
      FileName="C:\\Users\\Nico\\Documents\\Visual Studio 2008\\Projects\\ZombieSmashers\\MapEditor\\bin\\x86\\Debug\\maps"
      StackTrace:
           at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
           at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
           at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
           at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
           at System.IO.StreamReader..ctor(String path)
           at MapEditor.MapClasses.Map.ReadSegmentDefinitions() in C:\Users\Nico\Documents\Visual Studio 2008\Projects\ZombieSmashers\MapEditor\MapClasses\Map.cs:line 24
           at MapEditor.MapClasses.Map..ctor() in C:\Users\Nico\Documents\Visual Studio 2008\Projects\ZombieSmashers\MapEditor\MapClasses\Map.cs:line 20
           at MapEditor.Game1.Initialize() in C:\Users\Nico\Documents\Visual Studio 2008\Projects\ZombieSmashers\MapEditor\Game1.cs:line 48
           at Microsoft.Xna.Framework.Game.Run()
           at MapEditor.Program.Main(String[] args) in C:\Users\Nico\Documents\Visual Studio 2008\Projects\ZombieSmashers\MapEditor\Program.cs:line 14
      InnerException:


    Could you helpo me out? what could possibly be wrong here?
    Thanx in advance..:-)
  • 1/25/2009 11:14 PM In reply to

    Re: How to I add a text file to content

    Hi byron

    i was able to solve the problem but in a very unusual way. I just thought, the program was looking for the text file in the following directory:

    C:\Users\Nico\Documents\Visual Studio 2008\Projects\ZombieSmashers\MapEditor\bin\x86\Debug

    so i copied my text file into that directory and it worked. but i was just worried if it'll cause some bugs later on  in the project. Your opinions are fully appreciated bro.

    thnx in advance.
  • 1/26/2009 5:49 AM In reply to

    Re: How to I add a text file to content

    It will cause problems if you leave it like that. Most likely your code is searching \...\bin\x86\Debug\ for the file but you're sending it to \...\bin\x86\Debug\Content\

    Either search in the place you put it or put it in the place you're searching.
  • 1/27/2009 12:31 PM In reply to

    Re: How to I add a text file to content

    hello MrLeebo

    i pasted the code in the \...\bin\x86\Debug\ not in the \...\bin\x86\Debug\Content\. so whats the difference?

    what's the right thing to do here? :-)
  • 1/27/2009 12:56 PM In reply to

    Re: How to I add a text file to content

    The right thing to do is use Byrons method.

    When you open the file at runtime you need to specify the path from the 'Working Directory'. When running in debug mode, by default the 'Working Directioy' is \...\bin\x86\Debug\'. So if you put SomeFile.txt in your Content folder, then at build time it will be copied to the \...\bin\x86\Debug\Content folder, so you need to open @"Content\SomeFile.txt"

    If you put SomeFile.txt in the root directory of your project, then at build time it will be copied to the \...\bin\x86\Debug folder, so you just need to open @"SomeFile.txt"

    If you put SomeFile.txt in your Content\Maps\Special folder, then at build time it will be copied to the \...\bin\x86\Debug\Content\Maps\Special folder, so you need to open @"Content\Maps\Special\SomeFile.txt"


    Game hobbyist hell-bent on coding a diabolical Matrix
  • 1/29/2009 12:10 PM In reply to

    Re: How to I add a text file to content

    hi craig.  it worked after i followed Bryon's method. thanx a lot you guys. :-)
  • 9/8/2009 10:19 PM In reply to

    Re: How to I add a text file to content

    Bryon, Craig - just wanted to add my thanks. This was driving me mad but a search of the forums brought me to your advice. My game is now running smoothly again.
  • 9/11/2009 8:44 PM In reply to

    Re: How to I add a text file to content

    With 3.1 : 

    Solution -> Add -> New Project -> Content Pipeline Extension Library

    name it "TextFileExtension", although it's not important, it's just to stay in line with the following code.

    using System; 
    using Microsoft.Xna.Framework.Content.Pipeline; 
    using System.IO; 
     
    // TODO: replace these with the processor input and output types. 
    using TInput = System.Array; 
    using TOutput = System.String; 
    // TODO: replace this with the type you want to import. 
    using TImport = System.Array; 
     
    namespace TextFileExtension 
        /// <summary> 
        /// This class will be instantiated by the XNA Framework Content Pipeline 
        /// to apply custom processing to content data, converting an object of 
        /// type TInput to TOutput. The input and output types may be the same if 
        /// the processor wishes to alter data without changing its type. 
        /// 
        /// This should be part of a Content Pipeline Extension Library project. 
        /// 
        /// TODO: change the ContentProcessor attribute to specify the correct 
        /// display name for this processor. 
        /// </summary> 
        [ContentProcessor(DisplayName = "TextFileExtension.TextFileProcessor")] 
        public class TextFileProcessor : ContentProcessor<TInput, TOutput> 
        { 
            public override TOutput Process(TInput input, ContentProcessorContext context) 
            { 
                return new String((char [])input); 
            } 
        } 
     
        /// <summary> 
        /// This class will be instantiated by the XNA Framework Content Pipeline 
        /// to import a file from disk into the specified type, TImport. 
        ///  
        /// This should be part of a Content Pipeline Extension Library project. 
        ///  
        /// TODO: change the ContentImporter attribute to specify the correct file 
        /// extension, display name, and default processor for this importer. 
        /// </summary> 
        [ContentImporter(".TXT", DisplayName = "TextFileExtension.TextFileImporter", DefaultProcessor = "TextFileExtension.TextFileProcessor")] 
        public class TextFileImporter : ContentImporter<TImport> 
        { 
            public override TImport Import(string filename, ContentImporterContext context) 
            { 
                char[] result = null
                FileStream fs = File.OpenRead(filename); 
                StreamReader sr = new StreamReader(fs); 
     
                BinaryReader binReader = new BinaryReader(File.OpenRead(filename)); 
                try 
                { 
                    byte[] sizeArray = new byte[2]; 
                    int count = binReader.Read(sizeArray, 0, 2); 
                    if (count != 0) 
                    { 
                        binReader.BaseStream.Seek(0, SeekOrigin.Begin); 
                        result = binReader.ReadChars((int)fs.Length); 
                    } 
                } 
                catch (EndOfStreamException e) 
                { 
                    Console.WriteLine("{0} caught and ignored. " + 
                        "Using default values.", e.GetType().Name); 
                } 
                finally 
                { 
                    binReader.Close(); 
                } 
                return result; 
            } 
        } 

    Just add a reference to that project in the content folder and set your text file to "compile" and the importer/processor to TextFileProcessor/TextFileImporter

    This is for 3.1 of course, as it doesn't implement any ContentTypeWriter (I think that 3.1 automatically generate writers, but it might work on 3.0 because Strings might not need a custom writer... But what do I know...)

Page 1 of 1 (14 items) Previous Next