How to create a file relative to Program.cs [duplicate] - c#

This question already has answers here:
Get the application's path
(21 answers)
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
I want to create a file relative to Program.cs. This code in case of VSCode works correct:
string myFile1 = #".\temp1.txt";
File.Create(myFile1);
string myFile2 = "./temp2.txt";
File.Create(myFile2);
but Visual Studio IDE 2022 creates file in:
`MyProject\bin\Debug\net6.0`
Is there any universal solution?

You can include the following method anywhere within your project:
using System.Runtime.CompilerServices
public static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null )
=> callerFilePath ?? "";
Then, you can invoke that method from your Program.cs, and it will give you C:\Users\YOU\Documents\Projects\MyProject\Program.cs.
I suppose you know what to do from there.

Related

'The system cannot find the file specified' when clicking link [duplicate]

This question already has answers here:
When do we need to set ProcessStartInfo.UseShellExecute to True?
(5 answers)
Closed 2 months ago.
I am following this answer about making hyperlink work in a RichTextBox? by adding this:
private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
System.Diagnostics.Process.Start(e.LinkText);
}
(Actually what I'm really doing is to go to the property of the control, click on the LinkClicked action, and just put the Start() method in there.)
However when clicking the link I get this error:
System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'https://example.com' with working directory 'XYZ'. The system cannot find the file specified.'
Why is that?
If you are using .NET 6 or higher, try this:
Process.Start( new ProcessStartInfo { FileName = e.LinkText , UseShellExecute = true } );

How can i get the PC Name of a client Machine [duplicate]

This question already has answers here:
How to get client's computer name
(3 answers)
Closed 7 months ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
There is any way to get the PC Name of a client Machine in web application, who is working in different Network, in c# asp.net ?
"This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. showing this error.
By doing the answer given in this
How to get client's computer name ,
I think this will help you:
string clienIp = Request.UserHostName;
string computenMame = CompName(clientIp);
public static string CompName(string clienIp)
{
IPAddress myIP = IPAddress.Parse(clienIp);
IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
List<string> compName =
GetIPHost.HostName.ToString().Split('.').ToList();
return compName.First();
}

Add custom file properties programmatically [duplicate]

This question already has answers here:
Add new metadata properties to a file
(1 answer)
Custom File Properties
(4 answers)
Closed 4 years ago.
I need to add custom file properties (see here) to thousands of files programmatically.
The WindowsAPICodePack can get/set existing file properties, but it seems it can not add custom properties?!?
Here the code that works based on Add new metadata properties to a file:
You must reference the DSOFile.dll which can be downloaded from Microsoft here:
Microsoft Developer Support OLE File Property Reader 2.1 Sample
using DSOFile;
OleDocumentProperties myFile = new DSOFile.OleDocumentProperties();
myFile.Open(#"c:\temp\B30700.asm", false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
bool property_exists;
object prop_value;
prop_value = "999";
//Then check if there's already a property like the one you want to create
property_exists = false;
foreach (DSOFile.CustomProperty property in myFile.CustomProperties)
{
if (property.Name == "Your Property Name")
{
//Property exists
//End the task here (return;) oder edit the property
property_exists = true;
property.set_Value(prop_value);
}
}
if (!property_exists)
myFile.CustomProperties.Add("Your Property Name", ref prop_value);
myFile.Save();
myFile.Close(true);

Console Application stops debugging when setting a value to a variable and replacing it [duplicate]

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 7 years ago.
I have started building a simple console application and ran into some weird behavior. I have isolated the problem to the below code. For some reason when the the s.replace line is executed I immediately see this in the output: "'appname.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'" and then "The program '[17680] appname.vshost.exe: Managed' has exited with code -2147023895 (0x800703e9)."
Debugging is stopped. The value in the arg for path is the full UNC path where a file is located. I want to strip the path= and use the path value. Did I skip something somewhere and VS isnt giving me an exception? I've used VS 2008 and 2010 both with the same issue. Is this because the s is an arg?
foreach (string s in args)
{
if ((s != "") && (s.ToString().ToLower().Contains("path=")))
{
string a = #"\\computer\dir\";
a.Replace("path=", "");
}
}
This may be a visual studio issue because it appears to happen on any string I assign a value. I simply added the below removing the replace and got the same response:
string a = #"value";
Path example is \computer\directory1\directory2\
I've updated the code based on suggestions but the above still has the same problem. Fails on the replace line of code.
I don't know if it's the source of your error, but string.Replace returns a new string - it does not modify the underlying string. Plus you are checking for a null value after you check if the string contains a particular substring. The proper loop if you want to update the strings in the collection would be:
for(int i=0; i < args.Length; args++)
{
s = args[i];
if (s != null && s.ToLower().Contains("path="))
args[i] = s.Replace("path=", "");
else
throw new Exception("Missing file path in command line");
}

How to use EntityFramework BulkInsert? [duplicate]

This question already has an answer here:
How to use EntityFramework.BulkInsert?
(1 answer)
Closed 8 years ago.
I'm trying to use, this, but the system can not find the methods of lib...
Nothing that is specified in the documentation of the lib. is running, for example: GetContext() is not found, the own BulkInser is not found .... I put at the top of my code the Using of lib, but nothing works .....
How I can make to use that Lib ? ( I'm using the VS2013 )
My code:
using EntityFramework.BulkInsert.Extensions;
using (var transactionScope = new TransactionScope())
{
var ctx = new MyDBCon.MyDBDataContext();
ctx.BulkInsert(linhas); // error in BulkInsert ( method not found )
ctx.SubmitChanges();
transactionScope.Complete();
}
You need to include the library...
using EntityFramework.BulkInsert.Extensions;
Add this to the top of your class.

Categories

Resources