# C# Error while trying to create text file



## Meydanb (May 6, 2008)

hey, i dont know if this is the right forum, so sorry if its not.

so im doing this project for school, im doing a Check Mate Game and im having a problem:
when im trying to create a Text File im getting this error:


> Access to the path 'D:\Projects\Meydan\Chess\Saves' is denied.


this is the code:

```
public void WriteToFile()
        **

            SW = File.CreateText("D:\\Projects\\Meydan\\Chess\\Saves");
            for (int i = 0; i < 8; i++)
            **
                for (int j = 0; j < 8; j++)
                **
                    SW.WriteLine("Index_i"); // המיקום במערך של המשבצת
                    SW.WriteLine(i.ToString());
                    SW.WriteLine("Index_j"); // המיקום במערך של המשבצת
                    SW.WriteLine(j.ToString());
                    //-------------------------------------------------
                    SW.WriteLine("Empty"); // סימון שאומר שמתחת לשורה זו יהיה הערך של האם המשבצת ריקה
                    SW.WriteLine(bord[i, j].GetIsEmpty().ToString());
                    //-------------------------------------------------
                    SW.WriteLine("Type"); // סוג החייל, אם אין חייל בכל השורות אשר מתקשרות לחייל יהיה רשום 0
                    if (bord[i, j].GetIsEmpty())
                        SW.WriteLine("0");
                    else
                        SW.WriteLine(bord[i, j].GetPiece().Gettype());
                    //------------------------------------------------
                    SW.WriteLine("Path");// הכתובת של השתמונה של החייל
                    if (bord[i, j].GetIsEmpty())
                        SW.WriteLine("0");
                    else
                        SW.WriteLine(bord[i, j].GetPiece().GetPath());
                    //-------------------------------------------------
                    SW.WriteLine("First"); // האם התזוזה הראשונה
                    if (bord[i, j].GetIsEmpty())
                        SW.WriteLine("0");
                    else
                        SW.WriteLine(bord[i, j].GetPiece().Getfirst().ToString());
                    //-----------------------------------------------------------
                    SW.WriteLine("Color"); // צבע החייל, כלומר איזה צד- שחורים או לבנים
                    if (bord[i, j].GetIsEmpty())
                        SW.WriteLine("0");
                    else
                    **
                        if (bord[i, j].GetPiece().GetWhite())
                            SW.WriteLine("White");
                        else
                            SW.WriteLine("Black");
                    }
                    //---------------------------------------
                    SW.WriteLine("From_i"); // האינדקס של המקום האחרון שהחייל שנמצא על המשבצת היה בו, אם אין חייל יהיה 0
                    if (bord[i, j].GetIsEmpty())
                        SW.WriteLine("0");
                    else
                        SW.WriteLine(bord[i, j].GetFrom().X.ToString());

                    SW.WriteLine("From_j"); // האינדקס של המקום האחרון שהחייל שנמצא על המשבצת היה בו, אם אין חייל יהיה 0
                    if (bord[i, j].GetIsEmpty())
                        SW.WriteLine("0");
                    else
                        SW.WriteLine(bord[i, j].GetFrom().Y.ToString());
                    //----------------------------------------------------
                    SW.WriteLine("End"); //סימון שהמשבצת הראשונה הסתיימה ומעבר למשבצת הבאה
                }
            }

            SW.Close();
```
sorry for the comments in jibrish, its hebrew so youl probably wont understand them.

i tried giving premmisions to everyone in that folder, didnt work, plz help me, i need to start getting close to finishing this project or i wont get a grade on it.
thanks.


----------



## jamiemac2005 (Jul 5, 2007)

Hey, have you checked the file exists//is created by the program? Basically you need to narrow down the error to a specific function call. If you can't do that then i suggest you go about changing your script to include the File.exists function to check that it exists, then that you can open it, etc. (Examples here: http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx)...

Also are you running this straight from your IDE? It could be a local permissions problem because of that, run it from the command line (making sure it's your user's session).

Cheers,
Jamey


----------



## Meydanb (May 6, 2008)

well i found what was causing the error, the problem was that i forgot to add the name of the file and ".txt" so that was the problem...

by the way this is the code that creats the file so i dosnt exists until this code is executed...
so thnx anyway...


----------



## jamiemac2005 (Jul 5, 2007)

Haaha, ohhh, so you were trying to hardwrite directly to the directory? i thought saves was just the name of an empty file. Glad you've got it sorted.


----------



## Meydanb (May 6, 2008)

yep...
thnks for your help anyway.


----------

