If I use an open file dialog box that returns the absolute value of the file this works just fine:
BMP = new BitmapImage(new Uri(filename));
However, I want to have a text file that contains the names of the files, like this:
01TopographicalMap.bmp
01ElevationCompressed.elv
01Terrain.trn
01Placenames.pns
These files are loaded sequentially. Unfortunately, the previous Uri(filename) call barfs because it wants the whole absolute path. We're already in the same folder. What do I have to do to get the Uri to just default to the folder it's already in?
Thanks, for the help.
You can use Directory.GetCurrentDirectory()
Or:
Path.Combine(Directory.GetCurrentDirectory(), filename);
Related
I'm trying to access a .js file.
The file is located inside Database folder and named myfile.js which is just a JSON formatted text-file.
This is the code I'm using (this is run from MainPage.xaml.cs:
Uri uri = new Uri("/Database/myfile.js", UriKind.Relative);
var resource = Application.GetResourceStream(uri);
StreamReader streamReader = new StreamReader(resource.Stream);
string rawData = streamReader.ReadToEnd();
It always throw an exception of NullReferenceException because the resource variable is null.
So, the problem is in the Uri. I tried to use:
#"/Database/myfile.js"
"/Database/myfile.js"
"/LQI;component/Database/myfile.js"
But it doesn't work.
When I tried to move my file to root directory and replace the Uri string with
#"myfile.js"
it works! But I don't want that.
I also tried to rename the extension become .txt but nothing.
Please help.
Thanks!
Edit: I set the Build Action to "Content" which is default. I tried to set it to "Resource" but it doesn't work neither.
Edit #2: Okay, I found a clue (https://stackoverflow.com/a/12122332/2649132) that the LQI;component/Database/myfile.js only work when I set the item into Resource. And it does work.
But, the question will be still open since I doesn't work when I set the build action to "Content" with regular path.
The path is a relative path so take the leading "/" off of your path string so it reads "Database/myfile.js" and it should work fine.
I'm developing a winform application. It has a reference to a dll library. I want to use a function 'PDFImage' in this library. This function is used to put images into a PDF documnent. The function 'PDFimage' has an argument 'FileName' of type String which takes the file location of the image.
Now I have to put the image as a separate file with the .exe file created after the project is built. This is not convenient for me. What I do now is I mention the file name of the image as the function parameter like 'Flower.jpg'. And I have kept the image in the \bin\debug folder.
I don't want to do it like this as this needs the image file to be placed seperately with the executable file.
What I am trying to do is as follows:
I added the image files to the Resources folder as existing item. Now, to call the function PDFImage, I need to pass the file name as argument. How can I do this?
I have the source code of dll with me. Is it better to modify the source code as required and create another dll rather than what I am doing now?
See if this helps;
string apppath = Application.StartupPath;
string resname = #"\Resource.bmp";
string localfile = "";
localfile = apppath + resname;//Create the path to this executable.
//Btw we are going to use the "Save" method of Bitmap class.It
//takes an absolute path as input and saves the file there.
//We accesed the Image resource through Properties.Resources.Settings and called Save method
//of Bitmap class.This saves the resource as a local file in the same folder as this executable.
Properties.Resources.Image.Save(localfile);
MessageBox.Show("The path to the local file is : " + Environment.NewLine + localfile + Environment.NewLine +
"Go and check the folder where this executable is.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
//localfile is the path you need to pass to some function like this;
//SomeClass.Somefunction(localfile);
Hope this helps and here is a sample if you need.
All you can do with that is get the resource, save it to a file (temporary one may be) and then pass the filename to the function. Most function that take a file in .net also take a stream, so if you have control of both sides, I'd do that and then you don't have to mess about with the file system.
In my app I have a WebBrowser element.
I would like to load a local file in it.
I have some questions:
Where to place the HTML file (so that it will also be installed if a user executes the setup)
how to reference the file? (e.g. my guess is the user's installation folder would not always be the same)
EDIT
I've added the HTML file to my project.
And I have set it up so that it gets copied to output folder.
When I check it it is present when run: \bin\Debug\Documentation\index.html
However when I do the following I get a 'Page cannot be displayed' error in the webbrowser element.
I use the following code to try to display the HTML file in the Webbrowser.
webBrowser1.Navigate(#".\Documentation\index.html");
Do a right click->properties on the file in Visual Studio.
Set the Copy to Output Directory to Copy always.
Then you will be able to reference your files by using a path such as #".\my_html.html"
Copy to Output Directory will put the file in the same folder as your binary dlls when the project is built. This works with any content file, even if its in a sub folder.
If you use a sub folder, that too will be copied in to the bin folder so your path would then be #".\my_subfolder\my_html.html"
In order to create a URI you can use locally (instead of served via the web), you'll need to use the file protocol, using the base directory of your binary - note: this will only work if you set the Copy to Ouptut Directory as above or the path will not be correct.
This is what you need:
string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/my_html.html", curDir));
You'll have to change the variables and names of course.
quite late but it's the first hit i found from google
Instead of using the current directory or getting the assembly, just use the Application.ExecutablePath property:
//using System.IO;
string applicationDirectory = Path.GetDirectoryName(Application.ExecutablePath);
string myFile = Path.Combine(applicationDirectory, "Sample.html");
webMain.Url = new Uri("file:///" + myFile);
Note that the file:/// scheme does not work on the compact framework, at least it doesn't with 5.0.
You will need to use the following:
string appDir = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().GetName().CodeBase);
webBrowser1.Url = new Uri(Path.Combine(appDir, #"Documentation\index.html"));
Place it in the Applications setup folder or in a separte folder beneath
Reference it relative to the current directory when your app runs.
Somewhere, nearby the assembly you're going to run.
Use reflection to get path to your executing assembly, then do some magic to locate your HTML file.
Like this:
var myAssembly = System.Reflection.Assembly.GetEntryAssembly();
var myAssemblyLocation = System.IO.Path.GetDirectoryName(a.Location);
var myHtmlPath = Path.Combine(myAssemblyLocation, "my.html");
What worked for me was
<WebBrowser Source="pack://siteoforigin:,,,/StartPage.html" />
from here. I copied StartPage.html to the same output directory as the xaml-file and it loaded it from that relative path.
Windows 10 uwp application.
Try this:
webview.Navigate(new Uri("ms-appx-web:///index.html"));
Update on #ghostJago answer above
for me it worked as the following lines in VS2017
string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Navigate(new Uri(String.Format("file:///{0}/my_html.html", curDir)));
I have been trying different answers from here, but managed to derive something working, here it is:
1- Added the page in a folder i created at project level named WebPagesHelper
2- To have the page printed by webBrowser Control,
string curDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
var uri = new Uri(curDirectory);
string myFile = Path.Combine(uri.AbsolutePath, #"WebPagesHelper\index.html");
Uri new_uri = new Uri(myFile);
i had to get the assembly path, create a first uri to get an absolute path without the 'file://' attached, next i combined this absolute path with a relative path to the page in its folder, then made another URI from the result.
Then pass this to webBrowser URL property webBrowser.URL = new_uri;
There is a text file that I have created in my project root folder. Now, I am trying to use Process.Start() method to externally launch that text file.
The problem I have got here is that the file path is incorrect and Process.Start() can't find this text file. My code is as follows:
Process.Start("Textfile.txt");
So how should I correctly reference to that text file? Can I use the relative path instead of the absolute path? Thanks.
Edit:
If I change above code to this, would it work?
string path = Assembly.GetExecutingAssembly().Location;
Process.Start(path + "/ReadMe.txt");
Windows needs to know where to find the file, so you need somehow specify that:
Either using absolute path:
Process.Start("C:\\1.txt");
Or set current directory:
Environment.CurrentDirectory = "C:\\";
Process.Start("1.txt");
Normally CurrentDirectory is set to the location of the executable.
[Edit]
If the file is in the same directory where executable is you can use the code like this:
var directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var file = Path.Combine(directory, "1.txt");
Process.Start(file);
The way you are doing this is fine. This will find the text file that is in the same directory as your exe and it will open it with the default application (probably notepad.exe). Here are more examples of how to do this:
http://www.dotnetperls.com/process-start
However, if you want to put a path in, you have to use the full path. You can build the full path while only caring about the relative path using the method listed in this post:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e763ae8c-1284-43fe-9e55-4b36f8780f1c
It would look something like this:
string pathPrefix;
if(System.Diagnostics.Debugger.IsAttached())
{
pathPrefix = System.IO.Path.GetFullPath(Application.StartupPath + "\..\..\resources\");
}
else
{
pathPrefix = Application.StartupPath + "\resources\";
}
Process.Start(pathPrefix + "Textfile.txt");
This is for opening a file in a folder you add to your project called resources. If you want it in your project root, just drop off the resources folder in the above two strings and you will be good to go.
You'll need to know the current directory if you want to use a relative path.
System.Envrionment.CurrentDirectory
You could append that to your path with Path
System.IO.Path.Combine(System.Envrionment.CurrentDirectory, "Textfile.txt")
Try using Application.StartupPath path as default path may point to current directory.
This scenario has been explained on following links..
Environment.CurrentDirectory in C#.NET
http://start-coding.blogspot.com/2008/12/applicationstartuppath.html
On a windows box:
Start notepad with the file's location immediately following it. WIN
process.start("notepad C:\Full\Directory\To\File\FileName.txt");
I would like to load an image from a directory "../MyAppFolder/Logos".
My code:
Bitmap bmp = new Bitmap(#"/Logos/bitmap.bmp");
pictureBox1.Image = bmp;
This code doesn't work. When I use (#"/Bitmapx.bmp") it works, but when I want to load an image from a deeper directory I get an error message.
What am I doing wrong?
The leading slash targets the current drive root. Use the realative path...
Bitmap bmp = new Bitmap(#"Logos/bitmap.bmp");
..Or one of the many Path. methods to resolve the full path that you want.
and yes I know my example above targets the current working path... that would be why I added the above comment. And for the pointless downvoter you might like to learn that current versions of Windows don't care which slash you use.
Try this:
string myLogo = System.IO.Path.Combine(Application.StartupPath, #"Logos\bitmap.bmp");
Bitmap bmp = new Bitmap(myLogo);
pictureBox1.Image = bmp;
And make sure your Logos folder is in your application root folder.
The first character of your path is / which makes this path relative to the root level of the drive on which the current working directory lives. But you probably want a relative path so just remove the initial /.
What's more, relative paths are relative to the working directory. But the working directory is not necessarily the application directory. For example, if you navigate in a file dialog that can change your working directory.
If I were you I would probably pre-pend the path with the app directory and make it a fully-specified absolute path, exactly as HABJAN suggests.