0

Essentially on form load I want a basic VB input box to prompt the user for input in the form of a string (a servername, like MYSERVER01). As of now I am accomplishing like:

        //Get server name from user
        string serverName = Microsoft.VisualBasic.Interaction.InputBox("Enter the name of the server hosting the share (without '\\\\')", "File copy from Server", "", -1, -1);
        string remotePath = @"\\" + serverName + @"\" + "Share";

However, I need remotePath to be available throughout the the entire project, my question is, where I define it, bearing in mind I only want the messagebox to prompt the user once for the servername.

PnP
  • 3,133
  • 17
  • 62
  • 95
  • 4
    You want a static property. – SLaks Sep 16 '13 at 19:11
  • You want a different way to solve this. Either change the working directory and access files by relative path, or use a class that abstracts that, which can use the path in an instance variable. – CodeCaster Sep 16 '13 at 19:14

2 Answers2

0

You can do it in this way;

public static class SomeClass
{
    //Get server name from user
    string serverName = Microsoft.VisualBasic.Interaction.InputBox("Enter the name of the server hosting the share (without '\\\\')", "File copy from Server", "", -1, -1);
    private static string remotePath = @"\\" + serverName + @"\" + "Share";

    public static string RemotePath
    {
        get
        {
            return remotePath;
        }

        set
        {
            remotePath = value;
        }
    }
}

This way you can access remotePath throughout the application.To access remotePath from other classes use SomeClass.RemotePath,it's a get and set property so you can change it later on if you need.

devavx
  • 1,035
  • 9
  • 22
0

It seems like you are talking about program settings. The standard place to store them is .settings file. Storing settings is as easy as

Settings.Default["ServerName"] = "MYSERVER01";

Take a look at this answer for more details https://stackoverflow.com/a/453230/731793. Also read the official documentation http://msdn.microsoft.com/en-us/library/0zszyc6e.aspx

Community
  • 1
  • 1
Pavel Murygin
  • 2,242
  • 2
  • 18
  • 26