C# .NET - WindowForms Apps in C#

Asked By yasir
26-Apr-11 07:03 AM
I have a button named as "Browse" which should browse fa the Project Location similar to what we have in Visual studio 2008
  Ravi S replied to yasir
26-Apr-11 07:05 AM
HI
Try this code

  private void buttonBrowse_Click(object sender, EventArgs e)
      {
        openFileDialog1.ShowDialog();
        CenariosText.Text = openFileDialog1.FileName;
      }

Hope it helps...
  James H replied to yasir
26-Apr-11 07:06 AM
Hi,Like browse we have Fileupload control in .Net please check this

As the move is made to more sophisticated internet applications, users expect more advanced features. An ability to upload files to the server may be necessary for a number of applications, including document management systems and content management systems.



Create code for a simple Web app
I'll show you how to create a simple ASP.NET Web application that demonstrates uploading a file to a server. The C# code is available inhttp://www.techrepublic.com/article/create-a-file-upload-feature-with-c/5109284#, and the HTML form is available in http://www.techrepublic.com/article/create-a-file-upload-feature-with-c/5109284#. But if you'd rather go hands-on, you can follow the steps I took to build this project.

First, you fire up Visual Studio .NET. On the Start Page, click on the New Project button. Choose the following:
  • Project Type: Visual C# Projects
  • Templates: ASP.NET Web Application
  • Location: http://localhost/FileUpload

Once the Web form opens up, click on Toolbox, select HTML section, find File Field control, and drag it onto the form. Right-click on the control and set Run As Server Control. Change the Name property of the control to File1.

Click on Toolbox again, select Web Forms section, find a Button, and drag it onto the form.Set its Text property to Upload and its ID property to cmdUpload. Your form should end up looking like Figure A.

Figure A
Creating the file upload form


Now add the following code to the form definition of the aspx file:
encType="multipart/form-data"

So that the full form tag looks like this:
<form id="Form1" method="post" encType="multipart/form-data" runat="server">

Add the following code to the declaration section of the cs file:
string sFileDir= "C:\\";
long lMaxFileSize = 4096;

Note that the above values should be changed to the specific location and size limit that you want to set up. These values could also be retrieved either from a database, an INI file, or an XML file, but for this example you're simply hard-coding them.

Add the following code to the very top portion of the cs page:
Add using System.IO;

Then, add the following procedure cs code page:
private void DeleteFile (string strFileName)
{//Delete file from the server
     if (strFileName.Trim().Length > 0)
     {
     FileInfo fi = new FileInfo(strFileName);
        if (fi.Exists)//if file exists delete it
        {   
           fi.Delete();
        }
     }
}

And finally, add the code in http://www.techrepublic.com/article/create-a-file-upload-feature-with-c/5109284# to the cs file.

Press [F5] to compile and run the project.

Specify a file that you want to upload by clicking Browse and choosing an actual file from your local hard drive. Your screen should look something like Figure B.

Figure B
Testing your file upload feature


Then click the Upload button on your form. You should see a message displayed on top, as in Figure C.

Figure C
Upload confirmation screen


You should check to see if the file has been copied to the directory specified in your code with its original file name.

How does this work?
Values sFileDir and lMaxFileSize are hard coded at the top and would normally be retrieved from a database or an external file so that they are easy to change. sFileDir specifies where on the server the copied files should be saved to lMaxFileSize specifies the maximum allowed file size for an uploaded file.

Procedure DeleteFile is used to delete the copied file from the server. After the file upload takes place, the file may be moved either to the database or to some other location on the server depending on the application requirements. In this example you don’t copy the file anywhere, so you don’t call this procedure unless an error occurs, and you need to get rid of it. DeleteFile can be called after the file has been moved into the database or to another location for cleaning purposes. It accepts a full file name (directory and file name) as one argument. It verifies that the file exists and that the length of the argument is bigger than 0 and attempts to delete the file through the use of FileInfo object.

