|
-
Sep 3rd, 2010, 03:35 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Obtaining current directory correctly
Hey,
im using Sysyem.IO
Code:
public uctrlFactors()
{
InitializeComponent();
//gets the current directory
string strCPath = Directory.GetCurrentDirectory();
//adds that to the file name
Uri ui = new Uri(strCPath + "\\Images\\busy.gif");
//sets the url of the webBrowser
this.webBrowser1.Url = ui;
}
to get the directory and add an image to the path wich is included in the project. However although all my project is located on my C:\ drive i.e. (documents/visualstudio/projects) whenever I execute the code for some reason strCPath always returns H:\\.
Can anyone think of anyreason why this is happening in this project. I used the same code in a different project without a problem?
-
Sep 3rd, 2010, 05:25 AM
#2
Re: Obtaining current directory correctly
Do you know what the current directory actually is? It's not the directory that your EXE is located in, although they may be the same on occasions. The current directory can change over the course of a session, plus it can be set to anything when the app is started. If you really want the current directory and "H:\" is the current directory then that's what you should be using. If what you actually want is the path of the folder you EXE was run from then that's something else:
csharp Code:
Uri ui = Path.Combine(Application.StartupPath, @"Images\busy.gif")
That said, unless you want the user to be able to replace that file to change the image after deployment, you should probably be adding that file as a resource and then either saving it as a temp file and navigating to that or else using the WebBrowser's DocumentStream to display it directly.
-
Sep 3rd, 2010, 06:04 AM
#3
Thread Starter
Frenzied Member
Re: Obtaining current directory correctly
No the user will never be able to change the image. Basically it is just an animated gif I want to display when processing, this is why I am displaying it in a webbrowser control as a workaround.
So I have added the image to my project in an image folder. I have set the image properties to Embedded Resource & Copy To Output Directory - Copy always.
Presumably this is now classed as a resource, if what you are saying works for this I guess I just need to use the WebBrowser's DocumentStream to display it directly.
FileStream source = new FileStream("????????????", FileMode.Open, FileAccess.Read);
browser.DocumentStream = source;
What will the location be if it is an embeded resource?
-
Sep 3rd, 2010, 08:05 AM
#4
Thread Starter
Frenzied Member
Re: Obtaining current directory correctly
I had some trouble accessing the exact name it is given in the manifest so found it using the loop.
However when the web browser tries to didplay the image it doesnt error but just displays a red cross i.e. image not found.
What am I missing here?
On a side issue when I put a watch on _streamand hover it says
system io stream threw an exception of type 'System.InvalidOperationException ReadTimeout, although from looking on the net this may be ignorable and is just due to accessing unmanaged resources.
Code:
//gets the current directory
string[] res = GetType().Assembly.GetManifestResourceNames();
foreach (string r in res)
{
}
Stream _stream;
_stream = GetType().Assembly.GetManifestResourceStream("CRSUsageReport.Images.busy.gif");
this.webBrowser1.DocumentStream = _stream;
-
Sep 3rd, 2010, 10:17 PM
#5
Frenzied Member
Re: Obtaining current directory correctly
If you have added the image as an embedded resource then you can use this:
Code:
MyNamespace.Properties.Resources.myImage
The above will return a Bitmap.
Also by selecting 'Copy to Output Directory' the user will be able to modify the image as it will be available in the output directory.
-
Sep 4th, 2010, 05:47 AM
#6
Thread Starter
Frenzied Member
Re: Obtaining current directory correctly
I dont want the user to be able to modify the image. I simply want to package an animated gif with the project and display it in a webbrowser control.
-
Sep 4th, 2010, 09:10 AM
#7
Frenzied Member
Re: Obtaining current directory correctly
Assuming you are using Visual Studio 2005+:
Project > Properties (Found At The Bottom) > Resources > Add Existing Item
Then if, for example, your project is called HelloWorld and the image you added was called shark.jpg use the following code to get the image.
Code:
HelloWorld.Properties.Resources.shark
Notice how there is no file extension to shark.
Intellisense will provide you with all the resources you have added.
Take a look at the In This Section in this link: http://msdn.microsoft.com/en-us/library/9za7fxc7.aspx
-
Sep 6th, 2010, 02:59 AM
#8
Thread Starter
Frenzied Member
Re: Obtaining current directory correctly
So I have added the gif as a resource and intellisence identifies it exists but how to get the webbrowser control to display it?
The code below errors because it is expecting a string.
this.webBrowser1.Navigate(CRSUsageReport.Properties.Resources.busy)
-
Sep 7th, 2010, 10:58 AM
#9
Thread Starter
Frenzied Member
Re: Obtaining current directory correctly
Thanks for the pointers people, this is the solution I finalised on.
Code:
string SaveTo;
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("CRSUsageReport.Resources.busy.gif");
var tempPath = Path.GetTempPath();
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = stream.Read(buffer, 0, Length);
// write the required bytes
SaveTo = tempPath + "busy.gif";
FileStream writeStream = new FileStream(SaveTo, FileMode.Create, FileAccess.Write);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = stream.Read(buffer, 0, Length);
}
stream.Close();
writeStream.Close();
this.webBrowser1.Navigate(SaveTo);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|