sshnet / sshnet/SSH.NET

Can't delete file right after creating them

Open
#394 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
4.4k
Forks
993
Avg merge
9d 21h
Merged PRs (30d)
1

Description

I am wondering if this is an issue or my process flaw. I am not be able to delete a file that just created. But, on WinSCP, I am able to delete them manually.

There is a case where the file got locked and I can't delete them because it says that "Permission Denied" and I can't delete them as SFTP user or OS (windows) user. I did checked that the file is owned by Administrator.

Here is the code that I use to produce the problem.

private static void uploadZip(string fTPAddress, string ftpPath, string fTPUsername, string fTPPassword, string zipFilePath, string zipFileName) {
	// Build username/password
	var passwordAuth = new PasswordAuthenticationMethod(fTPUsername, fTPPassword);

	// Build connection
	var connectionInfo = new ConnectionInfo(fTPAddress, fTPUsername, passwordAuth);

	using (var client = new SftpClient(connectionInfo)) {
		Console.WriteLine("Connecting " + fTPAddress + "... ");
		client.Connect();
		Console.WriteLine(client.IsConnected ? "Connected!" : "Disconnected.");

		// Upload file
		try {
			Console.WriteLine("Creating new path + file...");
			client.Create(ftpPath + zipFileName);
			Console.WriteLine("Created!");

			if (client.Exists(ftpPath + zipFileName)) {
				Console.WriteLine(ftpPath + zipFileName + " Exists!");
				Console.WriteLine("Deleting " + ftpPath + zipFileName + "... ");
				client.Delete(ftpPath + zipFileName);
				Console.WriteLine("Deleted");
			} else {
				Console.WriteLine("Uploading file...");
				using (FileStream stream = File.Open(zipFilePath, FileMode.Open)) {
					client.UploadFile(stream, ftpPath + zipFileName);
				}
				Console.WriteLine("Uploaded!");
			}
		} catch (Exception e) {
			Console.WriteLine("-- Something wrong on execution: " + e.Message);
			Console.WriteLine(e.StackTrace);
		} finally {
			Console.WriteLine("Disconnecting... ");
			client.Disconnect();
			Console.WriteLine(client.IsConnected ? "Connected!" : "Disconnected.");

			Console.WriteLine("Deleting " + zipFilePath + "...");
			File.Delete(zipFilePath);
			Console.WriteLine(zipFilePath + " Deleted...");
		}
	}
}

The output:
C:\Users\btobias>C:\WorkingCopies\PCCDBAssembly\PCCDB.Docs.Run\bin\Dev\PCCDB.Docs.Run.exe
( 0 milliseconds) Type found: PCCDB.PCC.Library.LibraryPatronDump
( 0 milliseconds) Method found: LibraryPatronDumpExe
( 1 milliseconds) Parameter 0: ftpAddress (System.String): ftp.test.com
( 1 milliseconds) Parameter 1: ftpPath (System.String): /alma/test/
( 1 milliseconds) Parameter 2: ftpUsername (System.String): XXXXX
( 2 milliseconds) Parameter 3: ftpPassword (System.String): XXXXXXXXXXXXXX
( 2 milliseconds) Parameter 4: syncPersonnelIncluded (System.String): Y
( 3 milliseconds) Executing...
( 9292 milliseconds) Connecting ftp.pcci.edu...
( 9528 milliseconds) Connected!
( 9528 milliseconds) Creating new path + file...
( 9539 milliseconds) Created!
( 9546 milliseconds) /alma/test/PatronDump2.zip Exists!
( 9546 milliseconds) Deleting /alma/test/PatronDump2.zip...
( 9553 milliseconds) -- Something wrong on execution: Permission denied
( 9555 milliseconds) at Renci.SshNet.Sftp.SftpSession.RequestRemove(String path)
at PCCDB.PCC.Library.LibraryPatronDump.uploadZip(String fTPAddress, String ftpPath, String fTPUsername, String fTPPassword, String zipFilePath, String zipFileName) in C:\WorkingCopies\PCCDBAssembly\PCCDB.Docs\PCC\Library\LibraryPatronDump.cs:line 94

( 9556 milliseconds) Disconnecting...
( 9607 milliseconds) Disconnected.
( 9607 milliseconds) Deleting C:\Temp\PatronDump2.zip...
( 9609 milliseconds) C:\Temp\PatronDump2.zip Deleted...
( 9609 milliseconds) Finished...
( 9610 milliseconds)
Press any key...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported SftpClient.Create and client.Delete sequence, then inspect the SftpSession.RequestRemove call shown in the stack trace. Reproduce the permission-denied result and compare it with the server and Windows ownership details; done means establishing whether SSH.NET or the server permissions are responsible, with a focused regression test only if the library is at fault.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.