[Mono-list] how to get relative path
Chris Howie
cdhowie at gmail.com
Mon Jan 12 05:07:37 EST 2009
On Mon, Jan 12, 2009 at 4:55 AM, YyYo <yossiozani at gmail.com> wrote:
> Hi all...
>
> I am new to C# language and I have, probably a simple question, about how to
> get the relative path of the execution file.
>
> In my solution, I have a text file with some information. In my program, I
> want to open the text file using a relative path.
>
> My question is how can I get the path of the execution file, so from it I
> could know the relative path of the text file.
>
> Many thanks for the help...
There are a few cases I can see requiring this for:
1. The file is a data file that will likely never change, or only
needs to change when recompiling. In this case it would be better to
embed the file in the compiled executable as a resource and use
reflection to extract the stream at runtime.
2. The file is a configuration-type file that will probably change.
In this case you can either do what you're doing (which I would not
recommend) or use
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"YourAppName") to obtain a path to a directory where you can save and
load this stuff.
3. The file is a data file that will be processed by the application.
In this case you should either allow specification of the filename as
an argument, or if you really want to only allow one filename, just
open the file by its name and allow the environment to locate the file
in the current working directory.
And if you really want to approach the problem the way you are, then
you want to "using System.IO; using System.Reflection;" and use
Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"yourdatafilename").
--
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
More information about the Mono-list
mailing list