c# cant locate images [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i have this piece of code
string [] ImgLocation =
Directory.GetFiles(#"Assets\Cards\Pack_Classic\", " *.png",
SearchOption.TopDirectoryOnly);
it's supposed to give me the location of all the image files inside the folder.However it doesn't work at all, it just gives me 0 strings. Why is that ?
The images location is : _Poker\Poker\bin\Debug\Assets\Cards\Pack_Classic

GetFiles needs an absolute file path in order to work in a reliable way. Get it from Reflection (through the Assembly class)
string exeDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string fullPath = Path.Combine(exeDir, #"Assets\Cards\Pack_Classic");
string[] ImgLocation = Directory.GetFiles(fullPath, "*.png", SearchOption.TopDirectoryOnly);
exeDir is your bin\Debug folder.
Note: GetFiles works also with relative paths starting at the current working directory. The problem is that you don't always know where that one is! It can be different from the directory where the executable resides.

Related

Access to the path was denied. C# WebClient Download OSX [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm aware there are answers to these questions existing already, however they more relate to windows and don't seem to be relevant to this question.
To start off, this application was made in C# and Xamarin.Forms.
What I'm trying to do is download three files into a folder on the computer. The folder does exist, so it can't be that.
Here's the code; (links have been removed)
WebClient web = new WebClient();
web.DownloadFile(new Uri(""), #"../../../ush/");
web.DownloadFile(new Uri(""), #"../../../ush/");
web.DownloadFile(new Uri(""), #"../../../ush/");
I get this while debugging;
Access to the path ' ' was denied.
If anyone knows a solution to this, let me know.
Thanks.
Turns out I wasn't giving the path to a file, just to a directory. Thanks to Sami Kuhmonen I was able to discover this.

List folders in listview [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a program where I can make folders and put files into them but I want to know which folders already exist.
As example :
In my application folder bin/debug, I have 2 folders, folder 1 and folder 2.
So when I run my program I want the name of those 2 folders to be listed into a listview so I can see which folder already exist.
Anybody knows how I can do that?
Thx!
if you know about the binding, the rest is simple:
By this you get all dictionaries in your execution (bin/debug) directory
DirectoryInfo directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
DirectoryInfo[] subdirectory = directory.GetDirectories();
List<string> directoryNames = subdirectory.Select(subdir => subdir.Name).ToList();
This way you'll get all folder in your execution directory that exist, no matter of the name. You can however check if the name is folder1 or folder two, on you subdictonaries if you'd like to

Image server path ASP.NET Including controller and method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am having a problem deleting an image in my ASP.NET MVC 5 application.
I am creating a user management module of an application which requires images/photos of the staff uploaded. However, since the profile should be editable, the image should be made possible to be deleted. But when I try to delete the image, I find it difficult locating the correct path of the image. When I use
var fileToDelete = Path.Combine(Server.MapPath("~Content/photos/people/"),updatedStaff.Photo);
System.IO.File.Delete(fileToDelete);
or
`var fileToDelete = Server.MapPath("~Content/photos/people/"+updatedStaff.Photo);
System.IO.File.Delete(fileToDelete);`
The path returned for the image is wrong in that, it contains the controller and method in the path and so I cannot delete the image.
This is the error message I get:
Could not find a part of the path 'C:\Users\Josh\documents\visual studio 2015\Projects\EduPlus\EduPlus\staffmembers\edit\~Content\photos\people\de1e1cf0-d.jpg'
"staffmembers" is the controller and "edit" is the method
Please I will appreciate any assistance to figure out the problem.
Thank you
~/ is what you need to use before Content, not just ~ .
This should work perfectly fine.
var fileToDelete = Path.Combine(Server.MapPath("~/Content/photos/people"),
updatedStaff.Photo);

Access issue while creating zip in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was trying to create a zip file programmatically in C#.
Unfortunately the below line keeps throwing error for some unknown reason.
Package zip = ZipPackage.Open("E:\\Logs",System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.ReadWrite);
Access to the path "E:\Logs" is denied.
I tried some diff piece of code, but I had the same issue
FileStream fs = File.Create("E:\\Logs");
Not sure what could be the reason. Appreciate if this could be resolved.
First you must use \\ instead of \.
Second, Open method accept a file name. So you must use it like this:
Package zip = ZipPackage.Open("D:\\a.zip",System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
It gives you the answer in the error, you don't have access to that area, you're trying to create a file in the root directory of your drive and you don't have the user rights to do that, it's the same if you don't do it programatically, if you try to create a file named logs at the root of C on any modern version of Windows you will get told you need to elevate to administrator access.

attach file to an email in c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How to attach some files or folders as one compressed file to an email in c# ?
For example assume I have 2 folders and 3 files and it is necessary that I attach these files and folders into one compressed file.
You can use DotNetZip for compressing the folder as there is a method exactly for this task :
using (var zip = new ZipFile())
{
zip.AddDirectory("DirectoryOnDisk", "rootInZipFile");
zip.Save("MyFile.zip");
}
After that you have to simply attach it to the email (
.AddAttachment(..)
).

Categories

Resources