.net-assembly - WinSCP .NET 程序集 - 无法使用绝对文件路径

标签 .net-assembly filepath winscp winscp-net

我正在尝试使用 WinSCP .NET 程序集 (V5.1.7.0) 进行 FTP 协议(protocol),但我在目标服务器上的文件路径方面遇到困难。

问题是与服务器的任何连接(使用 WinSCP 可执行文件或我自己的软件中的 .NET 程序集)似乎都会立即路由到用户的子目录(例如 home/zild),但文件需要上传到不同的目录(例如 home/test),用户对此具有足够的权限(通过 WinSCP 可执行文件手动执行所需的操作进行验证)。如果我尝试将远程路径设置为 home/test//home/test/ (绝对路径),它实际上使用相对路径(例如 home/zild/home/test) 在这两种情况下。我认为 .NET 程序集只使用过绝对路径,所以这是一个惊喜。不幸的是,我找不到一种方法来设置所需位置的路径(home/test)。

有人可以解释一下我在这里做错了什么吗?我已经尝试搜索文档和 WinSCP 论坛,但仍然卡住了。

编辑:附加信息

代码:

public bool Upload(string remoteDirectory, List<FileInfo> filesToUpload)
{
    bool success = false;

    try
    {
        using (Session session = new Session())
        {                    
            session.ExecutablePath = sessionExecutablePath;
            session.Open(sessionOptions);

            foreach (FileInfo fi in filesToUpload)
            {
                // fi.FullName: "XXX\\premiumparty_banner.gif"
                // remoteDirectory: "/home/storage/test/"
                TransferOperationResult transferResult = session.PutFiles(fi.FullName, remoteDirectory);
                transferResult.Check();
                if (transferResult.IsSuccess)
                {
                    success = true;
                }
                else
                {
                    // TO DO - write an error message (ideally for each failed item).
                }
            }
        }
    }
    catch
    {
    }
    return success;
}

我还没有使用过日志记录,但是如果我能做到这一点,我也会在这里发布日志。

我已经清空了本地计算机上的大部分文件路径,但请注意,它包含日语字符。

最后,请注意,我在使用 session.ListDirectory() 时也遇到了同样的问题。

EDIT2:日志文件

