Get File Names - InfoPath Dev
in

InfoPath Dev

Get File Names

Last post 10-30-2010 02:33 PM by Potemkine. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 10-23-2010 04:11 PM

    • Lawson
    • Top 100 Contributor
    • Joined on 12-23-2009
    • Virginia
    • Posts 91

    Get File Names

    Hello,

    I have an IP 2003 form with C# code.

    I have an event where I would like to read all of the file names from a speceific file folder and then write the file names to a csv file.

    I am using the following code.

    DirectoryInfo di = new DirectoryInfo(@"C:\Users\Lawson\Desktop\file\file1\");foreach (FileInfo fi in di.GetFiles())

    {

    Console.WriteLine(fi.Name);

    }

    Console.ReadLine();

    string appendText = FileInfo;

    File.AppendAllText(@"C:\Users\Lawson\Desktop\file\output.csv", appendText);

    However, I cannot seem to get the file names into the csv document.

    Does anybody have any suggestions?

    Thanks,

    -Lawson


  • 10-24-2010 04:00 PM In reply to

    Hi,

    Lawson:
    string appendText = FileInfo;

    Here you use FileInfo like a variable, but you don't show us were you declare it. Here a sample of code to do what you need:

    string MyfileInfo=string.Empty;

    DirectoryInfo di = new DirectoryInfo(@"C:\Users\Lawson\Desktop\file\file1\");foreach (FileInfo fi in di.GetFiles())

    {

    MyfileInfo+=fi.Name;

    }

    File.AppendAllText(@"C:\Users\Lawson\Desktop\file\output.csv", MyfileInfo);

     

  • 10-24-2010 06:32 PM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-23-2009
    • Virginia
    • Posts 91

    Hey, thank you for your help.

    I tried using your code, but I get the following errors:

     Error 1 Invalid token 'foreach' in class, struct, or interface member declaration C:\Users\Lawson\Documents\InfoPath Projects\Hash Test Form 10-22-2010\FormCode.cs 112 17 Hash Test Form 10-22-2010
    Error 2 ; expected C:\Users\Lawson\Documents\InfoPath Projects\Hash Test Form 10-22-2010\FormCode.cs 112 38 Hash Test Form 10-22-2010
    Error 3 Invalid token '(' in class, struct, or interface member declaration C:\Users\Lawson\Documents\InfoPath Projects\Hash Test Form 10-22-2010\FormCode.cs 112 52 Hash Test Form 10-22-2010
    Error 4 Invalid token '+=' in class, struct, or interface member declaration C:\Users\Lawson\Documents\InfoPath Projects\Hash Test Form 10-22-2010\FormCode.cs 116 27 Hash Test Form 10-22-2010
    Error 5 Invalid token ';' in class, struct, or interface member declaration C:\Users\Lawson\Documents\InfoPath Projects\Hash Test Form 10-22-2010\FormCode.cs 116 36 Hash Test Form 10-22-2010
    Error 6 A namespace does not directly contain members such as fields or methods C:\Users\Lawson\Documents\InfoPath Projects\Hash Test Form 10-22-2010\FormCode.cs 120 17 Hash Test Form 10-22-2010

    Any suggestions?

     

  • 10-25-2010 07:58 AM In reply to

     

    Hi, I don't see any error in this codes are you sure you use C# ? try to zip you form with the codes and attach it. When you reply click on option and then atttach your file.
  • 10-25-2010 06:08 PM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-23-2009
    • Virginia
    • Posts 91

    Hey, thanks for your help. I found a stray bracket in the code. Once I removed it, everything worked perfectly.

    Thank you,

    -Lawson

     

  • 10-25-2010 06:26 PM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-23-2009
    • Virginia
    • Posts 91

    Now that I have the file names, I would also like to get a hash value for each document.

     I can get a hash value with the following code:

    // Open a local file

    IXMLDOMNode N = thisXDocument.DOM.selectSingleNode("/my:myFields/my:field1");

    //Get the text

    String HashDocument = N.text;

    FileStream fs = new FileStream(HashDocument, FileMode.Open);

    // Now generate a hash code for this file using MD5.

     

    HashAlgorithm alg = HashAlgorithm.Create("MD5");byte[] fileHashValue = alg.ComputeHash(fs);

     

     

    // Print out the generated hash code.

    string HashOutput = (BitConverter.ToString(fileHashValue));thisXDocument.DOM.selectSingleNode("/my:myFields/my:group2/my:field5").text = HashOutput;

    fs.Close();

     

    I haven't tried to unify this with the other code yet.

    Do you have any suggestions?

    Thanks,

    -Lawson

  • 10-29-2010 10:00 AM In reply to

    Hi, Lawson, sorry for the delay. What exactly you want to do?

  • 10-29-2010 07:07 PM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-23-2009
    • Virginia
    • Posts 91

    Hey, no worries.

    I would like to get the file names and corresponding hash values for each file, then I would like to place the names and hash values in a csv file.

    I can get the file names and the file hash values with the above code.

    Now I'm just trying to combine the two.

    Thanks, -Lawson

  • 10-30-2010 06:45 AM In reply to

     Ok, I elaborate it a little, I separate each file name by a semicolon";" and I delece the csv if it exist:

    MD5CryptoServiceProvider hashMd5 = new MD5CryptoServiceProvider();

                string MyfileInfo = string.Empty;

                DirectoryInfo di = new DirectoryInfo(@"C:\Users\Lawson\Desktop\file\file1\");
                foreach (FileInfo fi in di.GetFiles())
                {
                    byte[] passwordHash = hashMd5.ComputeHash(Encoding.Default.GetBytes(fi.Name));
                    MyfileInfo += Convert.ToBase64String(passwordHash) + ";";
                }
                if (File.Exists(@"C:\Users\Lawson\Desktop\file\file1\output.csv"))
                    File.Delete(@"C:\Users\Lawson\Desktop\file\file1\output.csv");
                File.AppendAllText(@"C:\Users\Lawson\Desktop\file\file1\output.csv", MyfileInfo);

  • 10-30-2010 08:54 AM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-23-2009
    • Virginia
    • Posts 91

    I used your code in following way:

    {

    IXMLDOMNode A = thisXDocument.DOM.selectSingleNode("/my:myFields/my:field1");

    //Get the text of the Contract Number node.

    String Data1 = A.text;

    IXMLDOMNode B = thisXDocument.DOM.selectSingleNode("/my:myFields/my:field2");

    //Get the text of the Contract Number node.

    String Data2 = B.text;

    IXMLDOMNode C = thisXDocument.DOM.selectSingleNode("/my:myFields/my:field3");

    //Get the text of the Contract Number node.

    String Data3 = C.text;

     

    string[] Datalines = { Data1, Data2, Data3 };

    string allLines = String.Join(",", Datalines, 0, 3) + Environment.NewLine;

    System.IO.File.WriteAllText(@"C:\Users\Lawson\Desktop\file\output.csv", allLines);

    string MyfileInfo = string.Empty;

    DirectoryInfo di = new DirectoryInfo(@"C:\Users\Lawson\Desktop\file\file1\");foreach (FileInfo fi in di.GetFiles())

    {

    byte[] passwordHash = hashMd5.ComputeHash(Encoding.Default.GetBytes(fi.Name));

    MyfileInfo += Convert.ToBase64String(passwordHash) + Environment.NewLine;File.AppendAllText(@"C:\Users\Lawson\Desktop\file\file1\output.csv", MyfileInfo);

    }

     

     

     

     

     

     

    }

     

    }

    }

     

    I get the following errors:

    Error 1 The name 'hashMd5' does not exist in the current context C:\Users\Lawson\Documents\InfoPath Projects\Hash Test Form 10-22-2010\FormCode.cs 112 39 Hash Test Form 10-22-2010
    Error 2 The name 'Encoding' does not exist in the current context C:\Users\Lawson\Documents\InfoPath Projects\Hash Test Form 10-22-2010\FormCode.cs 112 59 Hash Test Form 10-22-2010

    Any Suggestions?

  • 10-30-2010 09:30 AM In reply to

    Have you add these namespaces?

    using System.Security.Cryptography;
    using System.Text;

  • 10-30-2010 09:46 AM In reply to

    • Lawson
    • Top 100 Contributor
    • Joined on 12-23-2009
    • Virginia
    • Posts 91

    I'm using the following namespaces:

    using System;

    using Microsoft.Office.Interop.InfoPath.SemiTrust;

    using System.Collections.Generic;

    using System.IO;

    using System.Security;

    using System.Runtime.InteropServices;

    using System.Diagnostics.CodeAnalysis;

    using System.Security.Cryptography;

    using System.Text;

     

     

    I still get the following error:

    Error 1 The name 'hashMd5' does not exist in the current context C:\Users\Lawson\Documents\InfoPath Projects\Hash Test Form 10-22-2010\FormCode.cs 67 39 Hash Test Form 10-22-2010

  • 10-30-2010 02:33 PM In reply to

     hi, you probably miss this line on my codes:

    MD5CryptoServiceProvider hashMd5 = new MD5CryptoServiceProvider();

Page 1 of 1 (13 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.