Question about files

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I want to create a file and write into it. how can i set that the files will
be created where the program i'm creating was installed in a folder named
files? for example if i installed me program in d:\program i want to save my
file in d:\program\files.

Thanks,
Gidi.
 
Hi,

I want to create a file and write into it. how can i set that the files
will
be created where the program i'm creating was installed in a folder named
files? for example if i installed me program in d:\program i want to
save my
file in d:\program\files.

Thanks,
Gidi.


just add your path to AppPath then and you check if you have to create
this directory ;o)


string AppFilePath = Application.ExecutablePath;
string[] PathArray = AppFilePath.Split("\\".ToCharArray());
for(int i = 0; i < PathArray.GetUpperBound(0); i++)
AppPath += PathArray + "/";


Yours,
Steve
 
Hi Gidi,

Take a look at this code

if (!Directory.Exists("file"))
Directory.CreateDirectory("file");
FileStream fs = new FileStream("file/test.txt", FileMode.Append );
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("hello");
sw.Close();
fs.Close();

Because I just talk about "file" as a directory, it naturally assumes that
it is a relative path. Meaning it will start from the execution path.

HTH
Christiaan
 
Gidi said:
Hi,

I want to create a file and write into it. how can i set that the files
will
be created where the program i'm creating was installed in a folder named
files? for example if i installed me program in d:\program i want to save
my
file in d:\program\files.

Thanks,
Gidi.

In general you should avoid doing this because it means only people with
write access to program files will be able to use your application (admins
or power users). In todays worl forcing people to run as admin on their
machine is forcing people to take risks when they shouldn't have to. It is
far better to put the files per user under their user profile or if they are
common files in the common files area. If you are using Windows Forms both
of these areas are easily addressable via the Application object.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
Back
Top