When the user clicks the cmdUpload button, you first check if such a file exists. If the file exists you determine the file name without the directory (File1.PostedFile.FileName property stores the location and the name of the file on the client’s computer) using System.IO.Path.GetFileName. Then you check if the file size is not larger than the maximum allowed. You then save the file to the designated location on the server using File1.PostedFile.SaveAs method and pass the directory and the filename to it. Upon saving the file, you display a message stating that the file has been uploaded successfully. In case of an error, you delete the file and display the error message using lblMessage label.

When uploading the files, keep in mind that ASP.NET limits the file size of the uploaded files to 4 MB or 4096 KB. If you try to upload a file that is larger than that, you will get an error. You can change that setting by changing maxRequestLength setting in httpRuntime element of the Machine.config file.
  James H replied to yasir
26-Apr-11 07:08 AM
Sorry for my previous post.In WINFORMS we have the openFileDialog.
using System;
using System.IO;
using System.Windows.Forms;
 
namespace WindowsFormsApplication18
{
  public partial class Form1 : Form
  {
  public Form1()
  {
    InitializeComponent();
  }
 
  private void button1_Click(object sender, EventArgs e)
  {
    int size = -1;
    DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
    if (result == DialogResult.OK) // Test result.
    {
    string file = openFileDialog1.FileName;
    try
    {
      string text = File.ReadAllText(file);
      size = text.Length;
    }
    catch (IOException)
    {
    }
    }
    Console.WriteLine(size); // <-- Shows file size in debugging mode.
    Console.WriteLine(result); // <-- For debugging use only.
  }
  }
}
  Riley K replied to yasir
26-Apr-11 07:09 AM
Use Process.Start() and give the path, see below code

  Process.Start("explorer.exe",path);
  yasir replied to Riley K
26-Apr-11 07:13 AM
I dint get U Raghav!!!!!!!!!!1111
  Sahil Kumar replied to yasir
26-Apr-11 07:17 AM
Hi,

I did not understood your question properly. But as per my understanding you want to set default path like this

FolderBrowserDialog flg = new FolderBrowserDialog();

flg.RootFolder = Environment.SpecialFolder.MyDocuments;

if (flg.ShowDialog() == DialogResult.OK)


here default path for your folder browser dialog will be mydocuments. Same you can set for file browser dialog also.

I hope this will help you.
  yasir replied to Sahil Kumar
26-Apr-11 08:00 AM
Pls complete the code with brief explanation
  Sahil Kumar replied to yasir
27-Apr-11 04:50 AM
Hi,

FolderBrowserDialog flg = new FolderBrowserDialog();

flg.RootFolder = Environment.SpecialFolder.MyDocuments;

if (flg.ShowDialog() == DialogResult.OK)

{
//some code
}

here when ever you click on your folder or file brower button. The default path open will be MyDocuments. From here onwards you can browse and select file or folder.

Hope this will help you.

Create New Account
help
Wise for Visual Studio.NET Wise for Visual Studio.NET By Peter A. Bromberg, Ph.D. To "Print This Page" Link Peter Bromberg Wise for Visual Studio .NET is a total and complete installation development system for creating and editing Windows® Installer
Visual Studio .net .NET Framework Hi NG, ich habe vor längerer Zeit mit Visual Studio .Net 2003 gearbeitet und überlege momentan auf einen neueren Stand upzudaten. Ein Visual Studio .Net 2008 scheint es nicht zu geben. Habe zumindest mit googeln nichts gefunden. Was
Visual Studio versioning . . . . how to tell? .NET Framework To my knowledge, Visual studio 6 was released in 1998, then Visual Studio .NET 2002 is VS 7, then Visual Studio .NET 2003 is VS 7.1, then Visual
visual studio.net 2003 and Access 2007 database .NET Framework Hi I am currently using Visual Studio.Net 2003 running on Windows Server 2000 operating system. I have used Visual Studio.net 2003 connecting to Access 2002 databases in the pass with great success. Now
Is Visual Studio self-hosting ? .NET Framework Does Microsoft use Visual Studio IDE, Visual Studio Debugger, Visual Studio Linker and Visual Studio compiler for developing Visual Studio ? Or is Visual Studio not