WinSCP .NET程序集 - 创建目录后如何设置文件夹权限? [英] WinSCP .NET assembly - How to set folder permissions after creating directory?
问题描述
我正在使用WinSCP .NET程序集,并编写C#。
我注意到您只能在以下方法中设置权限: Session。 PutFiles
而不是在方法中: Session.CreateDirectory
Snd因此,在创建目录并将文件放入其中后,我无法访问该文件,因为我没有权限 - 我正在使用完整的URL访问该文件
如何访问文件?
PS。
当我手动更改目录权限时,我可以访问该文件。
请注意这回答了您在创建目录时如何设置权限的问题。但是您的问题的根本原因是您的服务器集合的默认权限是错误的。服务器不应该使用默认的权限,这样您就无法访问刚刚创建的目录/文件!
目前无法直接设置权限,当创建目录或随后用WinSCP .NET程序集修改它们。
请参阅 https://winscp.net/tracker/show_bug.cgi?id=1075
您可以通过以下方式将其删除:
- 创建一个本地空的临时目录
- 使用
Session.PutFiles
,在TransferOptions.FilePermissions
string directoryName =mydi R;
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);
}
甚至可以创建一个空的临时目录。只要选择任何目录,例如目录,并使用文件掩码仅包含这一个目录,防止目录和子目录中的文件被上传。还可以使用目标路径中所需远程目录的显式名称,将已上传的目录重命名到所需的名称。
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
I'm using WinSCP .NET assembly, and writing C#.
I noticed that you are able to set permissions only in the method: Session.PutFiles
and not in the method: 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
How can I access the file?
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.
See https://winscp.net/tracker/show_bug.cgi?id=1075
You can hack it though as follows:
- Create a local empty temporary directory
- Upload it using the
Session.PutFiles
, setting permissions you need inTransferOptions.FilePermissions
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, e.g. 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.
这篇关于WinSCP .NET程序集 - 创建目录后如何设置文件夹权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!