. 2013-11-18 17:26:33.064 
--------------------------------------------------------------------------
. 2013-11-18 17:26:33.064 WinSCP Version 5.1.7 (Build 3446) (OS 6.1.7601 Service Pack 1)
. 2013-11-18 17:26:33.064 Configuration: nul
. 2013-11-18 17:26:33.065 Local account: jk_lee-PC\jamie_h
. 2013-11-18 17:26:33.065 Working directory: C:\Program Files (x86)\WinSCP
. 2013-11-18 17:26:33.065 Process ID: 1156
. 2013-11-18 17:26:33.065 Command-line: "C:\Program Files (x86)\WinSCP\WinSCP.exe" /xmllog="C:\Users\jamie_h\AppData\Local\Temp\wscp12BC.00EB2115.tmp" /xmlgroups /nointeractiveinput /dotnet=517  /ini=nul /log="C:\Users\jamie_h\Desktop\恋愛イベントツールのテストファイル\log1.log"  /console /consoleinstance=_4796_348
. 2013-11-18 17:26:33.065 Time zone: Current: GMT+9, Standard: GMT+9, DST: GMT+10, DST Start: 1899/12/30, DST End: 1899/12/30
. 2013-11-18 17:26:33.065 Login time: 2013年11月18日 17:26:33
. 2013-11-18 17:26:33.065 --------------------------------------------------------------------------
. 2013-11-18 17:26:33.065 Session name: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="603820384e384e384e38" rel="noreferrer noopener nofollow">[email protected]</a> (Ad-Hoc session)
. 2013-11-18 17:26:33.065 Host name: X.X.X.X (Port: 21)
. 2013-11-18 17:26:33.065 User name: X (Password: Yes, Key file: No)
. 2013-11-18 17:26:33.065 Tunnel: No
. 2013-11-18 17:26:33.065 Transfer Protocol: FTP
. 2013-11-18 17:26:33.065 Ping type: C, Ping interval: 30 sec; Timeout: 15 sec
. 2013-11-18 17:26:33.065 Proxy: none
. 2013-11-18 17:26:33.065 FTP: FTPS: None; Passive: Yes [Force IP: A]; List all: A
. 2013-11-18 17:26:33.065 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2013-11-18 17:26:33.065 Cache directory changes: Yes, Permanent: Yes
. 2013-11-18 17:26:33.065 DST mode: 1; Timezone offset: 0h 0m
. 2013-11-18 17:26:33.065 --------------------------------------------------------------------------
. 2013-11-18 17:26:33.068 Connecting to X.X.X.X ...
. 2013-11-18 17:26:33.086 Connected with X.X.X.X. Waiting for welcome message...
< 2013-11-18 17:26:33.095 220 ProFTPD 1.3.3e Server (ProFTPD Default Installation) [::ffff:X.X.X.X]
> 2013-11-18 17:26:33.095 USER X
< 2013-11-18 17:26:33.105 331 Password required for X
> 2013-11-18 17:26:33.105 PASS ***********************
< 2013-11-18 17:26:33.116 230 User X logged in
> 2013-11-18 17:26:33.116 SYST
< 2013-11-18 17:26:33.124 215 UNIX Type: L8
> 2013-11-18 17:26:33.124 FEAT
< 2013-11-18 17:26:33.133 211-Features:
< 2013-11-18 17:26:33.133  MDTM
< 2013-11-18 17:26:33.143  MFMT
< 2013-11-18 17:26:33.143  LANG it-IT;zh-CN;ja-JP;fr-FR;zh-TW;ru-RU;ko-KR;en-US;bg-BG
< 2013-11-18 17:26:33.143  TVFS
< 2013-11-18 17:26:33.143  MFF modify;UNIX.group;UNIX.mode;
< 2013-11-18 17:26:33.143  MLST modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.mode*;UNIX.owner*;
< 2013-11-18 17:26:33.143  REST STREAM
< 2013-11-18 17:26:33.143  SIZE
< 2013-11-18 17:26:33.143 211 End
. 2013-11-18 17:26:33.143 Connected
. 2013-11-18 17:26:33.143 --------------------------------------------------------------------------
. 2013-11-18 17:26:33.143 Using FTP protocol.
. 2013-11-18 17:26:33.143 Doing startup conversation with host.
> 2013-11-18 17:26:33.143 PWD
< 2013-11-18 17:26:33.152 257 "/" is the current directory
. 2013-11-18 17:26:33.152 Getting current directory name.
. 2013-11-18 17:26:33.152 Startup conversation with host finished.
< 2013-11-18 17:26:33.153 Script: Active session: [1] <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="edb5adb5c3b5c3b5c3b5" rel="noreferrer noopener nofollow">[email protected]</a>
> 2013-11-18 17:26:33.293 Script: put  -nopermissions -preservetime -transfer="binary" -- "XXX\05_デザイン\スマフォ版\イベント\131101_クリスマス準備はパーティーで!?GACHA\premiumparty_banner.gif" "/home/storage/test/"
. 2013-11-18 17:26:33.294 Copying 1 files/directories to remote directory "/home/storage/test/"
. 2013-11-18 17:26:33.294   PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: 
. 2013-11-18 17:26:33.294   TM: B; ClAr: No; CPS: 0; InclM: 
. 2013-11-18 17:26:33.294   AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2013-11-18 17:26:33.296 File: "XXX\05_デザイン\スマフォ版\イベント\131101_クリスマス準備はパーティーで!?GACHA\premiumparty_banner.gif"
. 2013-11-18 17:26:33.305 Copying "XXX\05_デザイン\スマフォ版\イベント\131101_クリスマス準備はパーティーで!?GACHA\premiumparty_banner.gif" to remote directory started.
. 2013-11-18 17:26:33.306 Binary transfer mode selected.
. 2013-11-18 17:26:33.306 Starting upload of XXX\05_デザイン\スマフォ版\イベント\131101_クリスマス準備はパーティーで!?GACHA\premiumparty_banner.gif
> 2013-11-18 17:26:33.307 CWD /home/storage/test/
< 2013-11-18 17:26:33.315 250 CWD command successful
> 2013-11-18 17:26:33.315 PWD
< 2013-11-18 17:26:33.325 257 "/home/storage/test" is the current directory
> 2013-11-18 17:26:33.325 TYPE A
< 2013-11-18 17:26:33.333 200 Type set to A
> 2013-11-18 17:26:33.333 PASV
< 2013-11-18 17:26:33.342 227 Entering Passive Mode (X,X,X,X,234,80).
> 2013-11-18 17:26:33.342 MLSD
< 2013-11-18 17:26:33.358 150 Opening ASCII mode data connection for MLSD
. 2013-11-18 17:26:33.359 modify=20131114025545;perm=adfrw;size=115;type=file;unique=806U300826;UNIX.group=1000;UNIX.mode=0644;UNIX.owner=1000; カラーコード.txt
. 2013-11-18 17:26:33.359 modify=20131111065000;perm=adfrw;size=5904;type=file;unique=806U300825;UNIX.group=1000;UNIX.mode=0644;UNIX.owner=1000; premiumparty_button_gray.gif
. 2013-11-18 17:26:33.359 modify=20131118012640;perm=flcdmpe;type=pdir;unique=806U30208F;UNIX.group=1000;UNIX.mode=0755;UNIX.owner=1000; ..
. 2013-11-18 17:26:33.359 modify=20131111064838;perm=adfrw;size=9353;type=file;unique=806U300824;UNIX.group=1000;UNIX.mode=0644;UNIX.owner=1000; premiumparty_button.gif
. 2013-11-18 17:26:33.359 modify=20131113032811;perm=adfrw;size=14664;type=file;unique=806U3007A1;UNIX.group=1000;UNIX.mode=0644;UNIX.owner=1000; premiumparty_banner.gif
. 2013-11-18 17:26:33.359 modify=20131118012649;perm=flcdmpe;type=cdir;unique=806U302090;UNIX.group=1000;UNIX.mode=0755;UNIX.owner=1000; .
< 2013-11-18 17:26:33.368 226 Transfer complete
> 2013-11-18 17:26:33.371 TYPE I
< 2013-11-18 17:26:33.380 200 Type set to I
> 2013-11-18 17:26:33.380 PASV
< 2013-11-18 17:26:33.388 227 Entering Passive Mode (X,X,X,X,234,87).
> 2013-11-18 17:26:33.389 STOR premiumparty_banner.gif
< 2013-11-18 17:26:33.406 150 Opening BINARY mode data connection for premiumparty_banner.gif
< 2013-11-18 17:26:33.431 226 Transfer complete
> 2013-11-18 17:26:33.431 MFMT 20131113032811 premiumparty_banner.gif
< 2013-11-18 17:26:33.440 213 Modify=20131113032811; premiumparty_banner.gif
. 2013-11-18 17:26:33.440 Upload successful
> 2013-11-18 17:26:33.463 Script: exit
. 2013-11-18 17:26:33.463 Script: Exit code: 0
. 2013-11-18 17:26:33.464 Disconnected from server

