2

I am creating a plugin for Revit that registers several events within its application.

For every time an event happens, a line is writen on a txt file telling me about the event such as:

The user opened a document on Autodesk Revit 2019 (...)

I am obtaining the "Autodesk Revit 2019" (name of application) by getting the name of the MainWindowTitle of the application like so: Process.GetCurrentProcess().MainWindowTitle

public static string originalString = Process.GetCurrentProcess().MainWindowTitle;

(...)

Trace.WriteLine("O utilizador " + Environment.UserName + " abriu o " + originalString + " a " + DateTime.Now + " (ApplicationInitializedEventArgs)");

Which writes in the txt file:

O utilizador rita.aguiar abriu o a 20/09/2018 10:36:42 (ApplicationInitializedEventArgs)

As you can read, it did not write on the txt file "Autodesk Revit 2019 - [Home]" between the words "o" and "a" as I hoped for.

If I had writen Process.GetCurrentProcess().MainWindowTitle directly on the Trace.WriteLine I would have obtained "Autodesk Revit 2019 - [Home]", but I wish to write an assigned name instead.

How to successfully write "Autodesk Revit 2019 - [Home]" by assigning a name to Process.GetCurrentProcess().MainWindowTitle?

Later I would like to obtain this name by instead getting just Autodesk Revit 2019 like so:

public static string originalString = Process.GetCurrentProcess().MainWindowTitle;

public static string[] splittedString = originalString.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

public static string AppName = splittedString[0];

Any help would be appretiated!

Rita Aguiar
  • 65
  • 1
  • 10

3 Answers3

3

As I suggested answering your similar question on assigning a name to a string C# in the Revit API discussion forum, I would look at the code executing step by step in the debugger.

Then you can see for yourself exactly what is going on.

You could also add some more intermediate lines and variables for absolute clarity:

string originalString = Process
  .GetCurrentProcess()
  .MainWindowTitle;

string s2 = "O utilizador " 
  + Environment.UserName 
  + " abriu um documento no " 
  + originalString + " a " + DateTime.Now;

//or use string interpolation: 
//https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
string s3 = $"O utilizador {Environment.UserName} abriu um documento no {originalString} a {DateTime.Now}";

Trace.WriteLine( s2 );
Trace.WriteLine( s3 );

The debugger is good!

Invaluable, in fact.

Felix D.
  • 4,811
  • 8
  • 38
  • 72
Jeremy Tammik
  • 7,333
  • 2
  • 12
  • 17
  • Thank you, I did not know about the interpolation of strings, I will try it too. – Rita Aguiar Sep 21 '18 at 09:59
  • Unfortunately I haven't been able to solve this problem yet. The issue, however, has to do with using the Process.GetCurrentProcess().MainWindowTitle which does not retrieve what I was expecting. – Rita Aguiar Oct 01 '18 at 15:36
  • Maybe The Building Coder examples making use of `JtWindowHandle` will help. They definitely access the main Revit window in pre-Revit-2019 releases. [In Revit 2019, things have changed](http://thebuildingcoder.typepad.com/blog/2017/10/modeless-form-keep-revit-focus-and-on-top.html#10). – Jeremy Tammik Oct 02 '18 at 13:43
1

Some of the comments on the discussion how to determine Revit demo mode show how you can access the Revit main window title.

Jeremy Tammik
  • 7,333
  • 2
  • 12
  • 17
0

To be able to store the Main Window Title in a name of type string I had to first declare each string outsite of the methods I am using:

string originalString;
string[] splittedString;
string AppName;

After declaring each string name I obtained the Application Name "Autodesk Revit 2019" by including each definition inside the first private method, which was created to register when the Revit application is opened. This had to be done inside a method because it is only after the application is launched that we can access the MainWindowTitle. This is the reason why I was getting an empty string "" when trying to obtain the MainWindowTitle the moment the application is starting to launch, but before it has completely launched and thus opened a Window with such Title.

private void DumpEventArgs(ApplicationInitializedEventArgs args_initialized) 
{
    originalString = Process.GetCurrentProcess().MainWindowTitle;
    splittedString = originalString.Split(new[] { " -" }, StringSplitOptions.RemoveEmptyEntries);
    AppName = splittedString[0];

    //StreamWriter file = new StreamWriter("C://Users//" + Environment.UserName + "//AppData//Roaming//Autodesk//" + Environment.UserName + ".txt", append: true);
    //MessageBox.Show($"O utilizador {Environment.UserName} iniciou o {AppName} a {DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}");
    file.WriteLine($"{Environment.UserName},{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")},{AppName},iniciar");
}

And I could use this same string later when required because it has been declared outside the method, for example here I needed to write AppName again:

private void DumpEventArgs(DocumentSavedEventArgs args_saved)
    {
        //StreamWriter file = new StreamWriter("C://Users//" + Environment.UserName + "//AppData//Roaming//Autodesk//" + Environment.UserName + ".txt", append: true);
        //MessageBox.Show($"O utilizador {Environment.UserName} guardou um documento no {AppName} a {DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}");
        file.WriteLine($"{Environment.UserName},{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")},{AppName},guardar");
    }

Finally AppName retrieves what I wanted: "Autodesk Revit 2019".

Rita Aguiar
  • 65
  • 1
  • 10