nishan
nishan

Reputation: 11

how to check whether a unix path is valid or not in unix using c#

i am transferring some file to unix using my dot net code.I am asking the user to enter the unix path where he wants to transfer the file. before transferring the file I want to check whether the path user has entered is valid or not.

 try
      {
                Process objProcess = new Process();
                objProcess.StartInfo.FileName = "putty.exe";
                objProcess.StartInfo.Arguments = txtUserName.Text.Trim() + "@" + cmbIP.Text.Trim() + " -pw " + txtPassword.Text.Trim() + " -m \"" + CodeSetFileNameTemp + "\"";
                objProcess.Start();

                objProcess.WaitForExit();

                SUCCEEDED.AppendLine(viewName);
                SELECTED.Remove(SELECTED.ToString().IndexOf(viewName), viewName.Length + 2);


            }
            catch (Exception ex)
            {
                FAILED.AppendLine(viewName);
                SELECTED.Remove(SELECTED.ToString().IndexOf(viewName), viewName.Length + 2);
                MessageBox.Show(ex.Message + ". Unable to connect UNIX", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

Upvotes: 1

Views: 696

Answers (4)

paxdiablo
paxdiablo

Reputation: 882596

Just transfer the file. If there is a problem, you'll get an error of some sort. There's any number of reasons why such a transfer would fail (network problem, bad path, incorrect permissions, insufficient space and so on) and no pre-checking of the entered path will help you out there. Even if you could validate the path, there's no guarantee that things won't change between validation and attempted transfer.

This is similar to those who want a valid email address regex, seemingly unaware that even a correctly formed address may be invalid simply because there no email account behind it.

UNIX allows a very wide range of characters in path names, even such heinous things as backspaces and other control characters. I wouldn't use them all myself but they are valid.

So, bottom line, best way to check if a target location can be used is to use it in the manner you desire.


Based on your comment that no error detection is available, I believe the best approach is to send the file over then read it back into a different file.

That's the only way you can be certain that the file has been delivered complete and correct. Every other option that springs to mind (including simply checking for a valid path) will leave some uncertainty.

For example, /home/pax is well and truly a valid path under UNIX yet may not even exist on th target box. Even if it does exist, the transfer may fail halfway through and you will have no indication of that, other than if you bring back the file and compare it to the original.

Upvotes: 1

Adrian
Adrian

Reputation: 2875

What method are you using to transfer the files to the Unix server? If you're mounting it as a network drive using Samba, you can use System.IO.Directory.Exists to determine if the path is valid or not. If it's something like FTP you can try to list the directory, and catch the exception for it not being there.

Upvotes: 1

Eric J.
Eric J.

Reputation: 150208

You can use Directory.Exists.

That will tell you if the directory entered actually exists. Beyond that, other things (permissions) can still go wrong.

Upvotes: 1

yogi
yogi

Reputation: 19619

I assume that you are taking a Directories path from your user than you can use this

Directory.Exists(path);

It will return a boolean value flags the existence of the path provided.

Upvotes: 0

Related Questions