您的位置:首页 > 其它

点击超链接从VSTF、SVN及文件共享服务器上下载文件

2012-09-24 17:30 381 查看
最近实现一个公司的需求, 不同部门的产品文档路径放在不同地址、不同种类的服务器上, 草草做完一版, 发现文档路径中还有文件共享路径(\\192.168.0.0类型)、还有svn(http://192.168.0.0类型)、还有VSTF路径($/Document/ss....), 各种坑啊, 领导们说起来都很轻松哈, 这样那样就行..... 这个应该不难吧.... 都懒得跟他们解释, 爱咋咋吧

言归正传, 文档路径是放在不同类型的服务器上, 目标是一点击文档的链接就能直接下载文件, 还是所有的人都可以下载, 还不能影响用户的原有权限.开始想的简单了, 后来发现这东西头疼啊.

思路:

对于VSTF文件: 当点击的时候文档连接的时候, 通过VSTF的提供的工具, 将代码及Sql脚本获取到固定地址的文件夹, 该文件夹需要发布为网站且能够通过浏览器直接访问文件.

对于SVN文件: 可以通过HTTP访问, 但是涉及到权限问题, 需要给请求附加临时的账户信息, 这里没有考虑修改Cookie信息, 这会是用户的权限变成临时账户. 实现方法就是由WebServer发起请求, 附加账户信息, 将获得的信息返回给客户端浏览器.

对于文件共享: 为了达到练习的效果, 使用了WMI方式和CMD.exe两种方式.

总体思路: 就是返回给客户端的超链接为xxx.com/WebRequestHandler?url=http://192.168.0.0.xxx, 一般处理程序接收到请求后, 更具请求类型、地址类型做相应处理.

领导催促的比较急, 所以好多地方都直接偷懒了, 没有封装, 想的是下回搞TD的时候在整理下吧

具体代码如下:

页面中的超链接代码:

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Runtime.InteropServices;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace WebSite.Common
{
public class FileSharedHelper
{
private static readonly string _OuterDepSvnName = ConfigurationManager.AppSettings["OutDepartmentSVNName"];
private static readonly string _OuterDepSvnPwd = ConfigurationManager.AppSettings["OutDepartmentSVNPwd"];

#region WMI访问文件共享
/// <summary>
/// 建立到服务器资源的连接
/// </summary>
/// <param name="resourcePath">System.String类型, 表示指定本地驱动器或远程共享资源.</param>
/// <returns>System.Unit类型, 如果方法执行成功返回0(NO_ERROR), 否则返回ERROR_CODE.</returns>
public static int ConnectResource(string resourcePath)
{
int result = -1;
if (resourcePath.StartsWith("\\"))
{
//string pattern = "[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,3}";
//Regex reg = new Regex(pattern);
//if (reg.IsMatch(resourcePath))
//{
//    string ipAddress = "";
//    ipAddress = reg.Match(resourcePath).Value;
//result = Convert.ToInt32(WNetAddConnection(string.Format("{0}/{1}", "", _OuterDepSvnName), _OuterDepSvnPwd, resourcePath, null));
result = Convert.ToInt32(WNetAddConnection(_OuterDepSvnName, _OuterDepSvnPwd, resourcePath, ""));
//}
}
return result;
}

/// <summary>
/// 断开到服务器资源的连接
/// </summary>
/// <param name="resourcePath">System.String类型, 表示指定本地驱动器或远程共享资源.</param>
/// <returns>System.Unit类型, 如果方法执行成功返回0(NO_ERROR), 否则返回ERROR_CODE.</returns>
public static int DisconnectResource(string resourcePath)
{
int result = -1;
if (resourcePath.StartsWith("\\"))
{
result = Convert.ToInt32(WNetCancelConnection(resourcePath, 1, true));
}
return result;
}

/// <summary>
/// 强制断开资源
/// </summary>
/// <param name="resourcePath">System.String类型, 表示资源的路径. </param>
public static void ForceDisconnectResource(string resourcePath)
{
string cmd = string.Format("NET USE {0} /DELETE /Y", resourcePath);
WinExec(cmd, Convert.ToUInt16((int)ShowWindow.SW_HIDE));
}

/// <summary>
/// 强制断开远程资源
/// </summary>
/// <param name="winExecCommand">System.String类型, 表示要断开资源的命令</param>
/// <param name="showWindowOption">System.Unit类型, 表示显示窗口的相关属性.</param>
/// <returns>System.Unit, 表示是否成功断开资源. </returns>
[DllImport("kernel32.dll", EntryPoint = "WinExec")]
private static extern uint WinExec(string winExecCommand, uint showWindowOption);

//mpr.dll是Windws操作系统网络通讯相关模块,通过对功能需求的分析,直接调用mpr.dll来实现该功能。
/// <summary>
/// 添加远程网络连接.
/// </summary>
/// <param name="lpNetResource">WebSite.Common.NetResource类型, 表示包含网络访问资源的结构. </param>
/// <param name="lpPassword">System.String类型, 表示远程计算机的密码. </param>
/// <param name="lpUsername">System.String类型, 表示远程计算机的用户名. </param>
/// <param name="dwFlags">System.Unit类型, 表示连接选项. 可选值CONNECT_INTERACTIVE、CONNECT_PROMPT、CONNECT_REDIRECT、CONNECT_UPDATE_PROFILE、CONNECT_COMMANDLINE、CONNECT_CMD_SAVECRED</param>
/// <returns>System.Unit类型, 如果方法执行成功返回0(NO_ERROR), 否则返回ERROR_CODE.</returns>
[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);

/// <summary>
/// 释放存在的网络连接.
/// </summary>
/// <param name="lpName">System.String类型, 表示指定本地驱动器或远程共享资源. </param>
/// <param name="dwFlags">System.Unit类型, 表示断开连接方式, 可设置0或CONNECT_UPDATE_PROFILE. </param>
/// <param name="fForce">System.Boolean类型, 表示是否强制断开连接. </param>
/// <returns>System.Unit类型, 如果方法执行成功返回0(NO_ERROR), 否则返回ERROR_CODE.</returns>
[DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);

/// <summary>
/// 添加远程网络连接.
/// </summary>
/// <param name="netResource">WebSite.Common.NetResource类型, 表示包含网络访问资源的结构. </param>
/// <param name="username">System.String类型, 表示远程计算机的用户名.</param>
/// <param name="password">System.String类型, 表示远程计算机的密码.</param>
/// <returns>System.Unit类型, 如果方法执行成功返回0(NO_ERROR), 否则返回ERROR_CODE.</returns>
private static uint WNetAddConnection(NetResource netResource, string username, string password)
{
uint result = WNetAddConnection2(netResource, password, username, 0);
return result;
}

/// <summary>
/// 添加远程网络连接.
/// </summary>
/// <param name="username">System.String类型, 表示远程计算机的用户名.</param>
/// <param name="password">System.String类型, 表示远程计算机的密码.</param>
/// <param name="remoteName">System.String类型, 表示远程计算机资源路径.</param>
/// <param name="localName">System.String类型, 表示将远程计算机映射为本地资源的名称.</param>
/// <returns>System.Unit类型, 如果方法执行成功返回0(NO_ERROR), 否则返回ERROR_CODE.</returns>
private static uint WNetAddConnection(string username, string password, string remoteName, string localName)
{
NetResource netResource = new NetResource();
netResource.dwScope = 2;                                    //RESOURCE_GLOBALNET
netResource.dwType = 1;                                     //RESOURCETYPE_DISK
netResource.dwDisplayType = 3;                              //RESOURCEDISPLAYTYPE_SHARE
netResource.dwUsage = 1;                                    //RESOURCEUSAGE_CONNECTABLE
netResource.lpLocalName = localName;
netResource.lpRemoteName = remoteName.TrimEnd('\\');
//netResource.lpRemoteName = lpComment;
//netResource.lpProvider = null;
uint result = WNetAddConnection2(netResource, password, username, 0);
return result;
}

/// <summary>
/// 释放存在的网络连接.
/// </summary>
/// <param name="name">System.String类型, 表示指定本地驱动器或远程共享资源. </param>
/// <param name="flags">System.Unit类型, 表示断开连接方式, 可设置0或CONNECT_UPDATE_PROFILE.</param>
/// <param name="force">System.Boolean类型, 表示是否强制断开连接. </param>
/// <returns>System.Unit类型, 如果方法执行成功返回0(NO_ERROR), 否则返回ERROR_CODE.</returns>
private static uint WNetCancelConnection(string name, uint flags, bool force)
{
uint nret = WNetCancelConnection2(name, flags, force);
return nret;
}

#endregion

#region CMD访问文件共享

/// <summary>
/// 打开给定的文件共享内容
/// </summary>
/// <param name="sourcePath">System.String类型, 表示给定的共享资源路径. </param>
/// <returns>System.Boolean类型, 表示是否成功打开共享资源. </returns>
private static bool openShareFolder(string sourcePath, string name, string pwd)
{
string cmdLineInfo = "net use " + sourcePath + " /User:\"" + name + "\" \"" + pwd + "\" /PERSISTENT:NO";
return executeCommandLine(cmdLineInfo);
}

public static bool ExplorerShareFolder(string sourcePath)
{
string cmdLineInfo = "explorer " + sourcePath;
return executeCommandLine(cmdLineInfo);
}

public static void ExplorerShareFolderByWinMsg(string sourcePath)
{
explorerShareFolder(sourcePath);
}

/// <summary>
/// 打开给定的文件共享内容
/// </summary>
/// <param name="sourcePath">System.String类型, 表示给定的共享资源路径. </param>
/// <returns>System.Boolean类型, 表示是否成功打开共享资源. </returns>
public static bool OpenShareFolder(string sourcePath)
{
return openShareFolder(sourcePath, _OuterDepSvnName, _OuterDepSvnPwd);
}

/// <summary>
/// 断开给定的文件共享内容
/// </summary>
/// <param name="sourcePath">System.String类型, 表示给定的共享资源路径. </param>
/// <returns>System.Boolean类型, 表示断开是否成功. </returns>
public static bool CloseShareFolder(string sourcePath)
{
return closeShareFolder(sourcePath, _OuterDepSvnName, _OuterDepSvnPwd);
}

/// <summary>
/// 断开给定的文件共享内容
/// </summary>
/// <param name="sourcePath">System.String类型, 表示给定的共享资源路径. </param>
/// <returns>System.Boolean类型, 表示断开是否成功. </returns>
private static bool closeShareFolder(string sourcePath, string name, string pwd)
{
//string cmdLineInfo = "net use " + sourcePath + " /User:\"" + name + "\" \"" + pwd + "\" /DELETE";
string cmdLineInfo = "net use " + sourcePath + " /DELETE";
return executeCommandLine(cmdLineInfo);
}

/// <summary>
/// 强制断开资源
/// </summary>
/// <param name="resourcePath">System.String类型, 表示资源的路径. </param>
private static void explorerShareFolder(string resourcePath)
{
string cmd = string.Format("explorer {0}", resourcePath);
WinExec(cmd, Convert.ToUInt16((int)ShowWindow.SW_HIDE));
}

/// <summary>
/// 在Command命令行窗口执行给定的指令.
/// </summary>
/// <param name="commandLineInfo">System.String类型, 表示将要在命令行窗口执行的指令. </param>
/// <returns>System.Boolean类型, 表示命令行是否成功执行. </returns>
private static bool executeCommandLine(string commandLineInfo)
{
bool result = false;
using (Process proc = getCommandLineProcess())
{
//try
//{
proc.Start();
proc.StandardInput.WriteLine(commandLineInfo);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
proc.WaitForExit();
string errorInfo = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errorInfo))
result = true;
//    else
//        throw new Exception(errorInfo);
//}
//catch (Exception ex)
//{
//    throw ex;
//}
}
return result;
}

/// <summary>
/// 获取CMD命令行对象
/// </summary>
/// <returns>System.Diagnostics.Process, 表示返回的CMD进程对象. </returns>
private static Process getCommandLineProcess()
{
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
return proc;
}

#endregion
}

