473,516 Members | 2,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File browser in c#

Hi,

I want to be able to show a file browser embedded on a form - a browser
which is similar to the browser you get by SHBrowseForFolder().
Is that possible ?

Thank you in advance.
BR
Peter Larsen
Jan 24 '06 #1
4 23715
Hello,

Yes, it's possible via Shell API. Be aware of several tips and tricks when
implementing with .Net.
- You can obtain handle to the system's image list using SHGetFileInfo,
unfortunatelly you cannot set ImageList.Handle property directly because it's
readonly. The trick is to send TVM_SETIMAGELIST message to your tree view
control with handle returned by SHGetFileInfo.
- Use BeforeExpand event to populate child nodes

Hope this helps
--
Milosz Skalecki
MCP, MCAD
"Peter Larsen" wrote:
Hi,

I want to be able to show a file browser embedded on a form - a browser
which is similar to the browser you get by SHBrowseForFolder().
Is that possible ?

Thank you in advance.
BR
Peter Larsen

Jan 24 '06 #2
Hi Milosz,

Thanks for your comment and for the tips.

This is what i'm doing today - except from the TVM_SETIMAGELIST stuff.
Right now i am saving the icon in a ImageList and setting StateImageList
property to each tree-node.

I am not sure how to use TVM_SETIMAGELIST from within C# and i'm not sure
what it actually do.
Please give me more info :-)

/Peter

"Milosz Skalecki" <mi*****@REMOVEITwp.pl> wrote in message
news:22**********************************@microsof t.com...
Hello,

Yes, it's possible via Shell API. Be aware of several tips and tricks when
implementing with .Net.
- You can obtain handle to the system's image list using SHGetFileInfo,
unfortunatelly you cannot set ImageList.Handle property directly because
it's
readonly. The trick is to send TVM_SETIMAGELIST message to your tree view
control with handle returned by SHGetFileInfo.
- Use BeforeExpand event to populate child nodes

Hope this helps
--
Milosz Skalecki
MCP, MCAD
"Peter Larsen" wrote:
Hi,

I want to be able to show a file browser embedded on a form - a browser
which is similar to the browser you get by SHBrowseForFolder().
Is that possible ?

Thank you in advance.
BR
Peter Larsen

Jan 24 '06 #3
Please find messy rough example below (Sorry for the mess but i didn't have
much time :])

-- BEGIN CODE --
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace DirectoryBrowser
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView Browser;
private System.Windows.Forms.ImageList imageList1;
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Browser = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// Browser
//
this.Browser.ImageIndex = -1;
this.Browser.Location = new System.Drawing.Point(32, 36);
this.Browser.Name = "Browser";
this.Browser.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("Node0")});
this.Browser.SelectedImageIndex = -1;
this.Browser.Size = new System.Drawing.Size(444, 452);
this.Browser.TabIndex = 0;
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(748, 566);
this.Controls.Add(this.Browser);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0;
private const uint SHGFI_SMALLICON = 0x1;

