使用asp.net mvc将文件保存到网络文件夹

时间:2015-10-21 04:26:42

标签: c# asp.net

我正在尝试将文件保存到服务器上的文件夹中。我在Cassini-Local Dev Server上本地运行应用程序,并将文件保存在IIS上托管应用程序的服务器上。该文件夹允许网络服务创建/读/写文件到该文件夹​​。

  string destinationPath = string.Format("{0}\\{1}\\{2}\\{3}\\", @"\\server\G:\xyz\xyStore\uploads\Files\", uploadDate.ToString("yyyy"), uploadDate.ToString("MMM"), uploadDate.ToString("dd"));

 if (!Directory.Exists(destinationPath))
                            Directory.CreateDirectory(destinationPath);

                        //Save File To Folder
                        string storedFileName = string.Format("{0}{1}.{2}", destinationPath, System.Guid.NewGuid(), Path.GetExtension(file.FileName).ToUpper());
                        file.SaveAs(storedFileName);
                        miscFile.FileStoredName = storedFileName;

我一直在收到错误

  

不支持给定路径的格式

1 个答案:

答案 0 :(得分:0)

首先尝试连接您的网络文件夹。我们有类似于您的需求

    public void ConnectToUnc(string remoteUnc, string userName, string passwrod)
    {
        NETRESOURCE nr = new NETRESOURCE();
        // Disk
        nr.dwType = 0x00000001;
        nr.lpRemoteName = remoteUnc;

        // Try to connect to remote UNC (non-interactive, withod drive mapping)
        int ret = WNetUseConnection(IntPtr.Zero, nr, passwrod, userName, 0, null, null, null);
        if (ret != 0)
        {
            throw new Exception("Error connecting to UNC: " + ret);
        }
        var files = Directory.GetFiles(remoteUnc);
    }

导入Windows API

    [DllImport("Mpr.dll")]
    private static extern int WNetUseConnection(
        IntPtr hwndOwner,
        NETRESOURCE lpNetResource,
        string lpPassword,
        string lpUserID,
        int dwFlags,
        string lpAccessName,
        string lpBufferSize,
        string lpResult
        );


    [StructLayout(LayoutKind.Sequential)]
    private class NETRESOURCE
    {
        public int dwScope = 0;
        public int dwType = 0;
        public int dwDisplayType = 0;
        public int dwUsage = 0;
        public string lpLocalName = "";
        public string lpRemoteName = "";
        public string lpComment = "";
        public string lpProvider = "";
    }

Link to MSDN