3

Possible Duplicate:
change file extension c#

the following code is in c++, how would i represent this in c#?

FILE fp = fopen(ChangeFileExt(Application.ExeName, ".DAT").c_str(), "w");

Another question related to this topic is:

The equivalent in c# for the following:

fputs((thisstring.CommaText + "\n").c_str(), fp); 
Community
  • 1
  • 1
Camren Chetty
  • 41
  • 1
  • 4

3 Answers3

9

How about:

FileStream fs = File.OpenWrite(Path.ChangeExtension(Application.ExeName, "DAT"));
Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
4

You can open (create) a FileStream based upon the EXE name:

FileStream fs = File.Create(
        Path.ChangeExtension(Application.ExeName, "dat"));

or maybe a TextWriter is closer to C/C++ FILE :

StreamWriter writer = File.CreateText(
        Path.ChangeExtension(Application.ExeName, "dat"));

But please note that writing to the ProgramFiles folder is prohibited for normal users.

H H
  • 263,252
  • 30
  • 330
  • 514
1

Haven't compiled it, but this might give you an idea.

string appName = Assembly.GetExecutingAssembly().Location;
FileStream s = File.Open(Path.ChangeExtension(appName , ".DAT"), FileMode.OpenOrCreate);
Patrik Svensson
  • 13,536
  • 8
  • 56
  • 77
  • Thanks guys, got it working now... used the code Justin posted. another question related to the same topic is the equivalent in c# for the following: fputs((thisstring.CommaText + "\n").c_str(), fp); Thanks Camren – Camren Chetty Sep 19 '12 at 10:27
  • var writer = new System.IO.StreamWriter("c:\filepath\file.txt"); writer.Write("This is a string without line break."); writer.WriteLine("This is a single line in the file."); – Patrik Svensson Sep 19 '12 at 14:39