SRL Archive - C/C++ Help and Tutorials

SSC C# problem please HELP!


ShowerThoughts

2008-08-04 15:49:26

Look this piece of code even creates the file when it exists =/
but it is supposed to only create the file when it doesn't exists else it overwrites my old file with info =/

Code:

 if (File.Exists(DefaultTempDir + "SSC\\Path.txt") == false) ;
{
FileInfo MyFile = new FileInfo(DefaultTempDir + "SSC\\Path.txt");
FileStream fs = MyFile.Create();
fs.Close();

}

if (File.Exists(DefaultTempDir + "SSC\\Path.txt") == false);
{
FileInfo MyFile = new FileInfo(DefaultTempDir + "SSC\\Path.txt");
FileStream fs = MyFile.Create();
fs.Close();
}

Buckleyindahouse

2008-08-04 17:14:04

From: Hermpie
Look this piece of code even creates the file when it exists =/
but it is supposed to only create the file when it doesn't exists else it overwrites my old file with info =/

Code:

 if (File.Exists(DefaultTempDir + "SSC\\Path.txt") == false) ;
{
FileInfo MyFile = new FileInfo(DefaultTempDir + "SSC\\Path.txt");
FileStream fs = MyFile.Create();
fs.Close();

}

if (File.Exists(DefaultTempDir + "SSC\\Path.txt") == false);
{
FileInfo MyFile = new FileInfo(DefaultTempDir + "SSC\\Path.txt");
FileStream fs = MyFile.Create();
fs.Close();
}


Both your if statements == false. I think 1 has to atleast == true :).

SonOfSheep

2008-08-05 00:28:07

this code is messed up in so many ways so i just re-wrote it for you!


here is how to check if a file exists, and if it doesnt create a default file.
if it does it reads it into the command prompt.

[PHP]
//We will use the same file so why create the info twice?
FileInfo MyFile = new FileInfo("C:\\DolphinsRcool.txt");
FileStream fs;

//first you must check if it exists
if (!File.Exists(MyFile.FullName))
{
fs = MyFile.Create(); //It must not exist so let's make it so
fs.Write(ASCIIEncoding.UTF8.GetBytes("Test"), 0, 4); //writing TEST to file
fs.Close(); //close the stream
}

//now to open the file and read contents into a byte array
fs = MyFile.OpenRead(); //open stream
byte[] filecontents = new byte[fs.Length]; //buffer for contents
fs.Read(filecontents, 0, (int)fs.Length); //read into buffer
fs.Close(); //close stream

//command prompt time!
Console.WriteLine("File Contents:\r\n"); //duh
Console.Write(ASCIIEncoding.UTF8.GetString(filecontents)); //get string from buffer
Console.ReadKey(); //wait
[/PHP]

Buckleyindahouse

2008-08-05 07:06:35

Added some trys/catches to SonOfSheep version.

He is gonna copy and paste it so throw some catches in there.

[PHP] //We will use the same file so why create the info twice?

FileInfo MyFile = new FileInfo("C:\\DolphinsRcool.txt");

FileStream fs;



//first you must check if it exists

if (!File.Exists(MyFile.FullName))

{

try

{

fs = MyFile.Create(); //It must not exist so let's make it so

fs.Write(ASCIIEncoding.UTF8.GetBytes("Test"), 0, 4); //writing TEST to file

fs.Close(); //close the stream

}

catch(IOException IOE)

{

Console.WriteLine("File creating/writing error: "+IOE);

}

}



//now to open the file and read contents into a byte array

try

{

fs = MyFile.OpenRead(); //open stream

byte[] filecontents = new byte[fs.Length]; //buffer for contents

fs.Read(filecontents, 0, (int)fs.Length); //read into buffer

fs.Close(); //close stream

}

catch(IOException IOE)

{

Console.WriteLine("File reading error: "+IOE);

}

//command prompt time!

Console.WriteLine("File Contents:\r\n"); //duh

Console.Write(ASCIIEncoding.UTF8.GetString(filecontents)); //get string from buffer

Console.ReadKey(); //wait

[/PHP]

ShowerThoughts

2008-08-05 10:41:23

I dont need that my reading and writing is good :)

But still i luv u!

Since i use your thing , my reading doesnt works and that sucks…

Edit:
mistake it does works, only one strange thing….

MessageBox.Show(GetImagePath());
if (GetImagePath() == "") ;
{
FolderBrowserDialog Folderdlg = new FolderBrowserDialog();
Folderdlg.ShowDialog();
SetImagePath(Folderdlg.SelectedPath);
}

it shows me the location like C;\shit\shit\shit

but still it returns true when i do GetImagepath == "" pretty strange ….

SonOfSheep

2008-08-05 15:08:28

You keep putting the ; after if.

[php]
if(foo == bar); // the semicolon tells it to end the statement
{
//the code block is executed anyway
}
[/php]

You can do if statements in a couple of ways:

[php]
//one liner
if(foo == bar)
Execute(); //the semicolon is the end of the statement

//multiple lines must be combined into a code block
if(foo == bar)
{
Execute1(); //do some stuff
foo = bar + 1; //do some more stuff
}
[/php]

ShowerThoughts

2008-08-05 16:24:58

Thanks, MATE!

ShowerThoughts

2008-08-05 17:00:25

Sorry for double but,

I works great now a few big parts are done only the image converting + selecting left, wish me good luck :)

SonOfSheep do you got msn? HermenOtter @ Live .Nl