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

南楼画角 提交于 2019-12-23 04:01:08

问题


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.


回答1:


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/1075

You can hack it though as follows:

  • Create a local empty temporary directory
  • Upload it using the Session.PutFiles, setting permissions you need in TransferOptions.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.



来源:https://stackoverflow.com/questions/24726700/winscp-net-assembly-how-to-set-folder-permissions-after-creating-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!