[DllImport("shell32.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbSizeFileInfo,
uint uFlags);

private const UInt32 TV_FIRST = 4352;
private const UInt32 TVSIL_NORMAL = 0;
private const UInt32 TVSIL_STATE = 2;
private const UInt32 TVM_SETIMAGELIST = TV_FIRST + 9;
private const UInt32 TVM_GETNEXTITEM = TV_FIRST + 10;
private const UInt32 TVIF_HANDLE = 16;
private const UInt32 TVIF_STATE = 8;
private const UInt32 TVIS_STATEIMAGEMASK = 61440;
private const UInt32 TVM_SETITEM = TV_FIRST + 13;
private const UInt32 TVGN_ROOT = 0;

[StructLayout(LayoutKind.Sequential, Pack=8, CharSet=CharSet.Auto)]
private struct TVITEM
{
public uint mask;
public IntPtr hItem;
public uint state;
public uint stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public IntPtr lParam;
}

[DllImport("user32.dll")]
private static extern UInt32 SendMessage( IntPtr hWnd, UInt32 Msg,
UInt32 wParam, UInt32 lParam);

private const int MAX_PATH = 256;

[Flags]
private enum SHGetFileInfoConstants : int
{
SHGFI_ICON = 0x100, // get icon
SHGFI_DISPLAYNAME = 0x200, // get display name
SHGFI_TYPENAME = 0x400, // get type name
SHGFI_ATTRIBUTES = 0x800, // get attributes
SHGFI_ICONLOCATION = 0x1000, // get icon location
SHGFI_EXETYPE = 0x2000, // return exe type
SHGFI_SYSICONINDEX = 0x4000, // get system icon index
SHGFI_LINKOVERLAY = 0x8000, // put a link overlay on icon
SHGFI_SELECTED = 0x10000, // show icon in selected state
SHGFI_ATTR_SPECIFIED = 0x20000, // get only specified attributes
SHGFI_LARGEICON = 0x0, // get large icon
SHGFI_SMALLICON = 0x1, // get small icon
SHGFI_OPENICON = 0x2, // get open icon
SHGFI_SHELLICONSIZE = 0x4, // get shell size icon
SHGFI_PIDL = 0x8, // pszPath is a pidl
SHGFI_USEFILEATTRIBUTES = 0x10, // use passed dwFileAttribute
SHGFI_ADDOVERLAYS = 0x000000020, // apply the appropriate overlays
SHGFI_OVERLAYINDEX = 0x000000040 // Get the index of the overlay
}

private enum IconSize
{
Small = 0,
Large = 1
}

private const int FILE_ATTRIBUTE_NORMAL = 0x80;

[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
public string szTypeName;
}

[DllImport("shell32")]
private static extern IntPtr SHGetFileInfo (
string pszPath,
int dwFileAttributes,
ref SHFILEINFO psfi,
uint cbFileInfo,
uint uFlags);
private void Form1_Load(object sender, System.EventArgs e)
{
this.SetImageList();

foreach (string file in System.IO.Directory.GetFiles("c:\\", "*.*"))
{
TreeNode node = Browser.Nodes.Add(System.IO.Path.GetFileName(file) );
node.ImageIndex = GetImageIndex(file, IconSize.Small);
}
}

/// <summary>
///
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
private int GetImageIndex(string file, IconSize size)
{
SHFILEINFO shfi = new SHFILEINFO();
SHGetFileInfoConstants flags =
SHGetFileInfoConstants.SHGFI_SYSICONINDEX;

/* Check the size specified for return. */
if (size == IconSize.Small)
{
flags |= SHGetFileInfoConstants.SHGFI_SMALLICON ;
}
else
{
flags |= SHGetFileInfoConstants.SHGFI_LARGEICON;
}

SHGetFileInfo(file,
FILE_ATTRIBUTE_NORMAL,
ref shfi,
(uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi ),
(uint) flags);

return shfi.iIcon;
}

private void SetImageList()
{
SendMessage(
Browser.Handle,
TVM_SETIMAGELIST,
TVSIL_NORMAL,
(uint) this.GetSystemImageListHandle(IconSize.Small).ToIn t32());
}

private IntPtr GetSystemImageListHandle(IconSize size)
{
SHGetFileInfoConstants dwFlags =
//SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES |
SHGetFileInfoConstants.SHGFI_SYSICONINDEX ;
if (size == IconSize.Small)
{
dwFlags |= SHGetFileInfoConstants.SHGFI_SMALLICON;
}
else
{
dwFlags |= SHGetFileInfoConstants.SHGFI_LARGEICON;
}
// Get image list
SHFILEINFO shfi = new SHFILEINFO();
uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType());

// Call SHGetFileInfo to get the image list handle
// using an arbitrary file:
return SHGetFileInfo(
"c:\\",
FILE_ATTRIBUTE_NORMAL,
ref shfi,
shfiSize,
(uint)dwFlags);

}

}
}
-- END CODE --

