resource management and working directory in C#
I've an inherited form, Form1, and its assembly is in c:\Project1\bin\release.
I created another project which is in d:\Project2. This project contains
a form, Form2, which will inherit to Form1 assembly. In Form2, I've the following
code,
Cursor.Current = new Cursor(@"Resources\busy_m.cur");
which points to d:\Project2\Resources for the cur file (I set the working
directory in Project properties as '.' (current) since other people may check
out my project under different drive and root). Also, 'Resources' folder
and its content are added to the project.
When I compiled the Project2 I didn't have any run-time error since the current
directory (get from Environment.CurrentDirectory property) is still under
d:\Project2
However, once Form2 inherited to Form1, reference was added manually by 'Add
reference' (select c:\Project1\bin\release\Form1.dll), Form1.dll is automatically
copied to d:\Project2\bin\debug directory.
I didn't have compile error but when I run again the above code, I got the
following run-time error:-
An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred
in mscorlib.dll
Additional information: Could not find a part of the path "c:\Project1\bin\Release\Resources\busy_m.cur".
I found out that the current directory now during run time is changed to
c:\Project1\bin\Release (Form1 assembly path), rather than my active C# project
path which was d:\Project2.
How can I make sure the working directory is always under d:\Project2? Any
function can I call or any setting can I set in C# to make sure I've a predictable
working path to work on?
By the way, if I remove '.' as the working directory for Project2, for debug
compile the above code will actually look for the cur file at d:\Project2\bin\debug\Resources,
if I later compile the release version, the run time will look for this cur
at d:\Project2\bin\release\Resources directory. Should I duplicate the Resources
folder and contents to both debug and release folder? Is there a better way
to handle this resource file location problem in C#?
What is the best way to manage resource in C# so I don't need to care about
the potential invalid path problem anymore?
Thanks!
Re: resource management and working directory in C#
If your going to want to have components reside is other directories other
then the current one, you will need to add a .config file. Look at the
deployment and assembly sections in the docs (I don't have it loaded on this
machine so I can't give exact ref). I don't know if this will work in this
case, but it would be worth a try.
"MC" <visualtest@yahoo.com> wrote in message
news:3c0dc1f0$1@147.208.176.211...
>
> I've an inherited form, Form1, and its assembly is in
c:\Project1\bin\release.
> I created another project which is in d:\Project2. This project contains
> a form, Form2, which will inherit to Form1 assembly. In Form2, I've the
following
> code,
>
> Cursor.Current = new Cursor(@"Resources\busy_m.cur");
>
> which points to d:\Project2\Resources for the cur file (I set the working
> directory in Project properties as '.' (current) since other people may
check
> out my project under different drive and root). Also, 'Resources' folder
> and its content are added to the project.
>
> When I compiled the Project2 I didn't have any run-time error since the
current
> directory (get from Environment.CurrentDirectory property) is still under
> d:\Project2
>
> However, once Form2 inherited to Form1, reference was added manually by
'Add
> reference' (select c:\Project1\bin\release\Form1.dll), Form1.dll is
automatically
> copied to d:\Project2\bin\debug directory.
>
> I didn't have compile error but when I run again the above code, I got the
> following run-time error:-
>
> An unhandled exception of type 'System.IO.DirectoryNotFoundException'
occurred
> in mscorlib.dll
>
> Additional information: Could not find a part of the path
"c:\Project1\bin\Release\Resources\busy_m.cur".
>
> I found out that the current directory now during run time is changed to
> c:\Project1\bin\Release (Form1 assembly path), rather than my active C#
project
> path which was d:\Project2.
>
> How can I make sure the working directory is always under d:\Project2? Any
> function can I call or any setting can I set in C# to make sure I've a
predictable
> working path to work on?
>
> By the way, if I remove '.' as the working directory for Project2, for
debug
> compile the above code will actually look for the cur file at
d:\Project2\bin\debug\Resources,
> if I later compile the release version, the run time will look for this
cur
> at d:\Project2\bin\release\Resources directory. Should I duplicate the
Resources
> folder and contents to both debug and release folder? Is there a better
way
> to handle this resource file location problem in C#?
>
> What is the best way to manage resource in C# so I don't need to care
about
> the potential invalid path problem anymore?
>
> Thanks!