Sign in to follow this  
Andy474

[C#, Windows Form] Problem Writing to a file

Recommended Posts

Hi there. (i think this is the right forum, please excuse me if its not) I am Following Tutorials From http://nickgravelyn.com/archive/#tileengine (i'm on 4 part c at the moment) i am Currently working on Saving a tile map to a file, and i can load them fine. At the moment the code below has been given during the tutorials and works fine on his video. However, with my code. I cant see any output, there is no file after the save operation has been completed. However i can see the file in recent documents on my computer, but the system cant locate the file. Process : -Load a layer File |WORKING| -Press Save |WORKING| -Select the output directory |WORKING| -Type in a file name |WORKING| -Click save |WORKING| -See your File in the Directory |NOT WORKING| Here is the method Fired by my Windows form
(TileLayer is a predefined class)
(LayerListBox | saveFileDialog1 | are items on the form)
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LayerListBox.SelectedItem != null)
            {
                string filename = LayerListBox.SelectedItem as string;
                saveFileDialog1.FileName = filename;

                TileLayer layer = LayerDict[filename];
                Dictionary<int, string> usedTextures = new Dictionary<int, string>();

                foreach(string textureName in TextureListBox.Items)
                {
                    int index = layer.isUsingTextures(TextureDict[textureName]);

                    if (index != -1)
                    {
                        usedTextures.Add(index, textureName);
                    }
                }
                List<string> usedTextureList = new List<string>();
                for (int i = 0; i < usedTextures.Count; i++)
                    usedTextureList.Add(usedTextures[i]);

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    layer.Save(filename, usedTextureList.ToArray());
                }
            }
        }    


to figure out of the texture is begin used
public int isUsingTextures(Texture2D texture)
        {
            if (tileTextures.Contains(texture))
                return tileTextures.IndexOf(texture);

            return -1;
        }

ultimatly saving the File data
public void Save(string filename, string[] textureNames)
        {
            using (StreamWriter writer = new StreamWriter(filename))
            {
                writer.WriteLine("[Textures]");
                foreach (string t in textureNames)
                    writer.WriteLine(t);

                writer.WriteLine();
                writer.WriteLine("[Layout]");

                for (int y = 0; y < Height; y++)
                {
                    string line = string.Empty;

                    for (int x = 0; x < Width; x++)
                    {
                        line += LayerMap[y, x].ToString() + " ";
                    }
                    writer.WriteLine(line);
                }
            }
        }

Share this post


Link to post
Share on other sites
I think your problem might be related to the following piece of code:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
layer.Save(filename, usedTextureList.ToArray());
}


There you are using filename to supply the Save method its filename. However, I don't see you updating the filename variable with the new value that was chosen by the user through the save dialog. So instead of using the filename local variable, you should be using the FileName property of the save dialog. Thus;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
layer.Save(saveFileDialog1.FileName, usedTextureList.ToArray());
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this