Announcement

Collapse
No announcement yet.

Assign a name to a string C# (Process.GetCurrentProcess().MainWindowTitle)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Assign a name to a string C# (Process.GetCurrentProcess().MainWindowTitle)

    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
    I would like to attribute a name to this string (although I know that this is not exacly a string, but that it gets some characters that spell words).

    I have been trying:

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

    (...)

    Trace.WriteLine("O utilizador " + Environment.UserName + " abriu um documento no " + originalString + " a " + DateTime.Now + " (DocumentOpenedEventArgs)");

    Which writes in the txt file:

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

    See attached pngs for more information.
    As you can read, it did not write on the txt file "Autodesk Revit 2019 - [Home]" 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 insted 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!

    Attached Files

    #2
    Glad you switched to C#
    What do you mean by assigning a name?
    Process already has a ProcessName property.
    As for the remaining info, you should get from the UIControlledApplication properties and the rest by subscribing to events.

    Also:

    DON'T

    var splittedString = originalString.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

    DO

    var splittedString = originalString.Split(new []{'-'}, StringSplitOptions.RemoveEmptyEntries);

    or

    var splittedString = originalString.Split('-');
    Gonçalo Feio
    "Ignorance, ignorance, sheer ignorance - you know there's no confidence to equal it. It's only when you know something about a profession, I think, that you're timid and careful." George Orson Welles

    Comment


      #3
      Thank you for your help @feio.

      By assigning a name I mean writing:

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

      Trace.WriteLine("O utilizador " + Environment.UserName + " abriu um documento no " + originalString + " a " + DateTime.Now);

      instead of writing:

      Trace.WriteLine("O utilizador " + Environment.UserName + " abriu um documento no " + Process.GetCurrentProcess().MainWindowTitle + " a " + DateTime.Now);

      The issue is that by doing the first option, what is being written in the .txt file is:

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

      ( which is missing Autodesk Revit 2019 - [Home] ) instead of:

      O utilizador rita.aguiar abriu o Autodesk Revit 2019 - [Home] a 20/09/2018 10:36:42

      Later I would like to just write:

      O utilizador rita.aguiar abriu o Autodesk Revit 2019 a 20/09/2018 10:36:42



      Comment


        #4
        IMHO you should forget using System.Diagnostics.Process.
        Your know it's Revit and from UIControlledApplication you get the ControlledApplication with lots of members.
        That's where you should start looking for the info and events you seem to be interested in.
        You should also do your logging in a some other way (xml, json? or write to a DB) that can be read and managed more easily.
        Gonçalo Feio
        "Ignorance, ignorance, sheer ignorance - you know there's no confidence to equal it. It's only when you know something about a profession, I think, that you're timid and careful." George Orson Welles

        Comment


          #5
          Hi @feio.
          Thank you for your help. I am trying to get the Revit name from UIControlledApplication, however, I have tried writing:

          public static string originalString = UIControlledApplication.ControlledApplication.Prod uct;

          but I always get the error: An object reference is required for the non-static field,method, or property 'UIControlledApplication.ControlledApplication".

          I just wanted to get the current application name.

          What am I missing?
          Last edited by ritaaguiar; September 25, 2018, 09:28 AM.

          Comment


            #6
            You are pointing to nonexistent static properties.

            In your IExternalApplication implementation try this:

            Code:
            public Result OnStartup(UIControlledApplication application)
            {
                var controlledApp = application.ControlledApplication;
                var product = controlledApp.Product;
                return Result.Succeeded;
            }
            Gonçalo Feio
            "Ignorance, ignorance, sheer ignorance - you know there's no confidence to equal it. It's only when you know something about a profession, I think, that you're timid and careful." George Orson Welles

            Comment

            Related Topics

            Collapse

            Working...
            X