2013-11-18 17:26:33.152 257“/”是当前目录似乎不正确...

最佳答案

从日志中可以清楚地看出, session 在 / 中开始,上传在 /home/storage/test 中进行。当您声称该文件实际上最终位于 /home/zild/home/storage/test 中时,这意味着您的 FTP session Root 于 /home/zild (注意该路径在日志中从未出现过)。

您在客户端对此无能为力。如果您需要访问 FTP 根目录之外的文件夹,请将您的 FTP 服务器配置为不建立 session ;或使用可以访问所需文件夹的不同帐户(test?)。

关于.net-assembly - WinSCP .NET 程序集 - 无法使用绝对文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19969033/

相关文章:

.NET Instrumentation - 调用自定义程序集失败 - COR PROFILER

c# - 加载常量的 MSIL 指令

c# - 使用 WinSCP Session.GetFile 从 SFTP 进行流式传输失败 – ((WinSCP.PipeStream)stream).Length' 引发类型 'System.NotSupportedException' 的异常

c# - 文件加载异常 : Could not load file or assembly (The assembly reference did not match the assembly definition found)

c# - 创建 Windows 窗体作为类库

netbeans - 指定相对文件路径

windows - 查找从 EXE 包装器启动的 powershell 脚本的完整路径

java - WSIT:JKS 相对文件路径

c# - WinSCP .NET 程序集 - 在 GetFiles 之后删除文件(不是目录)

synchronization - 使用 WinSCP 将今天的文件从远程目录同步到本地目录