FileInfo equivalent in code analysis in C# - c#

I'm currently in the process of writing analysis code for my job. I have to "map" the code from FxCop to our own private framework. This is a bit of an issue, but not my main concern. I can't access the FileInfo class, my ultimate preview 2015 VS asked me if I wanted to generated it. So, basically, I'm asking what's the equivalent of the FileInfo class while doing code analysis? I've looked into the FileVersionInfo class, but I'm not quite sure if that's the way to go ?
EDITS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace SomeNamespace
{
public class SomeFileValidationClass
{
public FileInfo FileInfo { get; private set; }
public SomeFileValidationClass(FileInfo fileInfo)
{
this.FileInfo = fileInfo;
_failedResults = new List<SomeRuleResult>();
}
//Code missing, but the point still remains that FileInfo can't be accessed.
}

According to the documentation FileInfo is located in the System.IO namespace of mscorlib, so it should be present without needing any other references.
My only suggestion is that you are compiling against some odd version of mscorlib that doesn't have it for some reason, maybe its a bug in VS2015, or perhaps you have downloaded and built corefx?

Related

Is it possible to use 'HttpRuntime' in a NUnit project?

I'm following a guide to write output data from Visual Studio into a google spreadsheet. I'm using a NUnit project type for test-automation purposes.
At the end of the guide there is a code block that I pasted inside my project:
using OpenQA.Selenium.Support.UI;
using System;
using NUnit.Framework;
using OpenQA.Selenium;
using System.Collections;
using System.Collections.Generic;
using Google.Apis.Sheets.v4;
using Google.Apis.Auth.OAuth2;
using System.IO;
using Google.Apis.Services;
using Newtonsoft.Json;
using WikipediaTests.Foundation_Class;
namespace AutomationProjects
{
[TestFixture]
public class TestClass : TestFoundation
{
public class SpreadSheetConnector
{
//Codeblock from guide pasted here!
}
[Test]
public void test1()
{
//Test case 1. Do XYZ...
}
}
}
In the code block included in the guide there is a section that reads the JSON credential file:
private void ConnectToGoogle()
{
GoogleCredential credential;
using (var stream = new FileStream(Path.Combine(HttpRuntime.BinDirectory, "Export Project-03e8aa07234e.json"),
FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(_scopes);
}
//...
But I get an error for the 'HttpRuntime' saying: Error CS0103 The name 'HttpRuntime' does not exist in the current context
There is no suggestion from VS to add a new 'using' reference so I'm assuming that is not the problem.
So what could be the problem? To whole codeblock from the: guide
Short answer - yes.
Long answer - I believe you need to add the System.Web dll here. C# projects do not add all dependencies by default -- rather, they provide you with a list of potential references, and let the user pick and choose on an as-needed basis.
Under your project, find the Dependencies section. Right click, and click Add Reference. Under Assemblies, find System.Web and check the box next to it, then click OK.
Once you add that, then you will need to add using System.Web to the top of your file.
This guide may help too: https://learn.microsoft.com/en-us/visualstudio/ide/how-to-add-or-remove-references-by-using-the-reference-manager?view=vs-2019

Working with embedded resources in external dll, without visual studios

Okay, so I've ran into a problem that I am having trouble finding a solution. There are plenty of solutions using visual studios properties. The issue is that I'm not using visual studios, I'm using csc.exe to compile my code.
Here's what I have so far.
exe.cmd
dll.cmd
Resource.dll
Main.csx
Rsc.csx
a.png
This is all of the code for the .cmd files and .csx files
exe.cmd
#echo off
csc.exe /target:winexe /reference:Resource.dll /out:Main.exe Main.csx
pause
dll.cmd
#echo off
csc.exe /target:library /resource:a.png /out:Resource.dll Rsc.csx
pause
Rsc.csx
using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace EmIm
{
public static class Bck
{
public static Image GetBck()
{
Bitmap bmp = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("a.png"));
Image rtn = bmp;
return rtn;
}
}
}
Main.csx
using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using EmIm;
namespace prg
{
class class_m
{
public static void Main()
{
Form f1 = new Form();
try
{
f1.BackgroundImage = Bck.GetBck();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
f1.ShowDialog();
}
}
}
When I run this, I get a messagebox that displays value of 'null' is not valid for 'stream'
What steps do I need to take to be able to access a.png with reflection, I have checked to make sure a.png is the correct name in the assembly. Any help would be greatly appreciated. Thanks :)
A large thanks to Hans Passant for providing the simple solution to this problem. I had no idea even where to look to find out how to answer this problem, but he summed it up in a single comment.
only one file had to be altered in order for the program to work
new Rsc.csx file
using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
namespace EmIm
{
public static class Bck
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static Image GetBck()
{
Bitmap bmp = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("a.png"));
Image rtn = bmp;
return rtn;
}
}
}
Once again, thanks Hans Passant for that quick fix to my problem, hopefully any other programmers stubborn enough to avoid Visual Studios could possibly look to this to find a solution that works without it. Have fun programming :)