Hope this helps
--
Milosz Skalecki
MCP, MCAD
"Peter Larsen" wrote:
Hi Milosz,

Thanks for your comment and for the tips.

This is what i'm doing today - except from the TVM_SETIMAGELIST stuff.
Right now i am saving the icon in a ImageList and setting StateImageList
property to each tree-node.

I am not sure how to use TVM_SETIMAGELIST from within C# and i'm not sure
what it actually do.
Please give me more info :-)

/Peter

"Milosz Skalecki" <mi*****@REMOVEITwp.pl> wrote in message
news:22**********************************@microsof t.com...
Hello,

Yes, it's possible via Shell API. Be aware of several tips and tricks when
implementing with .Net.
- You can obtain handle to the system's image list using SHGetFileInfo,
unfortunatelly you cannot set ImageList.Handle property directly because
it's
readonly. The trick is to send TVM_SETIMAGELIST message to your tree view
control with handle returned by SHGetFileInfo.
- Use BeforeExpand event to populate child nodes

Hope this helps
--
Milosz Skalecki
MCP, MCAD
"Peter Larsen" wrote:
Hi,

I want to be able to show a file browser embedded on a form - a browser
which is similar to the browser you get by SHBrowseForFolder().
Is that possible ?

Thank you in advance.
BR
Peter Larsen


Jan 25 '06 #4
Thanks it works.
There are still a lot of things to do, but the concept is clear to me now.
Thanks for your time.
/Peter

Jan 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2440
by: CHIN | last post by:
Hi all.. here s my problem ( maybe some of you saw me on other groups, but i cant find the solution !! ) I have to upload a file to an external site, so, i made a .vbs file , that logins to the site, and then i have to select the file to upload.. i used sendkeys.. and i worked perfect.. BUT ... the computer must be locked for security (...
7
4522
by: Joe | last post by:
I have an upload file operation in the web application. UploadForm.asp is the form, and UploadAction.asp is the form processing. //UploadForm.asp <FORM NAME="InputForm" ACTION="UploadAction.asp" METHOD="POST" enctype=multipart/form-data> <input type="file" name="fileName"> //etc ... </FORM>
3
6483
by: David | last post by:
Should you be able to map a any file extension to the ASP.NET dll and have it execute ASP.NET script, just like an .aspx page, when it is requested by a browser. If not, why bother giving us the ability to create application mappings. I have an .aspx page that calls an SAP function which returns an actual PDF to my calling .aspx. My .aspx...
2
3379
by: Jeff G. | last post by:
Hello everyone, I have read through the newsgroups (thank God for 'em!) extensively looking for an example of how to pass a file (PDF) from a webresponse stream down to a web client. Here's the scenario - when the web browser requests a file, the IIS Server1 has to go back to Server2 to make a request for the file. Server2 then responds...
7
7756
by: moondaddy | last post by:
I want to dynamically create a JavaScript file and cache it on the client for re-use. I know how to write javascript to a web page from the code behind, but I don't know how to actually create a file such as MyNewScript.js and then cache that on the client so all the pages of that session can use it. Can this be done? Thanks --...
15
4714
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is...
6
10019
by: qysbc | last post by:
I have a web page and there is a link to open a TIFF file. The way I do it is to have the server code open a binary stream, set the content type to "image/tiff" and call Response.BinaryWrite. On the client machine, the file type TIFF is associated with Kodak Imaging Preview. This app works on most client machines. When you click on the link,...
2
10319
by: Lyle Fairfield | last post by:
I am using Microsoft’s Web Browser Control embedded on an Access Form to browse a specific site. I have a good reason for doing so; the pages on this site run code which aborts their display unless their window is the top window; they also treat all child windows as not-logged in windows. So I have not accomplished what I want with HTA’s or...
3
2443
by: pd | last post by:
guys, in my web app, i have a simple download button and a href which points to a file on my server, when the user clicks on the button or the link, the file should simply be downloaded to the users machine. now this file is a postgres backup file with the .backup extension. now when i use the button, in FF the file download button pops...
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7548
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.