How to download excel file using the windows application - c#

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.

Related

Read .xml file from project directory in Xamarin project

I have difficulty in reading the .xml file from Xamarin project. Whenever I debug my code I get below error
Could not find a part of the path "/storage/emulated/0/Xml/SupportedTypes.xml".
Below is the code what I am using
public static void Initialize(ConnectedDeviceCommType type)
{
string fileName = #"Xml\SupportedTypes.xml";
string filePath = Path.Combine(OS.Environment.ExternalStorageDirectory.ToString(), fileName);
try
{
var xml = XDocument.Load(filePath);
}
catch(Exception ex)
{
string st = ex.Message;
}
}
I have read that in some of the blogs that they say the .xml file needs to add to the Asset folder but in my case, I don't have any Asset folder in my project and The project I am trying to load is not an activity as well.
So can you please tell me is there any approach to read XML files directly in XAMARIN?
I finally made it work Thanks to Dimitris.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=windows
I don't really understand why you want to keep your xml file on your directrory. Because ,if you release your application, you probably won't set up the same directory structure as development. I suggest putting the files in the same folder as the application. Below the code can helps you to access your xml file from directory.
string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "SupportedTypes.xml");

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));

Copy Image to images directory in WPF

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))).

C# getting the filename of a file you open in the openfiledialog

Is there a way to get the file name of a file you open using the openfiledialog in C#? I need this because, the user is going to open an image file, but then the image file is added to a listbox(using its filename), then can be selected for display in a picturebox. Having trouble finding a solution for this.
Cheers.
Use OpenFileDialog.FileName:
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
InsertIntoList(openFileDialog.FileName);
}
Have you tried using openfiledialog.FileName property?
use openFileDialog.SafeFileName to get just the name of the file
openFileDialog.FileName returns the full path to the file

Categories

Resources