File Upload in C# windows Application - c#

In my C# Windows application I want to upload a pdf file but in my toolbox I cannot find a FileUpload control.
How can I go about and uploading a pdf file in a C# windows application.?
regards

After you put a OpenFileDialog control on your form, let's say that you click a button and:
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
//Do whatever you want
//openFileDialog1.FileName .....
}
}
it goes something like this :-)

You can use OpenFileDialog to get the filename of the file you need and then .NET File object to read data from the file. You might need a control being able to display a PDF file. Please read the following:
Viewing PDF in Windows forms using C#

Related

WPF - How to close a file that was opened in RichTextBox

On a click event of a button , I'm opening a file in RichTextBox of a WPF app as follows. I've another button for closing the file but need to figure out how to close that opened file after reading it but without exiting my entire WPF app.
Think of it as a scenario similar to what we have in Microsoft WORD application where you can open a WORD document by using File-->Open menu item, and then you can close it by using File-->Close menu item while keeping the WORD application still open with top Ribbon. In my case, I've a toolbar on top with open and close button that I need to perform similar Open/Close file operations on:
private void BtnOpenFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Rich Text Format (*.rtf)|*.rtf|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
FileStream fileStream = new FileStream(dlg.FileName, FileMode.Open);
TextRange range = new TextRange(mainRTB.Document.ContentStart, mainRTB.Document.ContentEnd);
range.Load(fileStream, DataFormats.Rtf);
}
}
If you want to "close" the file whose content is shown in a RichTextBox, I think it'd be enough to clear the RichTextBox and to dispose your resources.
By "disposing your resources", I mean closing your FileStream or wrap it in a using, for example.

How to save an excel document?

I have been trying to save an open excel workbook and have not been able to.I am using Visual Studio to write a program in c#. I have a windows Userform which when launches, open an excel document in a webBrowser within the form. On that userform, I created a Save button as I would like to save the changes made to the excel.
My code to open excel in webbrowser is:
private void Excelform_Load(object sender, EventArgs e)
{
// Application.DoEvents();
if (File.Exists(m_ExcelFileName))
{
webBrowser1.Navigate(m_ExcelFileName, false);
}
}
This works fine.
HOWEVER when I try to save the changes to the file I either get an error (File in or OR Operation not Executed). Tried webBrowser1.ShowSaveAsDialog(); but this requires user interaction. All I want is when the user click on the save_btn the file saves.
SO can anyone help be figure out how to save the changes made to an excel file that is currently open in a webBrowser on a userform?
Right now this is all I have:
private void toolStripButton3_Click(object sender, EventArgs e)
{
try
{
webBrowser1.ShowSaveAsDialog();
}
catch (Exception ex)
{
// Not updated
MessageBox.Show(ex.Message);
}
}
HELP PLEASE :(

how i can insert an image in web browser control in c# with out open image ui?

I have a web browser control in C# that it's design mode is on. I use it to make a (WYSIWYG) HTML editor. I want to insert a photo on this without UI, when user wants to insert image I show to him/her a window that shows on it some known name for user. Then with search (s)he find her/his photo and add it to control.
this is my open image form:
Photos are in database and user only know them with names. I upload all of them in a folder and show above list to user and user selects a picture. I want to add image by it's location in hard disk and allow user to set its alignment .
How I can do this ?
Adding a new element to a document is very logical. The first step is to create the node (element) you wish to append, the next is to find where you wish to append it within the document, and the final step is to actually do the appending.
I write an Example for you:
private void Form1_Load_1(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<html><body></img></body></html>";
}
private void insert_image_btn_Click(object sender, EventArgs e)
{
HtmlElement userimage = webBrowser1.Document.CreateElement("img");
userimage.SetAttribute("src", "image location");
userimage.Id = "imageid";
webBrowser1.Document.Body.AppendChild(userimage);
}
private void btn_set_aliign_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("imageid").SetAttribute("float", "right");
}

how to open listview window explorer in listview control

i have a listview window browser thats working completely fine .i open folder through folder browser and the files and folders in that particular directory opens in the listview via using
PopulateListView(path)
now in my mouse double click event im opening a particular file and folder it opens the file but when it opens the directory a new window pops up .i want that directory to b opened in the listview control...the code for this scenarioa is
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string pathdoubleClicked = listView1.FocusedItem.Tag.ToString();
PopulateListView(pathdoubleClicked);
Process.Start(pathdoubleClicked);
simpleStack.Push(pathdoubleClicked);
}
now i want to do it with if else like if the path is of drectory then go to populatelistview method other wise process.start but its now working any idea how can i do this
From what I understand, if the path is a directory, you want to display its content on double-click. If it's a file, you want to open it.
So you would simply do:
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string pathdoubleClicked = listView1.FocusedItem.Tag.ToString();
if (System.IO.Directory.Exists(pathdoubleClicked))
{
PopulateListView(pathdoubleClicked);
}
else
{
Process.Start(pathdoubleClicked);
}
// ?
simpleStack.Push(pathdoubleClicked);
}

Printing a dwf/dwfx file from WPF

I'm currently attempting to print a document from WPF. I'm using the web browser because it contains an active x control which has the autodesk plugin which I need to view the document.
I'm aware that WPF doesn't directly support web browser but I've just integrated the Windows Forms library for this. I've managed to get the code running and even printing, however the document that prints is blank.
I'm not sure if it could possibly be a conflict between the Windows Forms library and WPF; I'm navigating to the document and only printing once it's loaded with no errors thrown.
Here's the code I'm using:
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.WebBrowser w = new System.Windows.Forms.WebBrowser();
Uri uri = new Uri("C:\\BOS-BD-4518-000.dwg.dwf");
w.Navigate(uri);
w.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(w_DocumentCompleted);
}
void w_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
System.Windows.Forms.WebBrowser w = (System.Windows.Forms.WebBrowser)sender;
w.Print();
}
One possible hitch could be that the active x control is not being allowed to be load, does anyone know how to force the control to be initialised.
Does anyone have any ideas about how to solve this or another method of printing an autodesk (.dwf) document
Thanks in advance,
SumGuy
Not really an answer of sorts but a solution if anyone out does want to print a .dwf file. Don't, use the new form .dwfx. This is the new file type Autodesk are switching too and its actually a form of XPS which makes things quite easy. You can load it into a web browser without needing active x OR (this is the better way) use the XPS libraries in visual studio because it can be loaded very simply into an XPS viewer.
The code I eventually used to print the dreaded file is below:
private PrintQueue printQueue;
PrintDialog pDialog = new PrintDialog();
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;
if (pDialog.ShowDialog() == true)
PrintSystemJobInfo xpsPrintJob = printQueue.AddJob(v.FileName, v.FilePath, false);
How easy's that??? There are loads of other ways of doing it using XPS. You can basically just use the dwfx file as an XPS document

Categories