PDA

View Full Version : SSC C# problem please HELP!



ShowerThoughts
08-04-2008, 07:49 PM
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
08-04-2008, 09:14 PM
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
08-05-2008, 04:28 AM
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.



//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(filecon tents)); //get string from buffer
Console.ReadKey(); //wait

Buckleyindahouse
08-05-2008, 11:06 AM
Added some trys/catches to SonOfSheep version.

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


//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(filecon tents)); //get string from buffer

Console.ReadKey(); //wait

ShowerThoughts
08-05-2008, 02:41 PM
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
08-05-2008, 07:08 PM
You keep putting the ; after if.



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


You can do if statements in a couple of ways:



//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
}

ShowerThoughts
08-05-2008, 08:24 PM
Thanks, MATE!

ShowerThoughts
08-05-2008, 09:00 PM
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