iT邦幫忙

0

C# sftp讀取檔案permission denied

c#
  • 分享至 

  • twitterImage

請問一下為什麼這樣寫執行到openread那一行後都會顯示權限不足呢?
我上網翻了許久都翻不到,請求大神幫助,感謝您。

https://ithelp.ithome.com.tw/upload/images/20200217/20108649B0DzHsTxdT.jpg

using System;
using Renci.SshNet;
using Renci.SshNet.Sftp;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Xml;


//連接SFTP之功能
namespace ConsoleApp1
{
    class Program 
    {
        private static string _host = "";
        private static string _username = "";
        private static string _password = "";
        static void Main(string[] args)
        {
            
            var sftp = new SftpClient(_host, _username, _password);
            try
            {
                sftp.Connect();
                if (sftp != null)
                {
                    string a = collectDoc(sftp);
                    if (a != "f")
                    {
                        var doc = new XmlDocument();
                        doc.Load(sftp.OpenRead(a));
                        PackDocument pD = new PackDocument(a,doc);
                    }
                    else
                    {
                        
                    }
                    
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                if (sftp != null || sftp.IsConnected)
                {
                    sftp.Disconnect();
                }
            }
        }

       
        //取得所有檔案
        private static string collectDoc(SftpClient sftp) 
        {
            string errorMessage = "";
            List<string> fileList = new List<string>();
            //using(sftp)
            //{
                Action<SftpFile> ShowDirOrFile = (item) =>
                {
                    if (item.IsDirectory)
                    {
                        Console.WriteLine($"[{item.Name}]");
                    }
                    else
                    {
                        Console.WriteLine($"[{item.Name:-32}{item.Length,8:N0}]");
                    }
                };

                Action<string> Dir = (path) =>
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine($"Directory of {path}: \n");
                    var list = sftp.ListDirectory(path).Where(o => !".,..".Split(',').Contains(o.Name)).ToList();
                    if (list.Any())
                    {
                        list.ForEach(ShowDirOrFile);
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        Console.WriteLine("NO files.");
                    }
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.White;
                };
                Dir("/");                
                var listDirectory = sftp.ListDirectory("/");
                try
                {
                    foreach (var i in listDirectory)
                    {
                        string xmlName = i.Name;
                        return xmlName;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return "f";
                }
                return "f";
            //}      
        }
    }
}

看更多先前的討論...收起先前的討論...
Zed_Yang iT邦新手 3 級 ‧ 2020-02-17 11:26:07 檢舉
如果是直接用VS DEBUG模式下執行
是否有先以系統管理員身分開啟VS?
tryit iT邦研究生 4 級 ‧ 2020-02-17 11:32:32 檢舉
有喔~
優悠 iT邦新手 3 級 ‧ 2020-02-17 14:39:59 檢舉
給你黑暗大的範例做參考,非正解
https://blog.darkthread.net/blog/sftp-with-csharp/
tryit iT邦研究生 4 級 ‧ 2020-02-17 15:12:15 檢舉
很多都是參考他的喔~
但是為了後面方便作業,所以不能完全照抄
不知道這個對大大有沒有幫助
https://stackoverflow.com/questions/32878179/unable-to-upload-a-file-sftp-using-ssh-net-in-c-sharp-permission-denied

意思大概是實體位置路徑錯誤
tryit iT邦研究生 4 級 ‧ 2020-02-18 10:01:39 檢舉
這篇我有翻到~
我後來找到錯誤的原因了,謝謝!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答