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.
Related
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.
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");
I am wondering how to remove the version number from a file path in a Windows Form Application.
Currently I wish to save some users application data to a .xml file located in the roaming user profile settings.
To do this I use:
get
{
return Application.UserAppDataPath + "\\FileName.xml";
}
However this returns the following string:
C:\Users\user\AppData\Roaming\folder\subfolder\1.0.0.0\FileName.xml
and I was wondering if there is a non-hack way to remove the version number from the file path so the file path looks like this:
C:\Users\user\AppData\Roaming\folder\subfolder\FileName.xml
Besides parsing the string looking for the last "\", I do not know what to do.
Thanks
Use Directory.GetParent method for this purpose.
get
{
var dir = Directory.GetParent(Application.UserAppDataPath);
return Path.Combine(dir.FullName, "FileName.xml");
}
Also note that I've used Path.Combine instead of concatenating paths, this method helps you to avoid so many problems. Never concatenate strings to create path.
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.
I have to fetch all the files from a folder and i am using the function GetFiles() like
string[] dirImages = Directory.GetFiles(strPathYearImages + intYear , "*.png");
where strPathYearImages="Images\Holiday\2010\"
but when i write the whole path like
string[] dirImages = Directory.GetFiles(#"E:\IWP\Images\Holiday\"+ intYear , "*.png");
i get the required result.
How to solve this problem? I dont want to use the whole path.
Help me out.
Regards,
Jigar <3
The documentation for GetFiles() says:
The path parameter is permitted to
specify relative or absolute path
information. Relative path information
is interpreted as relative to the
current working directory
So you would want to make sure the current working directory is set correctly before trying to use the relative paths (eg to E:\IWP):
GetCurrentDirectory
SetCurrentDirectory
Use Application.StartupPath to get the location where the executable is running. From there you need to know where the images directory is relative to that directory. The only other option is the absolute path. How else would it know where to look?
You can also try using System.IO.Path methods to help - especially for combining path strings, plus it also gives you the location of special folders like My Documents, AppData, and the desktop.
Maybe it's because you are running it from VS inside. Your executable is in ProjectName\bin\Debug, therefore it looks for ProjectName\bin\Debug\Images, which obviously does not exists.
The problem is the first snippet tries to get the images under current path. So you could tell the images path relative to your current path.
Hey all, i got the answer to my question.
I just had to write
string[] dirImages = HttpContext.Current.Server.MapPath(strPathImages + intYear , "*.png");
Hope that it is helpful to all...