Copy Image to images directory in WPF - c#

I am developing a WPF application. I am letting user to select some picture and after that i want to save that image in EmployeePics directory which is in same Project directoy.
Here is the screenshot:
I have written following code but its not working:
string appPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmployeePics\\");
File.Copy(Chosen_File, appPath + Chosen_File);
I am getting following exception:

Try to use File.Copy method. If it is not impossible, please, provide more details.
Update:
To resolve pictures directory you could use System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmployeePics").
Update 2
Full code:
File.Copy(Chosen_File, Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmployeePics"), Path.GetFileName(Chosen_File))).

Related

Download and Move a file into a folder located in Local App Data C#

I want to download a .ini file for Fortnite and have it move from downloads to LocalAppData however I click the button and my program closes with a error 'An exception occurred during a WebClient request.' I left an image of my code and can anyone help me out? I need to to either download to the FortniteGame\Saved\Config\WindowsClient or I would like to be able to move the file to there. Thank You to anyone that can help.
There are a few problems with your question
You have pasted the image, never post an image of the code
you should always paste the inner exception while asking a question
Inner Exception -> DirectoryNotFoundException: Could not find a part of the path 'C:\Downloads\GameUserSettings.ini'.
DownloadFile method takes a file name, but you are passing path
Reference -> https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=net-5.0
I have updated the code and it is working for me.
using (WebClient webclient = new WebClient())
webclient.DownloadFile("https://cdn.discordapp.com/attachments/897280259219656744/897643067551645757/GameUserSettings.ini", "GameUserSettings.ini");
string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string specificFolder = Path.Combine(folder, "FortniteGame\\Saved\\Config\\WindowsClient");
string file = #"GameUserSettings.ini";
if (!Directory.Exists(specificFolder))
Directory.CreateDirectory(specificFolder);
File.Copy(file, Path.Combine(specificFolder, Path.GetFileName(file)));

How to download excel file using the windows application

I have master excel file with predefined template which will keep in project folder.
Now I need to download the file using the Windows application or WPF.
I have tried with following code but it did't work for me. So could you please help me for the same?
Thanks a lot for your support.
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string sourcePath = Application.StartupPath;
File.Copy(sourcePath + "\\filename.xls", saveFileDialog1.FileName);
}
It seems like it should be:
File.Copy(sourcePath + "\\filename.xls", saveFileDialog.FileName);
Both savefileDialog references should be the same and they should reference an object already defined with that same name.

How to display chm file in c# winforms application

I have added .chm file to my application root. when i fetch the file using below code it is referencing the path to bin/release/somehting.chm
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath+"\\"+"somehting.chm");
i want to get the path relative to installation location of application. please help.
the chm file added to the root directory is not loading after deploying the application. its not even loading while debugging in visual studio and not giving any error.
As I can see the first code snippet from your question calling Help.ShowHelp isn't so bad. Sometimes I'm using the related code below. Many solutions are possible ...
Please note, typos e.g. somehting.chm are disturbing in code snippets.
private const string sHTMLHelpFileName = "CHM-example.chm";
...
private void button1_Click(object sender, EventArgs e) {
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath + #"\" + sHTMLHelpFileName);
}
So, please open Visual Studio - Solution Explorer and check the properties of your CHM file. Go to the dropdown box shown in the snapshot below and set "Always copy" (here only German). Start your project in Debug mode and check your bin/debug output folder. Do the same for Release mode and output folder. The CHM should reside there and I hope your CHM call works.
You need :
String exeDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
So :
String HelpFilepath = "file://" + Path.Combine(exeDirectory , "somehting.chm");
Help.ShowHelp(this, path);
Answer from similar topic is:
// get full path to your startup EXE
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
// get directory of your EXE file
string exeDir = Path.GetDirectoryName(exeFile);
// and open Help
System.Windows.Forms.Help.ShowHelp(this, exeDir+"\\"+"somehting.chm");

how to Save File using C# Windows Application

I'm using C# windows application .
I want to save files in my local system.
I used Open File dialog to attach the files.
Here the text inside the file is copying,I want the file itself to get copied with a new name.But what I am really looking for is , it should just save the file automatically and not show the SaveDialog Box?
How it can be done in windows application.Can anybody help me please?
The code is shown below:
private string GetFileName()
{
OpenFileDialog op1 = new OpenFileDialog();
DialogResult result = op1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
txtEn.Text = op1.FileName;
FileName = op1.FileName;
//MessageBox.Show(FileName);
File.Copy(op1.FileName, #"D:\Backup\");
}
return FileName;
}
SQL Server 2012 seems unrelated to your question. Provided that you have proper access rights to the target directory, then in order to automate the procedure (as per your question) you don't need to use the OpenFileDialog; just a single line should suffice the goal:
//Overwriting a file of the same name is not allowed
File.Copy(FileName, #"D:\Backup\" + FileName)
or
//Overwriting a file of the same name is allowed
File.Copy(FileName, #"D:\Backup\" + FileName, true)
You can also apply some additional logic pertinent to backup file naming (upon necessity).
Hope this may help. Best regards,
Are you trying to copy a file from some x location on your file system to y location (in your case D:\Backup folder) in the file system? If that is the requirement here, I see that you are using the FileName property of OpenFileDialog which gets the File path. This you are appending to D:\Backup. You should instead use the Path.GetFileName property to first extract the file name with extension and then append it to the new folder path
File.Copy(fileName, #"D:\Backup\" + Path.GetFileName(fileName));

Portability worries because of custom cursor path in C#

I am using a custom cursor named hand2.cur in my C#-WPF application. I have added the cursor to a folder named Images which has all the images that I use in my application. However I've realized that I cannot add relative path to use my custom cursor as:
Cursor newCur = new Cursor("Images\\hand2.cur");
window.Cursor = newCur;
So I used this:
string absolute = System.IO.Path.GetFullPath("hand2.cur");
Cursor newCur = new Cursor(absolute);
window.Cursor = newCur;
This tries to find the hand2.cur file in the \bin\Release folder. So I added the file there and I got it working.
But the problem is, if I Publish this application and use it on a different computer, it does not work. Now the problem is with the cursor file path, because if I deploy it after commenting those 3 lines, it works correctly. So what do I do to rectify this problem?
I am using other images from the Image folder in my XAML code and they seem to port fine. But then again my knowledge of WPF is limited so if anyone has any ideas, that would help.
EDIT: I have added my Images folder to the project. I have also set the Build Action of the cursor file hand2.cur to Embedded Resource. However when I use the following two lines, I get an XAMLParseException.
System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/Slideshow;component/Images/hand2.cur"));
window.Cursor = new System.Windows.Input.Cursor(info.Stream);
The Inner Exception field when I view the details of the error reads: {"Cannot locate resource 'images/hand2.cur'."}
You could make the cursor a resource in your app/assembly and then use GetResourceStream with the pack Uri to the resources location. Pass the Stream of the StreamResourceInfo to the ctor of the Cursor. e.g.
var info = Application.GetResourceStream(new Uri("pack://application:,,,/Images/hand2.cur"));
var cursor = new Cursor(info.Stream);
I've got this working after I added the cursor file hand2.cur to my Resource1.resx resource file. Then I used the following statement in my code:
window.Cursor = new Cursor(new System.IO.MemoryStream(MyNameSpace.Resource1.hand2));

Categories

Resources