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
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
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
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?
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL