I am making an application called Profiler to store some people's profiles, and will use a database to store the profile photo's path.
This is my FindPhoto button (to upload the profile photo):
private void btnFindPhoto_Click(object sender, EventArgs e)
{
ofdFindPhoto.Title = "Select a Photo ";
ofdFindPhoto.InitialDirectory = #"D:\Desktop";
ofdFindPhoto.FileName = "";
ofdFindPhoto.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png|BMP Image|*.bmp";
ofdFindPhoto.Multiselect = false;
if (ofdFindPhoto.ShowDialog() != DialogResult.OK) { return; }
// Show the recently uploaded photo on profile
picProfilePhoto.ImageLocation = ofdFindPhoto.FileName;
}
When the user clicks the button to save the profile, I get the txtName.Text, txtPhone.Text, txtDescription.Text and photo path to save to the database. I want to use something like this to save the photo path:
string photoPath = Application.StartupPath + #"\Photos\" + txtName.Text +
Path.GetExtension(ofdFindPhoto.FileName);
This should generate some path like:
Drive:\InstalledPath\Profiler\Username.jpg (or whatever extension)
In short, I need to place a folder inside the application's root, but this folder should be created on install or first run (preferably that I can use on debug, too).
if(!Directory.Exists(Application.StartupPath + #"\Photos"))
Directory.CreateDirectory(Application.StartupPath + #"\Photos");
try this in program.cs.It checks for folder when the application in opened
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Profiles"))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Profiles");
}
Application.Run(new form1());
Related
I am working on a unity project and I need to save a certain screenshot from my game to a container in azure storage.
I already configured my azure account and created the container.
I managed to take the screenshot and save it in a directory on my computer but whenever I try to save it to my storage I cant because it does not found it. I go to that directory and I can see the screenshot.
This is how I do the screenshot part:
public void TappedCaptureScreenshot()
{
string filename = string.Format("{0}.png", DateTime.UtcNow.ToString("yy-MM-dd-HH-mm-ss-ff"));
Debug.Log("aca estoy 0 " + filename);
localPath = Screenshot.Capture(filename);
Debug.Log("local path " + localPath);
isCaptured = true;
//label.text = "Capture as: " + localPath;
}
This is how I save the picture:
public void TappedSave()
{
byte[] imageBytes = File.ReadAllBytes(localPath);
PutImage(imageBytes);
}
private void PutImage(byte[] imageBytes)
{
string filename = Path.GetFileName(localPath);
Debug.Log("Aca estoy 2 " + filename);
//label.text = "Put " + filename;
StartCoroutine(blobService.PutImageBlob(PutImageCompleted, imageBytes, container, filename, "image/png"));
}
Then whenever I clic a certain button, both functions are called.
This is the error I get:
FileNotFoundException: Could not find file "C:..\20-02-23-21-12-59-06.png"
I think the error has something to do with image bytes but I do not know why. Any suggestions?
I have a fileupload control (FileUpload1) in my webform which I use to load an excel file. I use a button called btn_open to display it in a gridview on click.
I also save the file using FileUpload1.SaveAs() method in a server folder.
Now I have another button called btn_edit which on click needs to use the same
file that I just loaded to do another set of operations.
How do I pass this file/file path from btn_open_Click to btn_edit_Click? I do not want to specify the exact location on the code. There will be multiple times I will open new excel files so I don't want to specify the file path to the server for every new file.This needs to happen programatically. Also, I want to avoid using Interop if its possible.
The following code snippet might make things more clear as to what I want to do.
protected void btn_open_Click(object sender, EventArgs e)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExtension.ToLower() == ".xlsx" || fileExtension.ToLower() == ".xls")
{
string path = Path.GetFileName(FileUpload1.FileName); //capture the file name of the file I have uploaded
path = path.Replace(" ", ""); // if there is any spacing between the file name it will remove it
FileUpload1.SaveAs(Server.MapPath("~/ExcelFile/") + path); //saves to Server folder called ExcelFile
String ExcelPath = Server.MapPath("~/ExcelFile/") + path; // Returns the physical file path that corresponds to the specified virtual path.
.
.
*code to display it in gridview*
.
.
}
else
{
Console.Writeline("File type not permissible");
}
}
protected void btn_edit_Click(object sender, EventArgs e)
{
//HOW DO I PASS THE ABOVE FILE HERE PROGRAMATICALLY WITHOUT SPECIFYING IT'S EXACT LOCATION ON THE SERVER??
}
Using Session variable helped me solve my problem. Hopefully it will be helpful to other people who might encounter similar issue.
On the first button (in my case btn_open_Click), add:
Session["myXlsPath"] = ExcelFilePath;
Note: "myXlsPath" is just a name I created for my Session variable. ExcelFilePath is the path of my excel file that I want to pass to the other button.
On the button you want to pass the file path to (in my case btn_edit_Click), add:
string ExcelFilePath = (string)Session["myXlsPath"];
I have recently started to get an error which states my directory can not be found I have tried a number of ways to solve this but have yet to find a solution.
The method should allow the user to select an image for their computer and add it to a folder called images inside the applications folder structure. The problem is that when using the File.copy(imageFilename, path); it throws the error. I have tried changing the path and you will see from the code snip it. It is even doing it when the program itself has passed the file path for the application and is still throwing me the error.
this is the method.
private void btnImageUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog imageFile = new OpenFileDialog();
imageFile.InitialDirectory = #"C:\";
imageFile.Filter = "Image Files (*.jpg)|*.jpg|All Files(*.*)|*.*";
imageFile.FilterIndex = 1;
if (imageFile.ShowDialog() == true)
{
if(imageFile.CheckFileExists)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
System.IO.File.Copy(imageFile.FileName, path);
}
}
}
I am using VS2013 and have included the using Microsoft.win32
Any further information needed please ask.
Thanks
There are 2 things need to be taken into consideration
private void btnImageUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog imageFile = new OpenFileDialog();
imageFile.InitialDirectory = #"C:\";
imageFile.Filter = "Image Files (*.jpg)|*.jpg|All Files(*.*)|*.*";
imageFile.FilterIndex = 1;
if (imageFile.ShowDialog() == true)
{
if(imageFile.CheckFileExists)
{
string path = AppDomain.CurrentDomain.BaseDirectory; // You wont need it
System.IO.File.Copy(imageFile.FileName, path); // Copy Needs Source File Name and Destination File Name
}
}
}
string path = AppDomain.CurrentDomain.BaseDirectory; You won need this because the default directory is your current directory where your program is running.
Secondly
System.IO.File.Copy(imageFile.FileName, path); Copy Needs Source File Name and Destination File Name so you just need to give the file name instead of path
so your updated code will be
private void btnImageUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog imageFile = new OpenFileDialog();
imageFile.InitialDirectory = #"C:\";
imageFile.Filter = "Image Files (*.jpg)|*.jpg|All Files(*.*)|*.*";
imageFile.FilterIndex = 1;
if (imageFile.ShowDialog() == true)
{
if(imageFile.CheckFileExists)
{
System.IO.File.Copy(imageFile.FileName, SomeName + ".jpg"); // SomeName Must change everytime like ID or something
}
}
}
I'm not sure if that's the problem, but the File.Copy method expects a source file name and a target file name, not a source file name and directory: https://msdn.microsoft.com/en-us/library/c6cfw35a(v=vs.110).aspx
So, to make this work, in your case you'd have to do something like the following (namespaces omitted):
File.Copy(imageFile.FileName, Path.Combine(path, Path.GetFileName(imageFile.FileName));
Note that this will fail if the destination file exists, to overwrite it, you need to add an extra parameter to the Copy method (true).
EDIT:
Just a note, the OpenFileDialog.CheckFileExists does not return a value indicating if the selected file exists. Instead, it is a value indicating whether a file dialog displays a warning if the user specifies a file name that does not exist. So instead of checking this property after the dialog is closed, you should set it to true before you open it (https://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.checkfileexists(v=vs.110).aspx)
I am very new to C#. I am doing some self studies. I did one small CRUD App using Windows Form Application. It's working fine. Now I want to do one like that in WPF. I realized, some functions, methods are not same as WFA.
Now I want to know how to save the uploaded image to a custom folder.
I have created folder in my Solution called Uploaded. I know how to Upload, Resize the images. Now I want to save this resized image to that Cutome Folder.
Here is my event.
private void SaveBtn_Click(object sender, RoutedEventArgs e)
{
string imagepath = ProfilePicURL.Text;
string picname = imagepath.Substring(imagepath.LastIndexOf('\\'));
//Rename the file as per the user first and last name
picname = FirstName.Text.Replace(" ", String.Empty) + "_" + LastName.Text.Replace(" ", String.Empty);
}
string imagepath = ProfilePicURL.Text;
var imageFile = new System.IO.FileInfo(imagepath);
if (imageFile.Exists)// check image file exist
{
// get your application folder
var applicationPath=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
// get your 'Uploaded' folder
var dir=new System.IO.DirectoryInfo(System.IO.Path.Combine(applicationPath,"uploaded"));
if(!dir.Exists)
dir.Create();
// Copy file to your folder
imageFile.CopyTo(System.IO.Path.Combine(dir.FullName,string.Format("{0}_{1}",
FirstName.Text.Replace(" ", String.Empty),
LastName.Text.Replace(" ", String.Empty))));
}
I wanted to give my user an option for "Start with Windows". When user check this option it will place a shortcut icon into Startup folder (not in registry).
On Windows restart, it will load my app automatically.
How can this be done?
you can use the Enviroment.SpecialFolder enum, although depending on your requirements you might look at creating a windows service instead of an app that has to start on startup.
File.Copy("shortcut path...", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + shorcutname);
edit:
File.Copy needs an origin file directory-path and the target directory-path to copy a file. The key in that snippet is Enviroment.GetFolderPath(Enviroment.SpecialFolder.Startup) which is getting the startup folder path where you want to copy your file to.
you could use the above code several ways. In case you've got an installer project for your app, you could run something like this on install. Another way could be when the app launches it checks if the shorcut exists there and puts one there if not (File.Exists()).
Here is a question about creating shortcuts in code also.
WshShell wshShell = new WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut;
string startUpFolderPath =
Environment.GetFolderPath(Environment.SpecialFolder.Startup);
// Create the shortcut
shortcut =
(IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(
startUpFolderPath + "\\" +
Application.ProductName + ".lnk");
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.Description = "Launch My Application";
// shortcut.IconLocation = Application.StartupPath + #"\App.ico";
shortcut.Save();
private void button2_Click(object sender, EventArgs e)
{
string pas = Application.StartupPath;
string sourcePath = pas;
string destinationPath = #"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup";
string sourceFileName = "filename.txt";//eny tipe of file
string sourceFile = System.IO.Path.Combine(sourcePath, sourceFileName);
string destinationFile = System.IO.Path.Combine(destinationPath);
if (!System.IO.Directory.Exists(destinationPath))
{
System.IO.Directory.CreateDirectory(destinationPath);
}
System.IO.File.Copy(sourceFile, destinationFile, true);
}