What file does FromFile pull from? - c#

This is my first time trying to use images in my code. I cannot figure out what file the FromFile command pulls from.
firstDice.Image = Image.FromFile(fDice.ToString() + ".png");
I am trying to get the image to correspond with whatever the random number fDice is.
Here is my error message:
System.IO.FileNotFoundException: '5.png'

Environment.CurrentDirectory
As Steve says in his comment, you must specify the full path otherwise.
You can use Path.Combine to create a path based on the current working directory. Such as:
Path.Combine(Environment.CurrentDirectory, "Images");

Related

Read text file from relative path within project

I am creating a unit test which works using the following exact path:
string path = #"/Users/{username}/Coding/computershare/ChallengeSampleDataSet1.txt";
I read the text from the file by passing this path
string pricesFromFile = System.IO.File.ReadAllText(path);
However, I do not want to hardcode the complete local file path - I want to use the relative path in the project directory.
Therefore I tried the below using other articles on StackOverflow:
string path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ChallengeSampleDataSet1.txt");
But file is not found. How can I fix this so that I'm able to load the file when running the app from another machine?
Edit: the error in console using the second method is
System.IO.FileNotFoundException : Could not find file '/Users/{username}/Coding/computershare/bin/Debug/net5.0/ChallengeSampleDataSet1.txt'.
This should do the trick:
string path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory + #"..\..\..\", "ChallengeSampleDataSet1.txt");
Note the direction of the slash (\)
Since the error is "/Users/{username}/Coding/computershare/bin/Debug/net5.0/ChallengeSampleDataSet1.txt" it indicates that your base directory is /bin/debug/net5.0 down from the root of the code. By double dotting up three levels, you'll be able to find the file in question.

How to check if a file exists or not in the directory the executable is?

How can I check if a file exists or not in the directory the executable is?
I know how I could code it like this.
string path = Application.StartupPath + "config.cfg"
if(!FileExists(path))
{
//create the file
}
But the problem I am facing is that, the file is created every single time, even when the file exists, overwriting the data of the cfg file with the default ones.
You are not creating the possible file path properly. Use Path.Combine like:
string path = Path.Combine(Application.StartupPath, "config.cfg");
You are getting a path without terminating \ from Application.StartupPath, later you are concatenating the file name to it, This will create an invalid path, and since that doesn't exist, you check fails.
Just to show, the actual reason for getting the error, you can fix your code like:
string path = Application.StartupPath +"\\"+ "config.cfg";
But, do not use the above code, instead use Path.Combine to join multiple path elements.

The given path's format is not supported in windows app

I tried to get file path using below code.
string script = File.ReadAllText(Application.StartupPath + "D:\\Tax Rouding Projects\\10-12-12 TaxRoundingUtility\\TaxRoundingUtility\\Scripts\\GP_SOP_AdjustTax.sql");
But i am getting error : The given path's format is not supported
if i try to open the file from windows explorer.. i am able to go file location..
D:\Tax Rouding Projects\10-12-12 TaxRoundingUtility\TaxRoundingUtility\Scripts\
But why i cannot using c# code...
Any thing i missed in the path...
The problem lies here
Application.StartupPath + "D:\Tax Rouding Projects\10-12-12 TaxRoundingUtility\TaxRoundingUtility\Scripts\GP_SOP_AdjustTax.sql"
This might end up giving you something like
"c:\program files\myappfolder\D:\Tax Rouding Projects\10-12-12
TaxRoundingUtility\TaxRoundingUtility\Scripts\GP_SOP_AdjustTax.sql"
which is an invalid path. Append only portion of path that you need like (the second part is just an example)
Application.StartupPath + #"\TaxRoundingUtility\Scripts\GP_SOP_AdjustTax.sql".
Also make sure to escape the '\' in your file path strings.
Edit: As Dante has mentioned in the comment in the question, If your target path is fixed and known, you do not need the Application.StartppPath. Just load/read the file for which you have the complete path.

Get number of pictures using URL and Directory.GetFiles

I want to get the number of pictures that is in a folder on a website. But I can't find a way to do it.
I have tried to use
string a = Request.QueryString["nr"];
string[] filePaths = Directory.GetFiles(#"URL" + a, a + "_profil*");
But it doesn't work. I get the error:
Exception Details: System.ArgumentException: URI formats are not supported.
It is expecting the path to the folder on the file system, e.g. c:\some\folder\. Depending on what you're doing, you might be able to use Server.MapPath("~/some/folder") to resolve the full path.

Server.MapPath - Physical path given, virtual path expected

I'm using this line of code:
var files = Directory.GetFiles(Server.MapPath("E:\\ftproot\\sales"));
to locate files in a folder however I get the error message saying that
"Physical Path given but virtual path
expected".
Am new enough to using System.IO in C# so I was wondering if it's possible to enter a physical path to do this?
if you already know your folder is: E:\ftproot\sales then you do not need to use Server.MapPath, this last one is needed if you only have a relative virtual path like ~/folder/folder1 and you want to know the real path in the disk...
var files = Directory.GetFiles(#"E:\ftproot\sales");

Categories