您的位置:首页 > 编程语言 > ASP

asp.net实现创建映射,发送文件,删除映射

2007-12-17 12:41 495 查看
前台页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <INPUT class="inputnormal" id="txtDown" style="WIDTH: 400px; HEIGHT: 24px" type="file"
                          size="47" name="smlPic" runat="server">
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>
后台文件:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Diagnostics;

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.CreateNoWindow = true;
        try
        {
            p.Start();
            p.StandardInput.WriteLine("net   use h:   ////IP//共享文件夹名称/"密码/" /user:用户名");//创建映射
            p.StandardInput.WriteLine("exit");
            p.WaitForExit();
            p.Close();
            string shortSmlPicFileName = "";
            string smlPicFileName = "";
            if (txtDown.PostedFile.FileName.Trim() != "")
            {
                shortSmlPicFileName = GetUniqueFileName(txtDown.PostedFile.FileName);
                smlPicFileName = "h:/" + shortSmlPicFileName;
                UpLoadPic(txtDown, smlPicFileName);
            }
            string vSmlPicFileName = (shortSmlPicFileName == "") ? "" : "h:/" + shortSmlPicFileName;
        }
        catch (Exception ex)
        {

        }
        finally
        {
            p.Start();
            p.StandardInput.WriteLine("net use h: /del");//删除映射
            p.StandardInput.WriteLine("exit");
            p.WaitForExit();
            p.Close();
        }
    }
 private string GetUniqueFileName(string fileName)
 {
  string newFileName = Guid.NewGuid().ToString();
  string ExtName = Path.GetExtension(fileName);
  return newFileName + ExtName;
 }
 private void UpLoadPic(HtmlInputFile inputFile, string fileName)
 {
  if (inputFile.PostedFile != null)
  {
            inputFile.PostedFile.SaveAs(fileName);
  }
 }
}
注意:共享文件夹要有写的权限

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息