/// <summary>
/// 通过多个字段指定网络连接资源信息.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
//dwScope: 指定枚举范围. 设置RESOURCE_CONNECTED,RESOURCE_GLOBALNET,RESOURCE_REMEMBERED三值之一.
public int dwScope;
//指定访问的资源类型. 设置三个值之一. RESOURCETYPE_ANY表示所有资源; RESOURCETYPE_DISK表示磁盘资; RESOURCETYPE_PRINT表示打印机.
public int dwType;
//指出资源显示类型. RESOURCEDISPLAYTYPE_DOMAIN;RESOURCEDISPLAYTYPE_SERVER;RESOURCEDISPLAYTYPE_SHARE;RESOURCEDISPLAYTYPE_GENERIC.
public int dwDisplayType;
//描述资源的使用方式. 设置RESOURCEUSAGE_CONNECTABLE或RESOURCEUSAGE_CONTAINER.
public int dwUsage;
//lpLocalName: 同dwScope关联, 指定本地映射.
public string lpLocalName;
//远程访问文件夹路径.
public string lpRemoteName;
//资源描述.
public string lpComment;
//资源提供者, 可以设置为NULL.
public string lpProvider;
}

public enum ShowWindow
{
SW_FORCEMINIMIZE = 0,
SW_HIDE = 1,
SW_MAXIMIZE = 2,
SW_MINIMIZE = 3,
SW_RESTORE = 4,
SW_SHOW = 5,
SW_SHOWDEFAULT = 6,
SW_SHOWMAXIMIZED = 7,
SW_SHOWMINIMIZED = 8,
SW_SHOWMINNOACTIVE = 9,
SW_SHOWNA = 10,
SW_SHOWNOACTIVATE = 11,
SW_SHOWNORMAL = 12

}
}


偷懒的地方太多, 仅仅为了实现功能, 考虑不周还望见谅!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: