|
Well it was a machine environment variable since the path was going to be the same across all users. And you are right Application.StartupPath does work correctly but here is where the problems started. I was using a dll. I wasn't using Application.StartupPath. I was just trying to get information out of a file so I wasn't even using a path. I was just doing something like
string someString = File.ReadAllText("FileName.txt");
When it would run normally (not at startup) then it would read and write to the folder where the app was. If it was run at startup however, it would read or write to the user's folder. I misspoke myself about the Application.StartupPath. I have been doing a lot of debugging and apparently that wasn't what happened but I assure you the issue I just presented did happen. The file with a dll is in c:\test. Here is the code. Now the first time it's run as expected there is a file called error.txt which says "Could not find file 'C:\test\test.txt'." The path.txt file and the test.txt files are as expected. However when it runs at startup, the time in the test.txt file doesn't change and when I look in the user folder, I see a new error.txt file along with a test.txt file. The error.txt file says "Could not find file 'C:\Documents and Settings\User\test.txt'." and the time in the test.txt file is more up to date. So again I misspoke before and I assure you it was accidental but and I have found a way to solve my problem but this was my situation. I hope you accept my apology.
A button and form load with the following code.
private void Form1_Load(object sender, EventArgs e)
{
TestClass tc = new TestClass();
tc.ReadFile();
tc.WriteFile();
}
private void button1_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\CurrentFolderTest.exe";
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
File.WriteAllText("path.txt", Application.StartupPath);
key.SetValue("foldertest", path);
}
Here is the code from the dll
public void WriteFile()
{
File.WriteAllText("test.txt", DateTime.Now.ToLongTimeString() + DateTime.Now.Millisecond.ToString());
}
public void ReadFile()
{
try
{
string test = File.ReadAllText("test.txt");
}
catch (Exception ex)
{
File.WriteAllText("error.txt", ex.Message);
}
}
|
|
|
|
|
That's better.
Two problems. First, normal users can't write to the Localmachine registry hive. Writing this key is something you would do at install time, not run time.
Second, whenever you read or write files, it is best practice to always build a fully qualified path to the file using a well known folder, like CommonAppData. Use Environment.GetFolderPath get the base folder path and the use Path.Combine to build your fully qualified file path from it.
Like you already found, the base folder paths between Windows XP and 7 are different. They can even change between 32 and 64 bit versions of Windows.
modified 10-Jun-13 14:15pm.
|
|
|
|
|
hi i just try to make a crystal report in visual studio 2008 using C#, OLE DB(ADO) and the connection was successfully made. but every time when i start to run the application it asks for a login USER NAME and PASSWORD but my database is not secured with any password here is my pies of code to view in CrystalViewer
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("CrystalReport1.rpt");
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
zakirox123
modified 9-Jun-13 3:15am.
|
|
|
|
|
Please do not post the same question in QA and the forums - it duplicates work and annoys people.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
sorry im totally confused thats why i did so?
|
|
|
|
|
Here[^] is a nice article that might help you.
|
|
|
|
|
Hi All,
Can anyone help me how to convert Coordinate from SAD_1969_UTM_Zone_23S to WGS84 using C#.
Ex :-
SAD_1969_UTM_Zone_23S coordiantes
Easting : 1472435.72
Northing : 9484899.68
WGS84 coordinates
lon : -44.0631144
lat : -19.9752943
Thanks & Regards,
Subbarao
|
|
|
|
|
Can you give us the definition of that format?
|
|
|
|
|
I am having datagridview in c#
I populated data inside datagridview.
I created Serial No inside datagridview along with other columns.
If i click serial no head,the serial no is sorted. If i do sorting in other columns,the serial no is also sorted.
How to avoid sorting serial no, if I click other column headers.
I have given the code below
dsstock = new DataSet();
dsstock = fetchDataSetValues(statement);
// grdStock.Rows.Clear();
if (dsstock.Tables[0].Rows.Count > 0)
{
grdStock.Columns.Add("Sl. No.", "Sl. No.");
grdStock.Columns[0].Width = 80;
grdStock.Columns.Add("ItemID", "Item ID");
grdStock.Columns[1].Width =300;
grdStock.Columns.Add("ItemName", "Item Name");
grdStock.Columns[2].Width = 600;
grdStock.Columns.Add("ItemQuantity", "Item Quantity");
grdStock.Columns[3].Width = 150;
int row = dsstock.Tables[0].Rows.Count - 1;
for (int r = 0; r <= row; r++)
{
grdStock.Rows.Add();
grdStock.Rows[r].Cells[0].Value = r + 1;
grdStock.Rows[r].Cells[1].Value = dsstock.Tables[0].Rows[r].ItemArray[0];
grdStock.Rows[r].Cells[2].Value = dsstock.Tables[0].Rows[r].ItemArray[1];
grdStock.Rows[r].Cells[3].Value = dsstock.Tables[0].Rows[r].ItemArray[2];
}
}
Thanks
Chandran
|
|
|
|
|
You can set a particular column sort to Programmatic or NotSortable .
For e.g. DataGridView1.Columns.Item(0).SortMode = DataGridViewColumnSortMode.Programmatic;
More about column sort modes here[^].
|
|
|
|
|
I have generated a product key of 16 digits for my windows application.But i don't know how to use this product key in the application so that whenever a user installs the application, he has to use this key at the time of installing the application.
Please suggest some solution.
AK Barai
|
|
|
|
|
|
How to validate a textbox in my windows form application using c#.
The controls for validation are disabled so i add Validator.dll from external source.But it is not working. Please suggest some solution. What would be the regular expression for validating a text box for a 21 length string with alphabets and digits at fixed places in it.
AK Barai
|
|
|
|
|
Firstly, the standard TextBox has a Validating event which is triggered when the user tries to leave the control - it may be that this is sufficient for your needs.
Secondly, we can't tell you a regex "for validating a text box for a 21 length string with alphabets and digits at fixed places in it" because we have no idea where the various alpha and numeric characters have to be!
The regex will vary considerably depending on exactly what format you require your user to enter.
Thirdly, it may be that you could use a MaskedTextBox instead - these can be set to limit what the user can enter where quite easily.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
|
Hi everyone,
I am currently writing a program in C#. I would love it if I was able to press a button on the keyboard which would cause the program to stop, jump to another section of code, execute the code. Then jump back to where it left off. I understand you can do this with a condition flag (and poling) but coming from embedded programming background there should be a similar way to do this in C# ( i hope!).
If not, what is the best way to go about this.
I appreciate the help.
Thanks
|
|
|
|
|
If I understand what you're talking about, there is no similar construct in C# or .NET.
|
|
|
|
|
Thanks, I was thinking that maybe there was something similar. I appreciate the response.
|
|
|
|
|
Generally, as Dave said, 'no' .. but, if you are writing a C# Console mode program, then, you can use SetConsoleCtrlHandler to trap CTRL-C, CTRL-BREAK
'g'
|
|
|
|
|
Thanks, i will look into it. I appreciate it.
|
|
|
|
|
To add to what Dave and Garth have said, No, but more "no-ish" than an absolute "NO".
If you are using a console app, then it's not simple, but it is possible. If you are writing a WinForms app then it's pretty simple.
In both cases it is a case of moving the "progam" you want to "interrupt" into a separate thread (look at the BackgroundWorker class) and handling input on the normal thread. When anything (such as a keyboard event) happens, it is processed on the main thread not the "program" thread.
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
BackgroundWorker work = new BackgroundWorker();
work.DoWork += new DoWorkEventHandler(work_DoWork);
work.RunWorkerAsync();
if (Console.ReadLine() == "")
{
Console.WriteLine("Aborted");
}
}
static void work_DoWork(object sender, DoWorkEventArgs e)
{
int i = 0;
while (i < 100)
{
Thread.Sleep(500);
Console.WriteLine(i++);
}
} Try that, you'll see what I mean!
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
This may be a possible fit for him, but I didn't think it was a close enough match for interrupt processing.
I took it that he wanted the program code to stop completely, the code attached to the interrupt runs and returns, then the code resumes. Putting the "interruptable" code on a thread keeps the code running, but allows the interrupt handling code to run side-by-side.
Who knows...without a better explanation of of what he really wants to do we're all just guessing.
|
|
|
|
|
I agree, but it's about the closest you are going to get to true interrupt processing under Windows these days - unless you want to start faffing with device drivers (and I wouldn't start with C# for those!)
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Thread.Interrupt[^] may help, but I suspect a little too high level
C# has already designed away most of the tedium of C++.
|
|
|
|
|
The C# debugging API might help.
|
|
|
|