Distributing and there is some problems - c#

I will be distributing my program. It will take pictures and save them to a folder. The problem is: C:/Users/G73/Desktop/
Everyone has there own file path... In the code it is
bitmap.Save("C:/Users/G73/Desktop/My OVMK Photos//OpenVMK" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg", ImageFormat.Jpeg);
It has my File path and the name of my computer... How would I make it to change to the users path?

Try this code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
bitmap.Save(Path.Combine(path, "My OVMK Photos//OpenVMK", DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"), ImageFormat.Jpeg);
It gets Desktop path for current user. You can get more special folders using Enviroment.SpecialFolder

To get the user's desktop -
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Which you would use with a Path.Combine - e.g.:
bitmap.Save (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "My OVMK Photos//OpenVMK...
Though for images you'd likely be better off using the My Pictures directory -
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

Try this
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));

Related

Save image in a folder using relative path in asp.net c#

I'm trying to save image in a folder using relative path. My code is
bmp1.Save(#"~/mintemp/" + filename, jgpEncoder, myEncoderParameters);
I'm getting error like this.
A generic error occurred in GDI+
But when i change path like below
bmp1.Save(#"E:\web data\website\mintemp\" + filename, jgpEncoder, myEncoderParameters);
Code working fine in absolute path. Can anyone help me to solve out this issue becaue i want to save using relative path. Thanks in advance, sorry for my bad English.
Try using Server.MapPath. The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
Note that, Server.MapPath(#"~/mintemp/") returns the physical path of the folder mintemp present in the root directory of the application
bmp1.Save(Server.MapPath(#"~/mintemp/") + filename, jgpEncoder, myEncoderParameters);
You can use Server.MapPath function to get the relative path.
In your case it'll be : Server.MapPath("E:\web data\website\mintemp\" + filename). It return a string that you can use in your Save method.
More explanations here : https://forums.asp.net/t/1813648.aspx?Any+difference+between+Server+MapPath+and+Server+MapPath+

How to manage my directory's in asp.net using fileupload

Please take some of your time and read my problem
I need by using fileupload to upload just images to my web application
But when I select the image and click on upload button if the program see it a jpg file it will create a directory with the name jpg an save it there
That goes to gif and png
Please any one if you could give the code I will be very very thankful to you
Or at least link could help me
string folder = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.FileName);
string path = Request.PhysicalApplicationPath + "/" + folder;
if (!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
string server_path = Request.PhysicalApplicationPath + "/myfiles/";
FileUpload1.SaveAs(server_path + FileUpload1.FileName);
You can use System.IO.Path.GetExtension(FileUpload1.FileName); to get the extension of the file.
Physical path for the virtual directory using:
System.Web.Hosting.HostingEnvironment.MapPath(virtualDirectoryPath)

Saving an uploaded file with HttpPostedFileBase.SaveAs in a physical path

I'd like to save an uploaded file to a physical path by the method HttpPostedFileBase.SaveAs().
When I choose a physical path, an exception appears indicates that the path must be virtual.
var fileName = Path.GetFileName(fileurl.FileName);
var path = "C:/Projets" + fileName;
fileurl.SaveAs(Server.MapPath(path));
How can I change my code to be able to save the file every where I want?
The Server.MapPath works only with physical locations that are part of the website. If you want to save the file outside you could use the following:
var fileName = Path.GetFileName(fileurl.FileName);
fileurl.SaveAs(Path.Combine(#"c:\projects", fileName));
Make sure though that the account under which your application pool is executing is granted write permissions to this folder.
Server.MapPath is for virtual path. You can try to use Path.GetFullPath(path).

Problem with relative path in System.file.io of asp.net

I'm storing the file path in the database as ~/FolderName/FileName and when i try to open the file using System.IO.FileInfo(filePath) in this manner. It is not able to decipher the file path. Moreover i'm using this statement in a class DownloadFile, so i'm not able to use Page.Server.MapPath. Is there a work around for this problem.
These are the following lines of code that i'm using:
if (dr1.Read())
{
String filePath = dr1[0].ToString();
HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
String disHeader = "Attachment; Filename=\"" + fileName + "\"";
HttpContext.Current.Response.AppendHeader("Content-Disposition", disHeader);
System.IO.FileInfo fileToDownload = new System.IO.FileInfo(filePath);
string fullName = fileToDownload.FullName;
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.WriteFile(fileToDownload.FullName);
sqlCon.Close();
}
where the filepath is of the format ~/ComponentFolderForDownloading/FileName.exe
How can i solve this problem?
If you can't use Server.MapPath for determining the file location, you need to use something else. The FileInfo class can not take an ASP.NET virtual path in its constructor. It needs the real, physical path of the file.
You'll need to strip the ~/ from the front of the path; perhaps exchange the / for a \, and then use Path.Combine with the root directory of your application to find the physical location. But that assumes that your locations are in physical directories - not virtual ones.
Server.MapPath was, of course, made specifically to do this.
An alternative would be to store the real, physical locations of the files in the DB; either in addition to or in stead of the virtual, ASP.NET ones.
If you know you are running in IIS, you can use:
HttpContext.Current.Server.MapPath("~/someurl");

File.Exists using the wrong root path?

In my c# class I wrote I have a photo property that returns the photo source if the image exists (nothing or default image otherwise). In my code I use:
public string Photo
{
get
{
string source = "~/images/recipes/" + id + ".jpg";
if (File.Exists(source))
return "~/images/recipes/" + id + ".jpg";
else
return "";
}
}
If I get the FileInfo() information for this image I see that I tries to find this image in the following directory: C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\~\images\recipes
Of course the image is not located in that directory and File.Exists is returning me the wrong value. But how can I fix this?
Try this:
if(File.Exists(System.Web.HttpContext.Current.Server.MapPath(source)))
You need to map the relative path back to a physical path:
string source = HttpContext.Current.Server.MapPath("~/images/recipes/" + id + ".jpg");
You'll have to use:
Server.MapPath(source)
As you can not be 100% sure where the code will be running from, ie. it will be different in development and on a production server. Also are you sure ~/ works in windows? Wont that just be interpreted as a directory named ~? Unless thats what you want.

Categories