简体   繁体   English

WinSCP .NET程序集 - 如何在创建目录后设置文件夹权限?

[英]WinSCP .NET assembly - How to set folder permissions after creating directory?

I'm building a web site and I want that when a user registers, to create a directory on the SFTP server and put in that directory a new file 我正在构建一个网站,我希望当用户注册时,在SFTP服务器上创建一个目录并在该目录中放入一个新文件

I'm using WinSCP .NET assembly, and writing C#. 我正在使用WinSCP .NET程序集,并编写C#。

I noticed that you are able to set permissions only in the method: Session.PutFiles and not in the method: Session.CreateDirectory 我注意到你只能在方法中设置权限: Session.PutFiles而不是方法: Session.CreateDirectory

Snd so after I create the directory and put the file in it, I cannot access the file because I don't have permissions - I'm accessing the file with the full URL 在我创建目录并将文件放入其中之后,我无法访问该文件,因为我没有权限 - 我正在使用完整的URL访问该文件

How can I access the file? 我该如何访问该文件?

PS. PS。 When I change the directory permissions manually, I am able to access the file. 当我手动更改目录权限时,我可以访问该文件。

Note that this answers your question how to set permissions when creating a directory. 请注意,这将回答您在创建目录时如何设置权限的问题。 But a root cause of your problem is that a default permissions your server sets are wrong. 但问题的根本原因是服务器设置的默认权限是错误的。 The server should not use default permissions such that you cannot access a directory/file you have just created yourself! 服务器不应使用默认权限,以便您无法访问自己刚刚创建的目录/文件!

It's currently not possible to directly set permissions, when a creating directory or modify them afterwards with WinSCP .NET assembly. 当创建目录或之后使用WinSCP .NET程序集修改目录时,目前无法直接设置权限。
See https://winscp.net/tracker/1075 请参阅https://winscp.net/tracker/1075

You can hack it though as follows: 你可以破解它如下:

string directoryName = "mydir";
string directoryPath = "/home/username/" + directoryName;
string tempPath = Path.Combine(Path.GetTempPath(), directoryName);

Directory.CreateDirectory(tempPath);

try
{
    TransferOptions options = new TransferOptions();
    options.FilePermissions = new FilePermissions { Octal = "755" };
    session.PutFiles(tempPath, directoryPath, false, options).Check();
}
finally
{
    Directory.Delete(tempPath);
}

You can even do without creating an empty temporary directory. 您甚至可以不创建空的临时目录。 Just pick any directory, eg directory of your account profile folder, and use a file mask to include only this one directory, preventing files in the directory and sub-directories from being uploaded. 只需选择任何目录,例如您的帐户配置文件文件夹的目录,并使用文件掩码仅包含此目录,从而防止目录和子目录中的文件被上载。 Also use an explicit name of desired remote directory in the target path to "rename" the uploaded directory to the name you want. 还要在目标路径中使用所需远程目录的显式名称,以将上载的目录“重命名”为所需的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM