Hi, I'm using a StreamReader that process an input file and generates an output file (both .txt) after querying Active Directory.
My application is working fine whenever I type in my own parameter for the filename (i.e. fileName = Console.ReadLine(); ), but the program does not create the output file whenever I try to drag a file (i.e. the input .txt file) onto my .exe file. I would like to be able to have this drag-and-drop capability, as some of the input filenames are very long.
Also, this is really weird, but if I put the absolute path to the input .txt file inside of my project properties (debug>StartOptions, Command line arguments) and run it that way, it works fine!
I don't know what is going on, but it's driving me crazy ;)
Any help would be most appreciated.
Thanks,
jeremytmunn
[email protected]
Loading
srinivasa TelukutlaPosted Jul 11, 2007, 1:32 PM
This could be the possible explanation:
-------------------------------------
The statement
sw = new StreamWriter( "FolderShareQuery.txt");
is supposed to create the file in the directly where the FolderShareQuery.exe is executed.
When you drag & drop the file on to the FolderShareQuery.exe the current directory is not set correctly. You can check it by printing System.IO.Directory.GetCurrentDirectory() to console.
I still need to figure out why current directory is not set to the folder where FolderShareQuery.exe exists when we drap&drop file onto the exe file.
Jeremy MunnPosted Jul 11, 2007, 1:13 PM
Thank you very much, it does indeed work.
What I did is just add the lines:
string fileDirectory = "";
...
fileDirectory = filePath.Substring(0,filePath.LastIndexOf(@"\")+1);
...
sw =
new StreamWriter(fileDirectory + "FolderShareQuery.txt");I don't exactly know why this solution works though...Is there any explanation why you should separate the path from the filename and then merge them together again when accessing the file?
Even if you don't have an answer to that question (it's just a bonus!), THank you very much for your time.
jeremytmunn
srinivasa TelukutlaPosted Jul 11, 2007, 11:50 AM
MODIFY YOUR CODE TO THE BELOW CODE AND IT WILL TAKE CARE OF YOUR PROBLEM
-----------------------------------------
class EntryPoint
{
static string delim = "\" "; static StreamWriter sw; static void Main(string[] args){
string filePath = ""; string fileDirectory = ""; // Determine whether or not an argument has been specified if (args.Length != 0){
// Has Argument so process query with specified argument as input pathfilePath = args[0].Trim();
fileDirectory = filePath.Substring(0,filePath.LastIndexOf(
@"\")+1);}
if (args.Length == 0){
// No Argument so prompt user for input path of file to process Console.WriteLine("No argument specified"); Console.Write("Please enter the name of the file to process: ");filePath =
Console.ReadLine();}
sw =
new StreamWriter(fileDirectory + "FolderShareQuery.txt");ProcessInputFile(filePath);
sw.Close();
Console.WriteLine("\r\rFile created successfully. Press any key to exit."); Console.ReadLine();}
Jeremy MunnPosted Jul 11, 2007, 9:50 AM
That's correct, the file is still not being created even if I comment those lines/leave those method calls out.
I keep thinking this has something to do with the way that Visual Studio is automatically handling the command line arguments, as it works great when I put the path in VS as a command line argument. I put it in as follows:
"C:\Documents and Settings\munnj\My Documents\Visual Studio 2005\Projects\FolderShareQuery\FolderShareQuery\bin\Debug\123.txt"
(with the double quotes)
Now, when I output this string (path), it gives me the path without quotes (which is the same whether I supply the command line arguments OR run the program by drag-and-drop)
So, if the string is the same regardless of which way that I'm running the app, there must be something that is happening when I'm running the app through VS, because this is when it works.
It also works whenever I don't drag-and-drop, like if I just double-click the .exe as any other app. Then I supply the input file and it works like a charm.
Does it have something to do with the way that I'm accessing args?
filePath = args[0];
I also tried .ToString() and that doesn't help at all so I'm running out of ideas!
Well, I would send source code, but the input file is a listing of permissions from our Active Directory and the output file lists the users in those groups, so it would compromise security.
http://home.mchsi.com/~j.munn/FolderShareQuery.zip
Ok, cross that, I did upload a .zip file to my web site so that you guys can see the source. If you're not on a domain (or even if you are for that matter) you won't get the desired output, but it should at least create the output file, which is what I'm trying to do.
I've tried this approach also, like trying to creat a method named HelloWorld() that just prints out a the line "hello, world!" to the output stream, but this doesn't work with drag-drop :)
Hopefully you all will have better luck than I!
BTW, the input file does have a line or two of the actual input.txt file, but I've left out the rest for secuirty reasons.
Thanks all for your help
jeremytmunn
Jan MontanoPosted Jul 10, 2007, 10:16 PM
I get your point. The behavior is odd. You should have encountered no problem with that. I copy/paste your code with 2 modifications for it to compile...
commented //static string delim = "\" ";
commented //ProcessInputFile(filePath);. I don't know the function.
So fardrag-drop works. It creates the file in my \Documents And Settings\Username folder.
I also put the input file in a path with characters equal to your path and also directories equal to the number of directories in your path just in case it's the one causing the problem. I also have spaces in my input file.
The only thing I can think of is it has something to do with your ProcessInputFile method. If you comment that call, is the file still not created?
Cheers,
Jan
Mike GoldPosted Jul 10, 2007, 7:02 PM
Jeremy MunnPosted Jul 10, 2007, 3:31 PM
NALNAS1.txt
and the path is as follows:
C:\Documents and Settings\munnj\My Documents\Visual Studio 2005\Projects\FolderShareQuery\FolderShareQuery\bin\Debug
So, there are spaces in the path to the file, but I know that the path is working correctly as my program does give me correct output (I'm also outputting to the console). What the program isn't doing correctly is outputting the .txt file (this will hold the same output as the console)
Like I said in my previous posts, everything works correctly except when I drag my .txt (input) file onto my .exe that I'm creating... It doesn't create the .txt (output) file.
Sorry if this doesn't make much since, I'm trying to describe my problem as best as I can. The only example that I can relate to the feature that I'm trying to incorporate here would be something like when you drag-and-drop a .txt file onto notepad.exe and it opens up notepad with the contents of the .txt
Thanks for all of your help
jeremytmunn
JordiPosted Jul 10, 2007, 2:57 PM
Jeremy MunnPosted Jul 10, 2007, 1:21 PM
Thanks for the reply.
Yeah, I thought about using a Windows Form Application, but for just opening a file, reading it, and then creating a new file after querying AD, I thought that a simple Console App would do... And I'm so close!
The only quirk is with the drag and drop, which I really don't understand why... I will post my Main method below and maybe you could comment on it!? Thanks again.
jeremytmunn
class
EntryPoint{
static string delim = "\" "; static StreamWriter sw = new StreamWriter("FolderShareQuery.txt"); static void Main(string[] args){
string filePath = ""; // Determine whether or not an argument has been specified if (args.Length != 0){
// Has Argument so process query with specified argument as input pathfilePath = args[0].ToString();
Console.WriteLine(filePath);}
else{
// No Argument so prompt user for input path of file to process Console.WriteLine("No argument specified"); Console.Write("Please enter the name of the file to process: ");filePath =
Console.ReadLine();}
ProcessInputFile(filePath);
sw.Close();
Console.WriteLine("\r\rFile created successfully. Press any key to exit."); Console.ReadLine();}
.
.
.
}
Mike GoldPosted Jul 10, 2007, 1:08 PM
If you are doing drag and drop, you are probably better off doing a Windows Form Application. This way you can drag files from the explorer into a control on your windows form and capture it there.