0

I am new in C# programming. I want to run my Window Form Application through command line with one argument which is file name to be open in my Application. How can I do the following validation related to argument passed to Application:

  1. Checking the argument string is valid file path (not a folder path)
  2. If the argument string is Relative path (like "~\xyz.pdf") then it will generate and validate the absolute path (like "C:\Downloads\xyz.pdf").
  3. If argument string is valid file path then file should be of specific extension (like file path is valid and it is PDF file only).
3
  • What have you tried, and where exactly are you stuck? Commented Aug 11, 2017 at 11:10
  • You're new to programming? Take this advice: google around and read documentation and learn from code samples. I'm sure if you google your 3 individual points you will find adequate solutions for each of them. Commented Aug 11, 2017 at 11:11
  • Some hints [Path.GetFullPath][1], [Path.IsPathRooted][2]. [1]: msdn.microsoft.com/en-us/library/… [2]: msdn.microsoft.com/en-us/library/system.io.path.ispathrooted Commented Aug 11, 2017 at 11:14

2 Answers 2

2
  1. File.Exists if you expect it to be an existing file, otherwise the only absolutely foolproof way is to attempt to create/open it.

  2. If it's a standard relative path (xyz.pdf), you can use Path.GetFullPath(Path.Combine(baseDirectory, path)). This will work if path is a relative or absolute path.

  3. Path.GetExtension

Sign up to request clarification or add additional context in comments.

Comments

0

Hmm, you could try something like:

public static void Test(string path){
    // If there is no File at the desired location
    if (File.Exists(path) == false){
    }

    // Get the FullPath
    string fullPath = Path.GetFullPath(path);

    // Check the Extension for .pdf
    if (path.EndsWith(".pdf")){

    }
    // Or you can do Path.GetExtension(path)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.