Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am trying to save my file in current working directory using C# Compact Framework application, where my .exe file is located but it give me IO error when i try to save like this:

"..\\..\\type1.txt"


I also tried this:

string st1 = @"\\type1.txt"

but this also gave me an IO exception..

I tried to fetch the current directory using Directory.GetCurrentDirectory(), but this gave me a not supported exception..

Please help me up asap.

Thanks,
Jason
Posted

Jason,

Paths aren't relative unless you are dealing in HTML.

You should just use the file name if you want it saved in the "current working folder" (Environment.CurrentDirectory), or get the application execution (Application.StartupPath) to save the file where the application was launched from.

So, to create the full path/file name for opening/saving in the folder the application is launched from... use:

C#
string fileName = String.Format(@"{0}\type1.txt", Application.StartupPath);


One more tip... using the @ in front of a string means 'take this literally'... so your \\ is not translated to a single \ the way you wrote that. @"\\..." would be the beginning of a network UNC path.

Hope that helps,
Frank
 
Share this answer
 
v6
Comments
Eduard Keilholz 22-Mar-11 17:53pm    
Of good help, and a good @"" tip! My 5
JOAT-MON 22-Mar-11 18:51pm    
+5 for the answer. I made a couple minor formatting changes to wrap code in code block.
fcronin 22-Mar-11 19:24pm    
Thanks brother, I never have been good at documentation... ;)
JOAT-MON 22-Mar-11 20:00pm    
No problem.
OP accidentally replied to my comment instead of to you (whoops! he deleted it and put it in its own answer). :)
Hi Frank,

Thanks for the help but as I mentioned I am using compact framework so Environment.CurrentDirectory, or get the application execution (Application.StartupPath) did not worked so I used the below string and that did the job for me.

C#
string fileName = String.Format(@"{0}\type1.txt", System.IO.Path.GetDirectoryName(
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);


Thanks a lot for your help.
Jason.
 
Share this answer
 
v3
Comments
JOAT-MON 22-Mar-11 20:08pm    
+5 Interesting, I wouldn't have thought that would be so different in CE. BTW - Added code block formatting.
fcronin 22-Mar-11 21:06pm    
Yes, very interesting... learn something new every day... never worked with the compact framework.
string fileName = String.Format(@"{0}\type1.txt", Application.StartupPath);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900