asp.net 操作共用目錄檔案

來源:互聯網
上載者:User

標籤:

背景:

伺服器A為程式伺服器,伺服器B為檔案伺服器。伺服器A的程式需要修改刪除伺服器B的檔案。

實現方式:採用虛擬目錄映射

操作步驟:

1.在伺服器A與伺服器B建立相同帳號和密碼的windows使用者 如:windows使用者名稱:abc 密碼:123

2.給伺服器B的檔案分享權限設定目錄授予Windows使用者abc問你件系統和共用讀寫的許可權

3.在IIS網站下建立虛擬目錄Share (Share為虛擬目錄名字,自己取),實體路徑指向伺服器B的檔案分享權限設定目錄。

4.在網站設定檔web.config中添加配置

  <system.web>    <identity impersonate="true" userName="abc" password="123"/>  </system.web>

5. 操作步驟到此結束

測試:

測試代碼

 1         /// <summary> 2         /// 刪除虛擬(或相對路徑)目錄下的檔案 3         /// </summary> 4         /// <param name="path">檔案相對路徑</param> 5         /// <param name="logsPath">日誌路徑</param> 6         /// <returns></returns> 7         [WebMethod] 8         public bool DeleteShareFileByPath(string path, string logsPath) 9         {10             try11             {12                 System.IO.File.Delete(Server.MapPath(path));13                 Logger.Write("刪除檔案【" + Server.MapPath(path) + "】", "", logsPath);14                 return true;15             }16             catch (Exception ex)17             {18                 Logger.Write("刪除檔案【" + Server.MapPath(path) + "】出錯:" + ex.Message, "", logsPath);19             }20             return false;21         }

調用DeleteShareFileByPath方法:

結果:

 

asp.net 操作共用目錄檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.