I cant get SevenZipLib library to work (p/invoke)

Very basic knowledge on c# and is the first i use anything p/invoke related.
Help me pls, i have made this code but it doesnt seem to work.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("C:\\Users\\lchris\\Desktop\\SevenZipLib_9.13.2\\SevenZipLib\\SevenZipLib\\7z86.dll")]
public static extern void SevenZipArchive(string c);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SevenZipArchive archive = new SevenZipArchive("file.rar"))
{
foreach (ArchiveEntry entry in archive)
{
Console.WriteLine(entry.FileName);
}
}
}
}
}
It tells me that SevenZipArchive is a 'Method' and is being used like a 'type'.
I have include the library to my project already, i just dont know how to use it.
Here is the library:
https://sevenziplib.codeplex.com/
You first need to remove this code:
[DllImport("...")]
public static extern void SevenZipArchive(string c);
You do not need to provide any p/invoke declarations. The library wraps that up for you.
This is a .net assembly. You use it just as you use any other. Take the following steps:
Download the project. You already did that I think.
Build the solution. Make sure that
In your project, add a reference to the assembly that you built in the previous stage. It's SevenZipLib\bin\Debug\SevenZipLib.dll or SevenZipLib\bin\Release\SevenZipLib.dll depending on the target you selected.
Add using SevenZipLib; to your project code to to gain access to the namespace.
Once you've done that your code will work. You can use the tests project that is supplied as part of the download as a rich source of example code.

C# "await" error when using WinRT from Desktop app

I trying to get my GPS position from a Desktop app, using Windows Runtime, in C#.
I followed this how-to to set up Visual Studio and this code sample to create the following trivial code as a C# console app :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime;
using System.Windows;
using Windows;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
namespace GeoLocCS
{
public sealed partial class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.Read();
}
}
public class Test
{
private Geolocator geolocator;
public Test()
{
Console.WriteLine("test");
geolocator = new Geolocator();
this.getPos();
}
async void getPos()
{
Geoposition pos = await geolocator.GetGeopositionAsync();
Console.WriteLine(pos.Coordinate.Latitude.ToString());
}
}
}
When trying to compile, I get the error :
Error 1 'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Devices.Geolocation.Geoposition>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?
I tried to reference every DLL that I could, like "System.runtime", "System.Runtime.WindowsRuntime", "Windows.Foundation"...
What I am missing ?
Thanks for the answer,
Serge
I finally figured out my problem :
I am on 8.1 so I changed this in the .csproj file (targetingPlatform) instead of targeting 8.0
After that modification, I could reference "Windows"
I manually referenced the two following DLLs :
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll
If you are On Win8.1 Please Use:
<TargetPlatformVersion>8.1</TargetPlatformVersion>
and not 8.0.
Adding to the existing answers:
I suddenly got this problem with a multiproject solution in WPF (still don't know why). I tried switching between TargetPlatformVersions 8.1 and 10.0, tried using the v4.5- and v4.5.1-System.Runtime.WindowsRuntime.dll, always alternating between BadImageFormatException and FileNotFoundException.
Turned out to be references in some of the app.config-files (not the .csproj-files!). Visual Studio 2017 must have added them at some point and as soon as I removed these entries, the above solutions finally worked.

C# DirectX input problem

So i have simple application, just a few lines:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.DirectInput;
namespace asdasd
{
public partial class Form1 : Form
{
public Device joystick;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (
DeviceInstance di in
Manager.GetDevices(
DeviceClass.GameControl,
EnumDevicesFlags.AttachedOnly))
{
joystick = new Device(di.InstanceGuid);
break;
}
if (joystick == null)
{
throw new Exception("No joystick found.");
}
}
}
}
and i try to get the active joystick on my computer, but i get error:
i have the assembly Microsoft.DirectX.DirectInput and i have directX SDK 2010 installed.
Can someone tell me where is the problem?
Try adding this to the config file:
http://devonenote.com/2010/08/mixed-mode-assembly-error-after-upgrading-to-dotnet-4-0/
(if configuration already exists, just merge these in)
And, maybe it's not the right place, but just take a look at XNA... Things are usually much easier with that.
I couldn't paste the XML directly here, it doesn't show up.
The DirectX assemblies are built against .NET v1.1 Microsoft stopped actively developing them before .NET v2.0 was released.
They cannot be used in projects targeting other than .NET v1.1. XNA is the "blessed" path forward for managed access to Direct X features. I don't know all if it's features, but SlimDX appears to give a more Direct X feeling API for C# than XNA, though I have not used it, I've heard a lot about it.
You might find better responses for chosing an upgrade path over at gamedev.stackexchange.com though.

Categories